pythread.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. PyAPI_FUNC(void) PyThread_init_thread(void);
  15. PyAPI_FUNC(unsigned long) PyThread_start_new_thread(void (*)(void *), void *);
  16. PyAPI_FUNC(void) _Py_NO_RETURN PyThread_exit_thread(void);
  17. PyAPI_FUNC(unsigned long) PyThread_get_thread_ident(void);
  18. #if (defined(__APPLE__) || defined(__linux__) || defined(_WIN32) \
  19. || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
  20. || defined(__DragonFly__) || defined(_AIX))
  21. #define PY_HAVE_THREAD_NATIVE_ID
  22. PyAPI_FUNC(unsigned long) PyThread_get_thread_native_id(void);
  23. #endif
  24. PyAPI_FUNC(PyThread_type_lock) PyThread_allocate_lock(void);
  25. PyAPI_FUNC(void) PyThread_free_lock(PyThread_type_lock);
  26. PyAPI_FUNC(int) PyThread_acquire_lock(PyThread_type_lock, int);
  27. #define WAIT_LOCK 1
  28. #define NOWAIT_LOCK 0
  29. /* PY_TIMEOUT_T is the integral type used to specify timeouts when waiting
  30. on a lock (see PyThread_acquire_lock_timed() below).
  31. PY_TIMEOUT_MAX is the highest usable value (in microseconds) of that
  32. type, and depends on the system threading API.
  33. NOTE: this isn't the same value as `_thread.TIMEOUT_MAX`. The _thread
  34. module exposes a higher-level API, with timeouts expressed in seconds
  35. and floating-point numbers allowed.
  36. */
  37. #define PY_TIMEOUT_T long long
  38. #if defined(_POSIX_THREADS)
  39. /* PyThread_acquire_lock_timed() uses _PyTime_FromNanoseconds(us * 1000),
  40. convert microseconds to nanoseconds. */
  41. # define PY_TIMEOUT_MAX (LLONG_MAX / 1000)
  42. #elif defined (NT_THREADS)
  43. // WaitForSingleObject() accepts timeout in milliseconds in the range
  44. // [0; 0xFFFFFFFE] (DWORD type). INFINITE value (0xFFFFFFFF) means no
  45. // timeout. 0xFFFFFFFE milliseconds is around 49.7 days.
  46. # if 0xFFFFFFFELL * 1000 < LLONG_MAX
  47. # define PY_TIMEOUT_MAX (0xFFFFFFFELL * 1000)
  48. # else
  49. # define PY_TIMEOUT_MAX LLONG_MAX
  50. # endif
  51. #else
  52. # define PY_TIMEOUT_MAX 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. PyAPI_FUNC(Py_tss_t *) PyThread_tss_alloc(void);
  92. PyAPI_FUNC(void) PyThread_tss_free(Py_tss_t *key);
  93. /* The parameter key must not be NULL. */
  94. PyAPI_FUNC(int) PyThread_tss_is_created(Py_tss_t *key);
  95. PyAPI_FUNC(int) PyThread_tss_create(Py_tss_t *key);
  96. PyAPI_FUNC(void) PyThread_tss_delete(Py_tss_t *key);
  97. PyAPI_FUNC(int) PyThread_tss_set(Py_tss_t *key, void *value);
  98. PyAPI_FUNC(void *) PyThread_tss_get(Py_tss_t *key);
  99. #endif /* New in 3.7 */
  100. #ifndef Py_LIMITED_API
  101. # define Py_CPYTHON_PYTHREAD_H
  102. # include "cpython/pythread.h"
  103. # undef Py_CPYTHON_PYTHREAD_H
  104. #endif
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #endif /* !Py_PYTHREAD_H */