Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:50

0001 // The _PySemaphore API a simplified cross-platform semaphore used to implement
0002 // wakeup/sleep.
0003 #ifndef Py_INTERNAL_SEMAPHORE_H
0004 #define Py_INTERNAL_SEMAPHORE_H
0005 
0006 #ifndef Py_BUILD_CORE
0007 #  error "this header requires Py_BUILD_CORE define"
0008 #endif
0009 
0010 #include "pycore_pythread.h"      // _POSIX_SEMAPHORES
0011 
0012 #ifdef MS_WINDOWS
0013 #   ifndef WIN32_LEAN_AND_MEAN
0014 #       define WIN32_LEAN_AND_MEAN
0015 #   endif
0016 #   include <windows.h>
0017 #elif defined(HAVE_PTHREAD_H)
0018 #   include <pthread.h>
0019 #elif defined(HAVE_PTHREAD_STUBS)
0020 #   include "cpython/pthread_stubs.h"
0021 #else
0022 #   error "Require native threads. See https://bugs.python.org/issue31370"
0023 #endif
0024 
0025 #if (defined(_POSIX_SEMAPHORES) && (_POSIX_SEMAPHORES+0) != -1 && \
0026         defined(HAVE_SEM_TIMEDWAIT))
0027 #   define _Py_USE_SEMAPHORES
0028 #   include <semaphore.h>
0029 #endif
0030 
0031 
0032 #ifdef __cplusplus
0033 extern "C" {
0034 #endif
0035 
0036 typedef struct _PySemaphore {
0037 #if defined(MS_WINDOWS)
0038     HANDLE platform_sem;
0039 #elif defined(_Py_USE_SEMAPHORES)
0040     sem_t platform_sem;
0041 #else
0042     pthread_mutex_t mutex;
0043     pthread_cond_t cond;
0044     int counter;
0045 #endif
0046 } _PySemaphore;
0047 
0048 // Puts the current thread to sleep until _PySemaphore_Wakeup() is called.
0049 // If `detach` is true, then the thread will detach/release the GIL while
0050 // sleeping.
0051 PyAPI_FUNC(int)
0052 _PySemaphore_Wait(_PySemaphore *sema, PyTime_t timeout_ns, int detach);
0053 
0054 // Wakes up a single thread waiting on sema. Note that _PySemaphore_Wakeup()
0055 // can be called before _PySemaphore_Wait().
0056 PyAPI_FUNC(void)
0057 _PySemaphore_Wakeup(_PySemaphore *sema);
0058 
0059 // Initializes/destroys a semaphore
0060 PyAPI_FUNC(void) _PySemaphore_Init(_PySemaphore *sema);
0061 PyAPI_FUNC(void) _PySemaphore_Destroy(_PySemaphore *sema);
0062 
0063 
0064 #ifdef __cplusplus
0065 }
0066 #endif
0067 #endif /* !Py_INTERNAL_SEMAPHORE_H */