blob: 05b982d32dc52602ffc9616b78018e1850d438ba [file] [log] [blame]
Victor Stinner4a3fe082020-04-14 14:26:24 +02001#include "pycore_interp.h" // _PyInterpreterState.pythread_stacksize
Guido van Rossumc3f82b61995-01-17 16:29:31 +00002
3/* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */
Guido van Rossum706262b2000-05-04 18:47:15 +00004/* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */
Tim Peterse64ef932002-02-28 21:34:34 +00005/* Eliminated some memory leaks, gsw@agere.com */
Guido van Rossumc3f82b61995-01-17 16:29:31 +00006
Guido van Rossum49b12261997-08-14 20:12:58 +00007#include <windows.h>
8#include <limits.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009#ifdef HAVE_PROCESS_H
Guido van Rossum49b12261997-08-14 20:12:58 +000010#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000011#endif
Guido van Rossumc3f82b61995-01-17 16:29:31 +000012
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000013/* options */
14#ifndef _PY_USE_CV_LOCKS
15#define _PY_USE_CV_LOCKS 1 /* use locks based on cond vars */
16#endif
17
18/* Now, define a non-recursive mutex using either condition variables
19 * and critical sections (fast) or using operating system mutexes
20 * (slow)
21 */
22
23#if _PY_USE_CV_LOCKS
24
25#include "condvar.h"
26
27typedef struct _NRMUTEX
28{
29 PyMUTEX_T cs;
30 PyCOND_T cv;
31 int locked;
32} NRMUTEX;
33typedef NRMUTEX *PNRMUTEX;
34
35PNRMUTEX
36AllocNonRecursiveMutex()
37{
Victor Stinner80aa5652013-07-07 17:17:59 +020038 PNRMUTEX m = (PNRMUTEX)PyMem_RawMalloc(sizeof(NRMUTEX));
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000039 if (!m)
40 return NULL;
41 if (PyCOND_INIT(&m->cv))
42 goto fail;
43 if (PyMUTEX_INIT(&m->cs)) {
44 PyCOND_FINI(&m->cv);
45 goto fail;
46 }
47 m->locked = 0;
48 return m;
49fail:
Victor Stinner80aa5652013-07-07 17:17:59 +020050 PyMem_RawFree(m);
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000051 return NULL;
52}
53
54VOID
55FreeNonRecursiveMutex(PNRMUTEX mutex)
56{
57 if (mutex) {
58 PyCOND_FINI(&mutex->cv);
59 PyMUTEX_FINI(&mutex->cs);
Victor Stinner80aa5652013-07-07 17:17:59 +020060 PyMem_RawFree(mutex);
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000061 }
62}
63
64DWORD
65EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
66{
67 DWORD result = WAIT_OBJECT_0;
68 if (PyMUTEX_LOCK(&mutex->cs))
69 return WAIT_FAILED;
70 if (milliseconds == INFINITE) {
71 while (mutex->locked) {
72 if (PyCOND_WAIT(&mutex->cv, &mutex->cs)) {
73 result = WAIT_FAILED;
74 break;
75 }
76 }
77 } else if (milliseconds != 0) {
78 /* wait at least until the target */
bobince64838ce2020-03-11 23:39:02 +000079 ULONGLONG now, target = GetTickCount64() + milliseconds;
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000080 while (mutex->locked) {
Benjamin Petersonaf580df2016-09-06 10:46:49 -070081 if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, (long long)milliseconds*1000) < 0) {
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000082 result = WAIT_FAILED;
83 break;
84 }
bobince64838ce2020-03-11 23:39:02 +000085 now = GetTickCount64();
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000086 if (target <= now)
87 break;
bobince64838ce2020-03-11 23:39:02 +000088 milliseconds = (DWORD)(target-now);
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000089 }
90 }
91 if (!mutex->locked) {
92 mutex->locked = 1;
93 result = WAIT_OBJECT_0;
94 } else if (result == WAIT_OBJECT_0)
95 result = WAIT_TIMEOUT;
96 /* else, it is WAIT_FAILED */
97 PyMUTEX_UNLOCK(&mutex->cs); /* must ignore result here */
98 return result;
99}
100
101BOOL
102LeaveNonRecursiveMutex(PNRMUTEX mutex)
103{
104 BOOL result;
105 if (PyMUTEX_LOCK(&mutex->cs))
106 return FALSE;
107 mutex->locked = 0;
native-api05e92212019-02-02 19:22:55 +0300108 /* condvar APIs return 0 on success. We need to return TRUE on success. */
109 result = !PyCOND_SIGNAL(&mutex->cv);
110 PyMUTEX_UNLOCK(&mutex->cs);
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +0000111 return result;
Victor Stinner80aa5652013-07-07 17:17:59 +0200112}
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +0000113
114#else /* if ! _PY_USE_CV_LOCKS */
115
116/* NR-locks based on a kernel mutex */
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200117#define PNRMUTEX HANDLE
Guido van Rossum706262b2000-05-04 18:47:15 +0000118
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200119PNRMUTEX
120AllocNonRecursiveMutex()
Guido van Rossum706262b2000-05-04 18:47:15 +0000121{
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200122 return CreateSemaphore(NULL, 1, 1, NULL);
Guido van Rossum706262b2000-05-04 18:47:15 +0000123}
124
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000125VOID
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200126FreeNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +0000127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 /* No in-use check */
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200129 CloseHandle(mutex);
Guido van Rossum706262b2000-05-04 18:47:15 +0000130}
131
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000132DWORD
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000133EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
Guido van Rossum706262b2000-05-04 18:47:15 +0000134{
Martin v. Löwisb26a9b12013-01-25 14:25:48 +0100135 return WaitForSingleObjectEx(mutex, milliseconds, FALSE);
Guido van Rossum706262b2000-05-04 18:47:15 +0000136}
137
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000138BOOL
139LeaveNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +0000140{
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200141 return ReleaseSemaphore(mutex, 1, NULL);
Guido van Rossum706262b2000-05-04 18:47:15 +0000142}
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +0000143#endif /* _PY_USE_CV_LOCKS */
Guido van Rossum706262b2000-05-04 18:47:15 +0000144
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200145unsigned long PyThread_get_thread_ident(void);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000146
Jake Teslerb121f632019-05-22 08:43:17 -0700147#ifdef PY_HAVE_THREAD_NATIVE_ID
148unsigned long PyThread_get_thread_native_id(void);
149#endif
150
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000151/*
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000152 * Initialization of the C package, should not be needed.
153 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000154static void
155PyThread__init_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000156{
157}
158
159/*
160 * Thread support.
161 */
Guido van Rossum3c288632001-10-16 21:13:49 +0000162
163typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 void (*func)(void*);
165 void *arg;
Guido van Rossum3c288632001-10-16 21:13:49 +0000166} callobj;
167
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000168/* thunker to call adapt between the function type used by the system's
169thread start function and the internally used one. */
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000170static unsigned __stdcall
Guido van Rossum3c288632001-10-16 21:13:49 +0000171bootstrap(void *call)
172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 callobj *obj = (callobj*)call;
174 void (*func)(void*) = obj->func;
175 void *arg = obj->arg;
176 HeapFree(GetProcessHeap(), 0, obj);
177 func(arg);
178 return 0;
Guido van Rossum3c288632001-10-16 21:13:49 +0000179}
180
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200181unsigned long
Tim Peters2e7e7df2003-07-04 04:40:45 +0000182PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 HANDLE hThread;
185 unsigned threadID;
186 callobj *obj;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000187
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200188 dprintf(("%lu: PyThread_start_new_thread called\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 PyThread_get_thread_ident()));
190 if (!initialized)
191 PyThread_init_thread();
192
193 obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
194 if (!obj)
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200195 return PYTHREAD_INVALID_THREAD_ID;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 obj->func = func;
197 obj->arg = arg;
Victor Stinner50b48572018-11-01 01:51:40 +0100198 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600199 size_t stacksize = tstate ? tstate->interp->pythread_stacksize : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 hThread = (HANDLE)_beginthreadex(0,
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600201 Py_SAFE_DOWNCAST(stacksize, Py_ssize_t, unsigned int),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 bootstrap, obj,
203 0, &threadID);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (hThread == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 /* I've seen errno == EAGAIN here, which means "there are
206 * too many threads".
207 */
208 int e = errno;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200209 dprintf(("%lu: PyThread_start_new_thread failed, errno %d\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 PyThread_get_thread_ident(), e));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 threadID = (unsigned)-1;
212 HeapFree(GetProcessHeap(), 0, obj);
213 }
214 else {
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200215 dprintf(("%lu: PyThread_start_new_thread succeeded: %p\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 PyThread_get_thread_ident(), (void*)hThread));
217 CloseHandle(hThread);
218 }
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200219 return threadID;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000220}
221
222/*
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300223 * Return the thread Id instead of a handle. The Id is said to uniquely identify the
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000224 * thread in the system
225 */
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200226unsigned long
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000227PyThread_get_thread_ident(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (!initialized)
230 PyThread_init_thread();
Guido van Rossum706262b2000-05-04 18:47:15 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 return GetCurrentThreadId();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000233}
234
Jake Teslerb121f632019-05-22 08:43:17 -0700235#ifdef PY_HAVE_THREAD_NATIVE_ID
236/*
237 * Return the native Thread ID (TID) of the calling thread.
238 * The native ID of a thread is valid and guaranteed to be unique system-wide
239 * from the time the thread is created until the thread has been terminated.
240 */
241unsigned long
242PyThread_get_thread_native_id(void)
243{
244 if (!initialized) {
245 PyThread_init_thread();
246 }
247
248 DWORD native_id;
249 native_id = GetCurrentThreadId();
250 return (unsigned long) native_id;
251}
252#endif
253
Victor Stinnerc664b342019-05-04 11:48:05 -0400254void _Py_NO_RETURN
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000255PyThread_exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000256{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200257 dprintf(("%lu: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 if (!initialized)
259 exit(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 _endthreadex(0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000261}
262
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000263/*
hui shang60278022018-01-18 07:21:01 +0800264 * Lock support. It has to be implemented as semaphores.
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000265 * I [Dag] tried to implement it with mutex but I could find a way to
266 * tell whether a thread already own the lock or not.
267 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000268PyThread_type_lock
269PyThread_allocate_lock(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 PNRMUTEX aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 dprintf(("PyThread_allocate_lock called\n"));
274 if (!initialized)
275 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 aLock = AllocNonRecursiveMutex() ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000278
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200279 dprintf(("%lu: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 return (PyThread_type_lock) aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000282}
283
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000284void
285PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000286{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200287 dprintf(("%lu: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 FreeNonRecursiveMutex(aLock) ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000290}
291
292/*
293 * Return 1 on success if the lock was acquired
294 *
295 * and 0 if the lock was not acquired. This means a 0 is returned
296 * if the lock has already been acquired by this thread!
297 */
Antoine Pitrou810023d2010-12-15 22:59:16 +0000298PyLockStatus
299PyThread_acquire_lock_timed(PyThread_type_lock aLock,
300 PY_TIMEOUT_T microseconds, int intr_flag)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000301{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000302 /* Fow now, intr_flag does nothing on Windows, and lock acquires are
303 * uninterruptible. */
304 PyLockStatus success;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 PY_TIMEOUT_T milliseconds;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 if (microseconds >= 0) {
308 milliseconds = microseconds / 1000;
309 if (microseconds % 1000 > 0)
310 ++milliseconds;
Victor Stinner850a18e2017-10-24 16:53:32 -0700311 if (milliseconds > PY_DWORD_MAX) {
312 Py_FatalError("Timeout larger than PY_TIMEOUT_MAX");
313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 }
Victor Stinner850a18e2017-10-24 16:53:32 -0700315 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 milliseconds = INFINITE;
Victor Stinner850a18e2017-10-24 16:53:32 -0700317 }
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000318
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200319 dprintf(("%lu: PyThread_acquire_lock_timed(%p, %lld) called\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 PyThread_get_thread_ident(), aLock, microseconds));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000321
Antoine Pitrou810023d2010-12-15 22:59:16 +0000322 if (aLock && EnterNonRecursiveMutex((PNRMUTEX)aLock,
323 (DWORD)milliseconds) == WAIT_OBJECT_0) {
324 success = PY_LOCK_ACQUIRED;
325 }
326 else {
327 success = PY_LOCK_FAILURE;
328 }
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000329
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200330 dprintf(("%lu: PyThread_acquire_lock(%p, %lld) -> %d\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 PyThread_get_thread_ident(), aLock, microseconds, success));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 return success;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000334}
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000335int
336PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
337{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000338 return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0);
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000339}
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000340
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000341void
342PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000343{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200344 dprintf(("%lu: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200347 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 +0000348}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000349
350/* minimum/maximum thread stack sizes supported */
Victor Stinner8c663fd2017-11-08 14:44:44 -0800351#define THREAD_MIN_STACKSIZE 0x8000 /* 32 KiB */
352#define THREAD_MAX_STACKSIZE 0x10000000 /* 256 MiB */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000353
354/* set the thread stack size.
355 * Return 0 if size is valid, -1 otherwise.
356 */
357static int
358_pythread_nt_set_stacksize(size_t size)
359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 /* set to default */
361 if (size == 0) {
Victor Stinner81a7be32020-04-14 15:14:01 +0200362 _PyInterpreterState_GET()->pythread_stacksize = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 return 0;
364 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 /* valid range? */
367 if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
Victor Stinner81a7be32020-04-14 15:14:01 +0200368 _PyInterpreterState_GET()->pythread_stacksize = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 return 0;
370 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000373}
374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375#define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000376
377
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900378/* Thread Local Storage (TLS) API
379
380 This API is DEPRECATED since Python 3.7. See PEP 539 for details.
381*/
382
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000383int
384PyThread_create_key(void)
385{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900386 DWORD result = TlsAlloc();
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000387 if (result == TLS_OUT_OF_INDEXES)
388 return -1;
389 return (int)result;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000390}
391
392void
393PyThread_delete_key(int key)
394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 TlsFree(key);
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000396}
397
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000398int
399PyThread_set_key_value(int key, void *value)
400{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900401 BOOL ok = TlsSetValue(key, value);
402 return ok ? 0 : -1;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000403}
404
405void *
406PyThread_get_key_value(int key)
407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 /* because TLS is used in the Py_END_ALLOW_THREAD macro,
409 * it is necessary to preserve the windows error state, because
410 * it is assumed to be preserved across the call to the macro.
411 * Ideally, the macro should be fixed, but it is simpler to
412 * do it here.
413 */
414 DWORD error = GetLastError();
415 void *result = TlsGetValue(key);
416 SetLastError(error);
417 return result;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000418}
419
420void
421PyThread_delete_key_value(int key)
422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 /* NULL is used as "key missing", and it is also the default
424 * given by TlsGetValue() if nothing has been set yet.
425 */
426 TlsSetValue(key, NULL);
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000427}
428
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900429
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000430/* reinitialization of TLS is not necessary after fork when using
431 * the native TLS functions. And forking isn't supported on Windows either.
432 */
433void
434PyThread_ReInitTLS(void)
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900435{
436}
437
438
439/* Thread Specific Storage (TSS) API
440
441 Platform-specific components of TSS API implementation.
442*/
443
444int
445PyThread_tss_create(Py_tss_t *key)
446{
447 assert(key != NULL);
448 /* If the key has been created, function is silently skipped. */
449 if (key->_is_initialized) {
450 return 0;
451 }
452
453 DWORD result = TlsAlloc();
454 if (result == TLS_OUT_OF_INDEXES) {
455 return -1;
456 }
457 /* In Windows, platform-specific key type is DWORD. */
458 key->_key = result;
459 key->_is_initialized = 1;
460 return 0;
461}
462
463void
464PyThread_tss_delete(Py_tss_t *key)
465{
466 assert(key != NULL);
467 /* If the key has not been created, function is silently skipped. */
468 if (!key->_is_initialized) {
469 return;
470 }
471
472 TlsFree(key->_key);
473 key->_key = TLS_OUT_OF_INDEXES;
474 key->_is_initialized = 0;
475}
476
477int
478PyThread_tss_set(Py_tss_t *key, void *value)
479{
480 assert(key != NULL);
481 BOOL ok = TlsSetValue(key->_key, value);
482 return ok ? 0 : -1;
483}
484
485void *
486PyThread_tss_get(Py_tss_t *key)
487{
488 assert(key != NULL);
489 /* because TSS is used in the Py_END_ALLOW_THREAD macro,
490 * it is necessary to preserve the windows error state, because
491 * it is assumed to be preserved across the call to the macro.
492 * Ideally, the macro should be fixed, but it is simpler to
493 * do it here.
494 */
495 DWORD error = GetLastError();
496 void *result = TlsGetValue(key->_key);
497 SetLastError(error);
498 return result;
499}