blob: d3dc2bec6ffebbf926bb4f24b7c95007beaa2651 [file] [log] [blame]
Guido van Rossumc3f82b61995-01-17 16:29:31 +00001
2/* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */
Guido van Rossum706262b2000-05-04 18:47:15 +00003/* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */
Tim Peterse64ef932002-02-28 21:34:34 +00004/* Eliminated some memory leaks, gsw@agere.com */
Guido van Rossumc3f82b61995-01-17 16:29:31 +00005
Guido van Rossum49b12261997-08-14 20:12:58 +00006#include <windows.h>
7#include <limits.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008#ifdef HAVE_PROCESS_H
Guido van Rossum49b12261997-08-14 20:12:58 +00009#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010#endif
Guido van Rossumc3f82b61995-01-17 16:29:31 +000011
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000012/* options */
13#ifndef _PY_USE_CV_LOCKS
14#define _PY_USE_CV_LOCKS 1 /* use locks based on cond vars */
15#endif
16
17/* Now, define a non-recursive mutex using either condition variables
18 * and critical sections (fast) or using operating system mutexes
19 * (slow)
20 */
21
22#if _PY_USE_CV_LOCKS
23
24#include "condvar.h"
25
26typedef struct _NRMUTEX
27{
28 PyMUTEX_T cs;
29 PyCOND_T cv;
30 int locked;
31} NRMUTEX;
32typedef NRMUTEX *PNRMUTEX;
33
34PNRMUTEX
35AllocNonRecursiveMutex()
36{
Victor Stinner80aa5652013-07-07 17:17:59 +020037 PNRMUTEX m = (PNRMUTEX)PyMem_RawMalloc(sizeof(NRMUTEX));
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000038 if (!m)
39 return NULL;
40 if (PyCOND_INIT(&m->cv))
41 goto fail;
42 if (PyMUTEX_INIT(&m->cs)) {
43 PyCOND_FINI(&m->cv);
44 goto fail;
45 }
46 m->locked = 0;
47 return m;
48fail:
Victor Stinner80aa5652013-07-07 17:17:59 +020049 PyMem_RawFree(m);
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000050 return NULL;
51}
52
53VOID
54FreeNonRecursiveMutex(PNRMUTEX mutex)
55{
56 if (mutex) {
57 PyCOND_FINI(&mutex->cv);
58 PyMUTEX_FINI(&mutex->cs);
Victor Stinner80aa5652013-07-07 17:17:59 +020059 PyMem_RawFree(mutex);
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000060 }
61}
62
63DWORD
64EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
65{
66 DWORD result = WAIT_OBJECT_0;
67 if (PyMUTEX_LOCK(&mutex->cs))
68 return WAIT_FAILED;
69 if (milliseconds == INFINITE) {
70 while (mutex->locked) {
71 if (PyCOND_WAIT(&mutex->cv, &mutex->cs)) {
72 result = WAIT_FAILED;
73 break;
74 }
75 }
76 } else if (milliseconds != 0) {
77 /* wait at least until the target */
78 DWORD now, target = GetTickCount() + milliseconds;
79 while (mutex->locked) {
Benjamin Petersonaf580df2016-09-06 10:46:49 -070080 if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, (long long)milliseconds*1000) < 0) {
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000081 result = WAIT_FAILED;
82 break;
83 }
84 now = GetTickCount();
85 if (target <= now)
86 break;
87 milliseconds = target-now;
88 }
89 }
90 if (!mutex->locked) {
91 mutex->locked = 1;
92 result = WAIT_OBJECT_0;
93 } else if (result == WAIT_OBJECT_0)
94 result = WAIT_TIMEOUT;
95 /* else, it is WAIT_FAILED */
96 PyMUTEX_UNLOCK(&mutex->cs); /* must ignore result here */
97 return result;
98}
99
100BOOL
101LeaveNonRecursiveMutex(PNRMUTEX mutex)
102{
103 BOOL result;
104 if (PyMUTEX_LOCK(&mutex->cs))
105 return FALSE;
106 mutex->locked = 0;
native-api05e92212019-02-02 19:22:55 +0300107 /* condvar APIs return 0 on success. We need to return TRUE on success. */
108 result = !PyCOND_SIGNAL(&mutex->cv);
109 PyMUTEX_UNLOCK(&mutex->cs);
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +0000110 return result;
Victor Stinner80aa5652013-07-07 17:17:59 +0200111}
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +0000112
113#else /* if ! _PY_USE_CV_LOCKS */
114
115/* NR-locks based on a kernel mutex */
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200116#define PNRMUTEX HANDLE
Guido van Rossum706262b2000-05-04 18:47:15 +0000117
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200118PNRMUTEX
119AllocNonRecursiveMutex()
Guido van Rossum706262b2000-05-04 18:47:15 +0000120{
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200121 return CreateSemaphore(NULL, 1, 1, NULL);
Guido van Rossum706262b2000-05-04 18:47:15 +0000122}
123
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000124VOID
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200125FreeNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +0000126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 /* No in-use check */
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200128 CloseHandle(mutex);
Guido van Rossum706262b2000-05-04 18:47:15 +0000129}
130
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000131DWORD
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000132EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
Guido van Rossum706262b2000-05-04 18:47:15 +0000133{
Martin v. Löwisb26a9b12013-01-25 14:25:48 +0100134 return WaitForSingleObjectEx(mutex, milliseconds, FALSE);
Guido van Rossum706262b2000-05-04 18:47:15 +0000135}
136
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000137BOOL
138LeaveNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +0000139{
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200140 return ReleaseSemaphore(mutex, 1, NULL);
Guido van Rossum706262b2000-05-04 18:47:15 +0000141}
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +0000142#endif /* _PY_USE_CV_LOCKS */
Guido van Rossum706262b2000-05-04 18:47:15 +0000143
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200144unsigned long PyThread_get_thread_ident(void);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000145
Jake Tesler4959c332019-05-12 10:08:24 -0700146unsigned long PyThread_get_thread_native_id(void);
147
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000148/*
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000149 * Initialization of the C package, should not be needed.
150 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000151static void
152PyThread__init_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000153{
154}
155
156/*
157 * Thread support.
158 */
Guido van Rossum3c288632001-10-16 21:13:49 +0000159
160typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 void (*func)(void*);
162 void *arg;
Guido van Rossum3c288632001-10-16 21:13:49 +0000163} callobj;
164
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000165/* thunker to call adapt between the function type used by the system's
166thread start function and the internally used one. */
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000167static unsigned __stdcall
Guido van Rossum3c288632001-10-16 21:13:49 +0000168bootstrap(void *call)
169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 callobj *obj = (callobj*)call;
171 void (*func)(void*) = obj->func;
172 void *arg = obj->arg;
173 HeapFree(GetProcessHeap(), 0, obj);
174 func(arg);
175 return 0;
Guido van Rossum3c288632001-10-16 21:13:49 +0000176}
177
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200178unsigned long
Tim Peters2e7e7df2003-07-04 04:40:45 +0000179PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 HANDLE hThread;
182 unsigned threadID;
183 callobj *obj;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000184
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200185 dprintf(("%lu: PyThread_start_new_thread called\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 PyThread_get_thread_ident()));
187 if (!initialized)
188 PyThread_init_thread();
189
190 obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
191 if (!obj)
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200192 return PYTHREAD_INVALID_THREAD_ID;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 obj->func = func;
194 obj->arg = arg;
Victor Stinner50b48572018-11-01 01:51:40 +0100195 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600196 size_t stacksize = tstate ? tstate->interp->pythread_stacksize : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 hThread = (HANDLE)_beginthreadex(0,
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600198 Py_SAFE_DOWNCAST(stacksize, Py_ssize_t, unsigned int),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 bootstrap, obj,
200 0, &threadID);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 if (hThread == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 /* I've seen errno == EAGAIN here, which means "there are
203 * too many threads".
204 */
205 int e = errno;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200206 dprintf(("%lu: PyThread_start_new_thread failed, errno %d\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 PyThread_get_thread_ident(), e));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 threadID = (unsigned)-1;
209 HeapFree(GetProcessHeap(), 0, obj);
210 }
211 else {
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200212 dprintf(("%lu: PyThread_start_new_thread succeeded: %p\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 PyThread_get_thread_ident(), (void*)hThread));
214 CloseHandle(hThread);
215 }
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200216 return threadID;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000217}
218
219/*
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300220 * Return the thread Id instead of a handle. The Id is said to uniquely identify the
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000221 * thread in the system
222 */
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200223unsigned long
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000224PyThread_get_thread_ident(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (!initialized)
227 PyThread_init_thread();
Guido van Rossum706262b2000-05-04 18:47:15 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 return GetCurrentThreadId();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000230}
231
Jake Tesler4959c332019-05-12 10:08:24 -0700232/*
233 * Return the native Thread ID (TID) of the calling thread.
234 * The native ID of a thread is valid and guaranteed to be unique system-wide
235 * from the time the thread is created until the thread has been terminated.
236 */
237unsigned long
238PyThread_get_thread_native_id(void)
239{
240 if (!initialized)
241 PyThread_init_thread();
242
243 return GetCurrentThreadId();
244}
245
Victor Stinnerc664b342019-05-04 11:48:05 -0400246void _Py_NO_RETURN
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000247PyThread_exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000248{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200249 dprintf(("%lu: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 if (!initialized)
251 exit(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 _endthreadex(0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000253}
254
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000255/*
hui shang60278022018-01-18 07:21:01 +0800256 * Lock support. It has to be implemented as semaphores.
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000257 * I [Dag] tried to implement it with mutex but I could find a way to
258 * tell whether a thread already own the lock or not.
259 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000260PyThread_type_lock
261PyThread_allocate_lock(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 PNRMUTEX aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 dprintf(("PyThread_allocate_lock called\n"));
266 if (!initialized)
267 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 aLock = AllocNonRecursiveMutex() ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000270
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200271 dprintf(("%lu: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 return (PyThread_type_lock) aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000274}
275
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000276void
277PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000278{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200279 dprintf(("%lu: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 FreeNonRecursiveMutex(aLock) ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000282}
283
284/*
285 * Return 1 on success if the lock was acquired
286 *
287 * and 0 if the lock was not acquired. This means a 0 is returned
288 * if the lock has already been acquired by this thread!
289 */
Antoine Pitrou810023d2010-12-15 22:59:16 +0000290PyLockStatus
291PyThread_acquire_lock_timed(PyThread_type_lock aLock,
292 PY_TIMEOUT_T microseconds, int intr_flag)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000293{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000294 /* Fow now, intr_flag does nothing on Windows, and lock acquires are
295 * uninterruptible. */
296 PyLockStatus success;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 PY_TIMEOUT_T milliseconds;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (microseconds >= 0) {
300 milliseconds = microseconds / 1000;
301 if (microseconds % 1000 > 0)
302 ++milliseconds;
Victor Stinner850a18e2017-10-24 16:53:32 -0700303 if (milliseconds > PY_DWORD_MAX) {
304 Py_FatalError("Timeout larger than PY_TIMEOUT_MAX");
305 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 }
Victor Stinner850a18e2017-10-24 16:53:32 -0700307 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 milliseconds = INFINITE;
Victor Stinner850a18e2017-10-24 16:53:32 -0700309 }
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000310
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200311 dprintf(("%lu: PyThread_acquire_lock_timed(%p, %lld) called\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 PyThread_get_thread_ident(), aLock, microseconds));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000313
Antoine Pitrou810023d2010-12-15 22:59:16 +0000314 if (aLock && EnterNonRecursiveMutex((PNRMUTEX)aLock,
315 (DWORD)milliseconds) == WAIT_OBJECT_0) {
316 success = PY_LOCK_ACQUIRED;
317 }
318 else {
319 success = PY_LOCK_FAILURE;
320 }
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000321
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200322 dprintf(("%lu: PyThread_acquire_lock(%p, %lld) -> %d\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 PyThread_get_thread_ident(), aLock, microseconds, success));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 return success;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000326}
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000327int
328PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
329{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000330 return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0);
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000331}
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000332
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000333void
334PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000335{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200336 dprintf(("%lu: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200339 dprintf(("%lu: Could not PyThread_release_lock(%p) error: %ld\n", PyThread_get_thread_ident(), aLock, GetLastError()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000340}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000341
342/* minimum/maximum thread stack sizes supported */
Victor Stinner8c663fd2017-11-08 14:44:44 -0800343#define THREAD_MIN_STACKSIZE 0x8000 /* 32 KiB */
344#define THREAD_MAX_STACKSIZE 0x10000000 /* 256 MiB */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000345
346/* set the thread stack size.
347 * Return 0 if size is valid, -1 otherwise.
348 */
349static int
350_pythread_nt_set_stacksize(size_t size)
351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 /* set to default */
353 if (size == 0) {
Victor Stinner50b48572018-11-01 01:51:40 +0100354 _PyInterpreterState_GET_UNSAFE()->pythread_stacksize = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 return 0;
356 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 /* valid range? */
359 if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
Victor Stinner50b48572018-11-01 01:51:40 +0100360 _PyInterpreterState_GET_UNSAFE()->pythread_stacksize = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 return 0;
362 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000365}
366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367#define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000368
369
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900370/* Thread Local Storage (TLS) API
371
372 This API is DEPRECATED since Python 3.7. See PEP 539 for details.
373*/
374
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000375int
376PyThread_create_key(void)
377{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900378 DWORD result = TlsAlloc();
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000379 if (result == TLS_OUT_OF_INDEXES)
380 return -1;
381 return (int)result;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000382}
383
384void
385PyThread_delete_key(int key)
386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 TlsFree(key);
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000388}
389
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000390int
391PyThread_set_key_value(int key, void *value)
392{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900393 BOOL ok = TlsSetValue(key, value);
394 return ok ? 0 : -1;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000395}
396
397void *
398PyThread_get_key_value(int key)
399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 /* because TLS is used in the Py_END_ALLOW_THREAD macro,
401 * it is necessary to preserve the windows error state, because
402 * it is assumed to be preserved across the call to the macro.
403 * Ideally, the macro should be fixed, but it is simpler to
404 * do it here.
405 */
406 DWORD error = GetLastError();
407 void *result = TlsGetValue(key);
408 SetLastError(error);
409 return result;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000410}
411
412void
413PyThread_delete_key_value(int key)
414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 /* NULL is used as "key missing", and it is also the default
416 * given by TlsGetValue() if nothing has been set yet.
417 */
418 TlsSetValue(key, NULL);
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000419}
420
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900421
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000422/* reinitialization of TLS is not necessary after fork when using
423 * the native TLS functions. And forking isn't supported on Windows either.
424 */
425void
426PyThread_ReInitTLS(void)
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900427{
428}
429
430
431/* Thread Specific Storage (TSS) API
432
433 Platform-specific components of TSS API implementation.
434*/
435
436int
437PyThread_tss_create(Py_tss_t *key)
438{
439 assert(key != NULL);
440 /* If the key has been created, function is silently skipped. */
441 if (key->_is_initialized) {
442 return 0;
443 }
444
445 DWORD result = TlsAlloc();
446 if (result == TLS_OUT_OF_INDEXES) {
447 return -1;
448 }
449 /* In Windows, platform-specific key type is DWORD. */
450 key->_key = result;
451 key->_is_initialized = 1;
452 return 0;
453}
454
455void
456PyThread_tss_delete(Py_tss_t *key)
457{
458 assert(key != NULL);
459 /* If the key has not been created, function is silently skipped. */
460 if (!key->_is_initialized) {
461 return;
462 }
463
464 TlsFree(key->_key);
465 key->_key = TLS_OUT_OF_INDEXES;
466 key->_is_initialized = 0;
467}
468
469int
470PyThread_tss_set(Py_tss_t *key, void *value)
471{
472 assert(key != NULL);
473 BOOL ok = TlsSetValue(key->_key, value);
474 return ok ? 0 : -1;
475}
476
477void *
478PyThread_tss_get(Py_tss_t *key)
479{
480 assert(key != NULL);
481 /* because TSS is used in the Py_END_ALLOW_THREAD macro,
482 * it is necessary to preserve the windows error state, because
483 * it is assumed to be preserved across the call to the macro.
484 * Ideally, the macro should be fixed, but it is simpler to
485 * do it here.
486 */
487 DWORD error = GetLastError();
488 void *result = TlsGetValue(key->_key);
489 SetLastError(error);
490 return result;
491}