pythread.h 5.2 KB

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