pythread.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #ifndef Py_PYTHREAD_H
  2. #define Py_PYTHREAD_H
  3. typedef void *PyThread_type_lock;
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #ifdef __MINGW32__
  8. # if !defined(HAVE_PTHREAD_H) || defined(NT_THREADS)
  9. # undef _POSIX_THREADS
  10. # endif
  11. #endif
  12. /* Return status codes for Python lock acquisition. Chosen for maximum
  13. * backwards compatibility, ie failure -> 0, success -> 1. */
  14. typedef enum PyLockStatus {
  15. PY_LOCK_FAILURE = 0,
  16. PY_LOCK_ACQUIRED = 1,
  17. PY_LOCK_INTR
  18. } PyLockStatus;
  19. #ifndef Py_LIMITED_API
  20. #define PYTHREAD_INVALID_THREAD_ID ((unsigned long)-1)
  21. #endif
  22. PyAPI_FUNC(void) PyThread_init_thread(void);
  23. PyAPI_FUNC(unsigned long) PyThread_start_new_thread(void (*)(void *), void *);
  24. PyAPI_FUNC(void) _Py_NO_RETURN PyThread_exit_thread(void);
  25. PyAPI_FUNC(unsigned long) PyThread_get_thread_ident(void);
  26. #if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(_WIN32) || defined(_AIX)
  27. #define PY_HAVE_THREAD_NATIVE_ID
  28. PyAPI_FUNC(unsigned long) PyThread_get_thread_native_id(void);
  29. #endif
  30. PyAPI_FUNC(PyThread_type_lock) PyThread_allocate_lock(void);
  31. PyAPI_FUNC(void) PyThread_free_lock(PyThread_type_lock);
  32. PyAPI_FUNC(int) PyThread_acquire_lock(PyThread_type_lock, int);
  33. #define WAIT_LOCK 1
  34. #define NOWAIT_LOCK 0
  35. #ifndef Py_LIMITED_API
  36. #ifdef HAVE_FORK
  37. /* Private function to reinitialize a lock at fork in the child process.
  38. Reset the lock to the unlocked state.
  39. Return 0 on success, return -1 on error. */
  40. PyAPI_FUNC(int) _PyThread_at_fork_reinit(PyThread_type_lock *lock);
  41. #endif /* HAVE_FORK */
  42. #endif /* !Py_LIMITED_API */
  43. /* PY_TIMEOUT_T is the integral type used to specify timeouts when waiting
  44. on a lock (see PyThread_acquire_lock_timed() below).
  45. PY_TIMEOUT_MAX is the highest usable value (in microseconds) of that
  46. type, and depends on the system threading API.
  47. NOTE: this isn't the same value as `_thread.TIMEOUT_MAX`. The _thread
  48. module exposes a higher-level API, with timeouts expressed in seconds
  49. and floating-point numbers allowed.
  50. */
  51. #define PY_TIMEOUT_T long long
  52. #if defined(_POSIX_THREADS)
  53. /* PyThread_acquire_lock_timed() uses _PyTime_FromNanoseconds(us * 1000),
  54. convert microseconds to nanoseconds. */
  55. # define PY_TIMEOUT_MAX (LLONG_MAX / 1000)
  56. #elif defined (NT_THREADS)
  57. /* In the NT API, the timeout is a DWORD and is expressed in milliseconds */
  58. # if 0xFFFFFFFFLL * 1000 < LLONG_MAX
  59. # define PY_TIMEOUT_MAX (0xFFFFFFFFLL * 1000)
  60. # else
  61. # define PY_TIMEOUT_MAX LLONG_MAX
  62. # endif
  63. #else
  64. # define PY_TIMEOUT_MAX LLONG_MAX
  65. #endif
  66. /* If microseconds == 0, the call is non-blocking: it returns immediately
  67. even when the lock can't be acquired.
  68. If microseconds > 0, the call waits up to the specified duration.
  69. If microseconds < 0, the call waits until success (or abnormal failure)
  70. microseconds must be less than PY_TIMEOUT_MAX. Behaviour otherwise is
  71. undefined.
  72. If intr_flag is true and the acquire is interrupted by a signal, then the
  73. call will return PY_LOCK_INTR. The caller may reattempt to acquire the
  74. lock.
  75. */
  76. PyAPI_FUNC(PyLockStatus) PyThread_acquire_lock_timed(PyThread_type_lock,
  77. PY_TIMEOUT_T microseconds,
  78. int intr_flag);
  79. PyAPI_FUNC(void) PyThread_release_lock(PyThread_type_lock);
  80. PyAPI_FUNC(size_t) PyThread_get_stacksize(void);
  81. PyAPI_FUNC(int) PyThread_set_stacksize(size_t);
  82. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  83. PyAPI_FUNC(PyObject*) PyThread_GetInfo(void);
  84. #endif
  85. /* Thread Local Storage (TLS) API
  86. TLS API is DEPRECATED. Use Thread Specific Storage (TSS) API.
  87. The existing TLS API has used int to represent TLS keys across all
  88. platforms, but it is not POSIX-compliant. Therefore, the new TSS API uses
  89. opaque data type to represent TSS keys to be compatible (see PEP 539).
  90. */
  91. Py_DEPRECATED(3.7) PyAPI_FUNC(int) PyThread_create_key(void);
  92. Py_DEPRECATED(3.7) PyAPI_FUNC(void) PyThread_delete_key(int key);
  93. Py_DEPRECATED(3.7) PyAPI_FUNC(int) PyThread_set_key_value(int key,
  94. void *value);
  95. Py_DEPRECATED(3.7) PyAPI_FUNC(void *) PyThread_get_key_value(int key);
  96. Py_DEPRECATED(3.7) PyAPI_FUNC(void) PyThread_delete_key_value(int key);
  97. /* Cleanup after a fork */
  98. Py_DEPRECATED(3.7) PyAPI_FUNC(void) PyThread_ReInitTLS(void);
  99. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
  100. /* New in 3.7 */
  101. /* Thread Specific Storage (TSS) API */
  102. typedef struct _Py_tss_t Py_tss_t; /* opaque */
  103. #ifndef Py_LIMITED_API
  104. #if defined(_POSIX_THREADS)
  105. /* Darwin needs pthread.h to know type name the pthread_key_t. */
  106. # include <pthread.h>
  107. # define NATIVE_TSS_KEY_T pthread_key_t
  108. #elif defined(NT_THREADS)
  109. /* In Windows, native TSS key type is DWORD,
  110. but hardcode the unsigned long to avoid errors for include directive.
  111. */
  112. # define NATIVE_TSS_KEY_T unsigned long
  113. #else
  114. # error "Require native threads. See https://bugs.python.org/issue31370"
  115. #endif
  116. /* When Py_LIMITED_API is not defined, the type layout of Py_tss_t is
  117. exposed to allow static allocation in the API clients. Even in this case,
  118. you must handle TSS keys through API functions due to compatibility.
  119. */
  120. struct _Py_tss_t {
  121. int _is_initialized;
  122. NATIVE_TSS_KEY_T _key;
  123. };
  124. #undef NATIVE_TSS_KEY_T
  125. /* When static allocation, you must initialize with Py_tss_NEEDS_INIT. */
  126. #define Py_tss_NEEDS_INIT {0}
  127. #endif /* !Py_LIMITED_API */
  128. PyAPI_FUNC(Py_tss_t *) PyThread_tss_alloc(void);
  129. PyAPI_FUNC(void) PyThread_tss_free(Py_tss_t *key);
  130. /* The parameter key must not be NULL. */
  131. PyAPI_FUNC(int) PyThread_tss_is_created(Py_tss_t *key);
  132. PyAPI_FUNC(int) PyThread_tss_create(Py_tss_t *key);
  133. PyAPI_FUNC(void) PyThread_tss_delete(Py_tss_t *key);
  134. PyAPI_FUNC(int) PyThread_tss_set(Py_tss_t *key, void *value);
  135. PyAPI_FUNC(void *) PyThread_tss_get(Py_tss_t *key);
  136. #endif /* New in 3.7 */
  137. #ifdef __cplusplus
  138. }
  139. #endif
  140. #endif /* !Py_PYTHREAD_H */