pythread.h 5.8 KB

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