blob: 0ce5e94f89bf72dfde09916088e6682ed9ad1e83 [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 */
Miss Islington (bot)8673b772021-06-20 13:12:27 -070079 _PyTime_t now = _PyTime_GetPerfCounter();
80 if (now <= 0) {
81 Py_FatalError("_PyTime_GetPerfCounter() == 0");
82 }
83 _PyTime_t nanoseconds = _PyTime_FromNanoseconds((_PyTime_t)milliseconds * 1000000);
84 _PyTime_t target = now + nanoseconds;
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000085 while (mutex->locked) {
Miss Islington (bot)8673b772021-06-20 13:12:27 -070086 _PyTime_t microseconds = _PyTime_AsMicroseconds(nanoseconds, _PyTime_ROUND_TIMEOUT);
87 if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, microseconds) < 0) {
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000088 result = WAIT_FAILED;
89 break;
90 }
Miss Islington (bot)8673b772021-06-20 13:12:27 -070091 now = _PyTime_GetPerfCounter();
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000092 if (target <= now)
93 break;
Miss Islington (bot)8673b772021-06-20 13:12:27 -070094 nanoseconds = target - now;
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +000095 }
96 }
97 if (!mutex->locked) {
98 mutex->locked = 1;
99 result = WAIT_OBJECT_0;
100 } else if (result == WAIT_OBJECT_0)
101 result = WAIT_TIMEOUT;
102 /* else, it is WAIT_FAILED */
103 PyMUTEX_UNLOCK(&mutex->cs); /* must ignore result here */
104 return result;
105}
106
107BOOL
108LeaveNonRecursiveMutex(PNRMUTEX mutex)
109{
110 BOOL result;
111 if (PyMUTEX_LOCK(&mutex->cs))
112 return FALSE;
113 mutex->locked = 0;
native-api05e92212019-02-02 19:22:55 +0300114 /* condvar APIs return 0 on success. We need to return TRUE on success. */
115 result = !PyCOND_SIGNAL(&mutex->cv);
116 PyMUTEX_UNLOCK(&mutex->cs);
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +0000117 return result;
Victor Stinner80aa5652013-07-07 17:17:59 +0200118}
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +0000119
120#else /* if ! _PY_USE_CV_LOCKS */
121
122/* NR-locks based on a kernel mutex */
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200123#define PNRMUTEX HANDLE
Guido van Rossum706262b2000-05-04 18:47:15 +0000124
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200125PNRMUTEX
126AllocNonRecursiveMutex()
Guido van Rossum706262b2000-05-04 18:47:15 +0000127{
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200128 return CreateSemaphore(NULL, 1, 1, NULL);
Guido van Rossum706262b2000-05-04 18:47:15 +0000129}
130
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000131VOID
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200132FreeNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +0000133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 /* No in-use check */
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200135 CloseHandle(mutex);
Guido van Rossum706262b2000-05-04 18:47:15 +0000136}
137
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000138DWORD
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000139EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
Guido van Rossum706262b2000-05-04 18:47:15 +0000140{
Martin v. Löwisb26a9b12013-01-25 14:25:48 +0100141 return WaitForSingleObjectEx(mutex, milliseconds, FALSE);
Guido van Rossum706262b2000-05-04 18:47:15 +0000142}
143
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000144BOOL
145LeaveNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +0000146{
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200147 return ReleaseSemaphore(mutex, 1, NULL);
Guido van Rossum706262b2000-05-04 18:47:15 +0000148}
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +0000149#endif /* _PY_USE_CV_LOCKS */
Guido van Rossum706262b2000-05-04 18:47:15 +0000150
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200151unsigned long PyThread_get_thread_ident(void);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000152
Jake Teslerb121f632019-05-22 08:43:17 -0700153#ifdef PY_HAVE_THREAD_NATIVE_ID
154unsigned long PyThread_get_thread_native_id(void);
155#endif
156
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000157/*
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000158 * Initialization of the C package, should not be needed.
159 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000160static void
161PyThread__init_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000162{
163}
164
165/*
166 * Thread support.
167 */
Guido van Rossum3c288632001-10-16 21:13:49 +0000168
169typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 void (*func)(void*);
171 void *arg;
Guido van Rossum3c288632001-10-16 21:13:49 +0000172} callobj;
173
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000174/* thunker to call adapt between the function type used by the system's
175thread start function and the internally used one. */
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000176static unsigned __stdcall
Guido van Rossum3c288632001-10-16 21:13:49 +0000177bootstrap(void *call)
178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 callobj *obj = (callobj*)call;
180 void (*func)(void*) = obj->func;
181 void *arg = obj->arg;
182 HeapFree(GetProcessHeap(), 0, obj);
183 func(arg);
184 return 0;
Guido van Rossum3c288632001-10-16 21:13:49 +0000185}
186
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200187unsigned long
Tim Peters2e7e7df2003-07-04 04:40:45 +0000188PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 HANDLE hThread;
191 unsigned threadID;
192 callobj *obj;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000193
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200194 dprintf(("%lu: PyThread_start_new_thread called\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 PyThread_get_thread_ident()));
196 if (!initialized)
197 PyThread_init_thread();
198
199 obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
200 if (!obj)
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200201 return PYTHREAD_INVALID_THREAD_ID;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 obj->func = func;
203 obj->arg = arg;
Victor Stinner50b48572018-11-01 01:51:40 +0100204 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600205 size_t stacksize = tstate ? tstate->interp->pythread_stacksize : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 hThread = (HANDLE)_beginthreadex(0,
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600207 Py_SAFE_DOWNCAST(stacksize, Py_ssize_t, unsigned int),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 bootstrap, obj,
209 0, &threadID);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (hThread == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 /* I've seen errno == EAGAIN here, which means "there are
212 * too many threads".
213 */
214 int e = errno;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200215 dprintf(("%lu: PyThread_start_new_thread failed, errno %d\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 PyThread_get_thread_ident(), e));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 threadID = (unsigned)-1;
218 HeapFree(GetProcessHeap(), 0, obj);
219 }
220 else {
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200221 dprintf(("%lu: PyThread_start_new_thread succeeded: %p\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 PyThread_get_thread_ident(), (void*)hThread));
223 CloseHandle(hThread);
224 }
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200225 return threadID;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000226}
227
228/*
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300229 * Return the thread Id instead of a handle. The Id is said to uniquely identify the
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000230 * thread in the system
231 */
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200232unsigned long
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000233PyThread_get_thread_ident(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 if (!initialized)
236 PyThread_init_thread();
Guido van Rossum706262b2000-05-04 18:47:15 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 return GetCurrentThreadId();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000239}
240
Jake Teslerb121f632019-05-22 08:43:17 -0700241#ifdef PY_HAVE_THREAD_NATIVE_ID
242/*
243 * Return the native Thread ID (TID) of the calling thread.
244 * The native ID of a thread is valid and guaranteed to be unique system-wide
245 * from the time the thread is created until the thread has been terminated.
246 */
247unsigned long
248PyThread_get_thread_native_id(void)
249{
250 if (!initialized) {
251 PyThread_init_thread();
252 }
253
254 DWORD native_id;
255 native_id = GetCurrentThreadId();
256 return (unsigned long) native_id;
257}
258#endif
259
Victor Stinnerc664b342019-05-04 11:48:05 -0400260void _Py_NO_RETURN
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000261PyThread_exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000262{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200263 dprintf(("%lu: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 if (!initialized)
265 exit(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 _endthreadex(0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000267}
268
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000269/*
hui shang60278022018-01-18 07:21:01 +0800270 * Lock support. It has to be implemented as semaphores.
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000271 * I [Dag] tried to implement it with mutex but I could find a way to
272 * tell whether a thread already own the lock or not.
273 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000274PyThread_type_lock
275PyThread_allocate_lock(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 PNRMUTEX aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 dprintf(("PyThread_allocate_lock called\n"));
280 if (!initialized)
281 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 aLock = AllocNonRecursiveMutex() ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000284
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200285 dprintf(("%lu: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 return (PyThread_type_lock) aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000288}
289
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000290void
291PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000292{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200293 dprintf(("%lu: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 FreeNonRecursiveMutex(aLock) ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000296}
297
298/*
299 * Return 1 on success if the lock was acquired
300 *
301 * and 0 if the lock was not acquired. This means a 0 is returned
302 * if the lock has already been acquired by this thread!
303 */
Antoine Pitrou810023d2010-12-15 22:59:16 +0000304PyLockStatus
305PyThread_acquire_lock_timed(PyThread_type_lock aLock,
306 PY_TIMEOUT_T microseconds, int intr_flag)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000307{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000308 /* Fow now, intr_flag does nothing on Windows, and lock acquires are
309 * uninterruptible. */
310 PyLockStatus success;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 PY_TIMEOUT_T milliseconds;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 if (microseconds >= 0) {
314 milliseconds = microseconds / 1000;
315 if (microseconds % 1000 > 0)
316 ++milliseconds;
Victor Stinner850a18e2017-10-24 16:53:32 -0700317 if (milliseconds > PY_DWORD_MAX) {
318 Py_FatalError("Timeout larger than PY_TIMEOUT_MAX");
319 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 }
Victor Stinner850a18e2017-10-24 16:53:32 -0700321 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 milliseconds = INFINITE;
Victor Stinner850a18e2017-10-24 16:53:32 -0700323 }
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000324
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200325 dprintf(("%lu: PyThread_acquire_lock_timed(%p, %lld) called\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 PyThread_get_thread_ident(), aLock, microseconds));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000327
Antoine Pitrou810023d2010-12-15 22:59:16 +0000328 if (aLock && EnterNonRecursiveMutex((PNRMUTEX)aLock,
329 (DWORD)milliseconds) == WAIT_OBJECT_0) {
330 success = PY_LOCK_ACQUIRED;
331 }
332 else {
333 success = PY_LOCK_FAILURE;
334 }
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000335
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200336 dprintf(("%lu: PyThread_acquire_lock(%p, %lld) -> %d\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 PyThread_get_thread_ident(), aLock, microseconds, success));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 return success;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000340}
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000341int
342PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
343{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000344 return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0);
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000345}
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000346
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000347void
348PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000349{
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200350 dprintf(("%lu: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200353 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 +0000354}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000355
356/* minimum/maximum thread stack sizes supported */
Victor Stinner8c663fd2017-11-08 14:44:44 -0800357#define THREAD_MIN_STACKSIZE 0x8000 /* 32 KiB */
358#define THREAD_MAX_STACKSIZE 0x10000000 /* 256 MiB */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000359
360/* set the thread stack size.
361 * Return 0 if size is valid, -1 otherwise.
362 */
363static int
364_pythread_nt_set_stacksize(size_t size)
365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 /* set to default */
367 if (size == 0) {
Victor Stinner81a7be32020-04-14 15:14:01 +0200368 _PyInterpreterState_GET()->pythread_stacksize = 0;
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 /* valid range? */
373 if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
Victor Stinner81a7be32020-04-14 15:14:01 +0200374 _PyInterpreterState_GET()->pythread_stacksize = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 return 0;
376 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000379}
380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381#define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000382
383
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900384/* Thread Local Storage (TLS) API
385
386 This API is DEPRECATED since Python 3.7. See PEP 539 for details.
387*/
388
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000389int
390PyThread_create_key(void)
391{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900392 DWORD result = TlsAlloc();
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000393 if (result == TLS_OUT_OF_INDEXES)
394 return -1;
395 return (int)result;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000396}
397
398void
399PyThread_delete_key(int key)
400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 TlsFree(key);
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000402}
403
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000404int
405PyThread_set_key_value(int key, void *value)
406{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900407 BOOL ok = TlsSetValue(key, value);
408 return ok ? 0 : -1;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000409}
410
411void *
412PyThread_get_key_value(int key)
413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 /* because TLS is used in the Py_END_ALLOW_THREAD macro,
415 * it is necessary to preserve the windows error state, because
416 * it is assumed to be preserved across the call to the macro.
417 * Ideally, the macro should be fixed, but it is simpler to
418 * do it here.
419 */
420 DWORD error = GetLastError();
421 void *result = TlsGetValue(key);
422 SetLastError(error);
423 return result;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000424}
425
426void
427PyThread_delete_key_value(int key)
428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 /* NULL is used as "key missing", and it is also the default
430 * given by TlsGetValue() if nothing has been set yet.
431 */
432 TlsSetValue(key, NULL);
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000433}
434
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900435
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000436/* reinitialization of TLS is not necessary after fork when using
437 * the native TLS functions. And forking isn't supported on Windows either.
438 */
439void
440PyThread_ReInitTLS(void)
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900441{
442}
443
444
445/* Thread Specific Storage (TSS) API
446
447 Platform-specific components of TSS API implementation.
448*/
449
450int
451PyThread_tss_create(Py_tss_t *key)
452{
453 assert(key != NULL);
454 /* If the key has been created, function is silently skipped. */
455 if (key->_is_initialized) {
456 return 0;
457 }
458
459 DWORD result = TlsAlloc();
460 if (result == TLS_OUT_OF_INDEXES) {
461 return -1;
462 }
463 /* In Windows, platform-specific key type is DWORD. */
464 key->_key = result;
465 key->_is_initialized = 1;
466 return 0;
467}
468
469void
470PyThread_tss_delete(Py_tss_t *key)
471{
472 assert(key != NULL);
473 /* If the key has not been created, function is silently skipped. */
474 if (!key->_is_initialized) {
475 return;
476 }
477
478 TlsFree(key->_key);
479 key->_key = TLS_OUT_OF_INDEXES;
480 key->_is_initialized = 0;
481}
482
483int
484PyThread_tss_set(Py_tss_t *key, void *value)
485{
486 assert(key != NULL);
487 BOOL ok = TlsSetValue(key->_key, value);
488 return ok ? 0 : -1;
489}
490
491void *
492PyThread_tss_get(Py_tss_t *key)
493{
494 assert(key != NULL);
495 /* because TSS is used in the Py_END_ALLOW_THREAD macro,
496 * it is necessary to preserve the windows error state, because
497 * it is assumed to be preserved across the call to the macro.
498 * Ideally, the macro should be fixed, but it is simpler to
499 * do it here.
500 */
501 DWORD error = GetLastError();
502 void *result = TlsGetValue(key->_key);
503 SetLastError(error);
504 return result;
505}