| 1 |
#include <Python.h> |
| 2 |
#include "src/thread.h" |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
long |
| 10 |
PyThread_get_thread_ident(void) |
| 11 |
{ |
| 12 |
#ifdef _WIN32 |
| 13 |
return (long)GetCurrentThreadId(); |
| 14 |
#else |
| 15 |
return (long)pthread_self(); |
| 16 |
#endif |
| 17 |
} |
| 18 |
|
| 19 |
static int initialized; |
| 20 |
|
| 21 |
void |
| 22 |
PyThread_init_thread(void) |
| 23 |
{ |
| 24 |
if (initialized) |
| 25 |
return; |
| 26 |
initialized = 1; |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
PyThread_type_lock |
| 31 |
PyThread_allocate_lock(void) |
| 32 |
{ |
| 33 |
struct RPyOpaque_ThreadLock *lock; |
| 34 |
lock = malloc(sizeof(struct RPyOpaque_ThreadLock)); |
| 35 |
if (lock == NULL) |
| 36 |
return NULL; |
| 37 |
|
| 38 |
if (RPyThreadLockInit(lock) == 0) { |
| 39 |
free(lock); |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
|
| 43 |
return (PyThread_type_lock)lock; |
| 44 |
} |
| 45 |
|
| 46 |
void |
| 47 |
PyThread_free_lock(PyThread_type_lock lock) |
| 48 |
{ |
| 49 |
struct RPyOpaque_ThreadLock *real_lock = lock; |
| 50 |
RPyThreadAcquireLock(real_lock, 0); |
| 51 |
RPyThreadReleaseLock(real_lock); |
| 52 |
RPyOpaqueDealloc_ThreadLock(real_lock); |
| 53 |
free(lock); |
| 54 |
} |
| 55 |
|
| 56 |
int |
| 57 |
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
| 58 |
{ |
| 59 |
return RPyThreadAcquireLock((struct RPyOpaque_ThreadLock*)lock, waitflag); |
| 60 |
} |
| 61 |
|
| 62 |
PyLockStatus |
| 63 |
PyThread_acquire_lock_timed(PyThread_type_lock lock, |
| 64 |
PY_TIMEOUT_T microseconds, |
| 65 |
int intr_flag) |
| 66 |
{ |
| 67 |
return (PyLockStatus)RPyThreadAcquireLockTimed((struct RPyOpaque_ThreadLock*)lock, microseconds, intr_flag); |
| 68 |
} |
| 69 |
|
| 70 |
void |
| 71 |
PyThread_release_lock(PyThread_type_lock lock) |
| 72 |
{ |
| 73 |
RPyThreadReleaseLock((struct RPyOpaque_ThreadLock*)lock); |
| 74 |
} |
| 75 |
|
| 76 |
long |
| 77 |
PyThread_start_new_thread(void (*func)(void *), void *arg) |
| 78 |
{ |
| 79 |
PyThread_init_thread(); |
| 80 |
return RPyThreadStartEx(func, arg); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
Py_tss_t * |
| 86 |
PyThread_tss_alloc(void) |
| 87 |
{ |
| 88 |
Py_tss_t *new_key = (Py_tss_t *)PyMem_RawMalloc(sizeof(Py_tss_t)); |
| 89 |
if (new_key == NULL) { |
| 90 |
return NULL; |
| 91 |
} |
| 92 |
new_key->_is_initialized = 0; |
| 93 |
return new_key; |
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
PyThread_tss_free(Py_tss_t *key) |
| 98 |
{ |
| 99 |
if (key != NULL) { |
| 100 |
PyThread_tss_delete(key); |
| 101 |
PyMem_RawFree((void *)key); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
int |
| 106 |
PyThread_tss_is_created(Py_tss_t *key) |
| 107 |
{ |
| 108 |
assert(key != NULL); |
| 109 |
return key->_is_initialized; |
| 110 |
} |
| 111 |
|
| 112 |
|