pythread.h 5.6 KB

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