pythread.h 2.92 KB
Newer Older
1

2 3 4
#ifndef Py_PYTHREAD_H
#define Py_PYTHREAD_H

5 6
typedef void *PyThread_type_lock;
typedef void *PyThread_type_sema;
7 8 9 10 11

#ifdef __cplusplus
extern "C" {
#endif

12 13 14 15 16 17 18 19
/* Return status codes for Python lock acquisition.  Chosen for maximum
 * backwards compatibility, ie failure -> 0, success -> 1.  */
typedef enum PyLockStatus {
    PY_LOCK_FAILURE = 0,
    PY_LOCK_ACQUIRED = 1,
    PY_LOCK_INTR
} PyLockStatus;

20 21 22 23 24 25 26 27
PyAPI_FUNC(void) PyThread_init_thread(void);
PyAPI_FUNC(long) PyThread_start_new_thread(void (*)(void *), void *);
PyAPI_FUNC(void) PyThread_exit_thread(void);
PyAPI_FUNC(long) PyThread_get_thread_ident(void);

PyAPI_FUNC(PyThread_type_lock) PyThread_allocate_lock(void);
PyAPI_FUNC(void) PyThread_free_lock(PyThread_type_lock);
PyAPI_FUNC(int) PyThread_acquire_lock(PyThread_type_lock, int);
28 29
#define WAIT_LOCK	1
#define NOWAIT_LOCK	0
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

/* PY_TIMEOUT_T is the integral type used to specify timeouts when waiting
   on a lock (see PyThread_acquire_lock_timed() below).
   PY_TIMEOUT_MAX is the highest usable value (in microseconds) of that
   type, and depends on the system threading API.
   
   NOTE: this isn't the same value as `_thread.TIMEOUT_MAX`.  The _thread
   module exposes a higher-level API, with timeouts expressed in seconds
   and floating-point numbers allowed.
*/
#if defined(HAVE_LONG_LONG)
#define PY_TIMEOUT_T PY_LONG_LONG
#define PY_TIMEOUT_MAX PY_LLONG_MAX
#else
#define PY_TIMEOUT_T long
#define PY_TIMEOUT_MAX LONG_MAX
#endif

/* In the NT API, the timeout is a DWORD and is expressed in milliseconds */
#if defined (NT_THREADS)
50
#if (Py_LL(0xFFFFFFFF) * 1000 < PY_TIMEOUT_MAX)
51
#undef PY_TIMEOUT_MAX
52
#define PY_TIMEOUT_MAX (Py_LL(0xFFFFFFFF) * 1000)
53 54 55 56 57 58 59
#endif
#endif

/* If microseconds == 0, the call is non-blocking: it returns immediately
   even when the lock can't be acquired.
   If microseconds > 0, the call waits up to the specified duration.
   If microseconds < 0, the call waits until success (or abnormal failure)
60

61
   microseconds must be less than PY_TIMEOUT_MAX. Behaviour otherwise is
62 63 64 65 66 67 68 69 70 71
   undefined.

   If intr_flag is true and the acquire is interrupted by a signal, then the
   call will return PY_LOCK_INTR.  The caller may reattempt to acquire the
   lock.
*/
PyAPI_FUNC(PyLockStatus) PyThread_acquire_lock_timed(PyThread_type_lock,
                                                     PY_TIMEOUT_T microseconds,
                                                     int intr_flag);

72
PyAPI_FUNC(void) PyThread_release_lock(PyThread_type_lock);
73

74 75 76
PyAPI_FUNC(size_t) PyThread_get_stacksize(void);
PyAPI_FUNC(int) PyThread_set_stacksize(size_t);

77
/* Thread Local Storage (TLS) API */
78 79 80 81
PyAPI_FUNC(int) PyThread_create_key(void);
PyAPI_FUNC(void) PyThread_delete_key(int);
PyAPI_FUNC(int) PyThread_set_key_value(int, void *);
PyAPI_FUNC(void *) PyThread_get_key_value(int);
82
PyAPI_FUNC(void) PyThread_delete_key_value(int key);
83

84 85 86
/* Cleanup after a fork */
PyAPI_FUNC(void) PyThread_ReInitTLS(void);

87 88 89 90
#ifdef __cplusplus
}
#endif

91
#endif /* !Py_PYTHREAD_H */