1 #include "pycore_interp.h"    // _PyInterpreterState.pythread_stacksize
2 
3 /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */
4 /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */
5 /* Eliminated some memory leaks, gsw@agere.com */
6 
7 #include <windows.h>
8 #include <limits.h>
9 #ifdef HAVE_PROCESS_H
10 #include <process.h>
11 #endif
12 
13 /* 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 
27 typedef struct _NRMUTEX
28 {
29     PyMUTEX_T cs;
30     PyCOND_T cv;
31     int locked;
32 } NRMUTEX;
33 typedef NRMUTEX *PNRMUTEX;
34 
35 PNRMUTEX
AllocNonRecursiveMutex()36 AllocNonRecursiveMutex()
37 {
38     PNRMUTEX m = (PNRMUTEX)PyMem_RawMalloc(sizeof(NRMUTEX));
39     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;
49 fail:
50     PyMem_RawFree(m);
51     return NULL;
52 }
53 
54 VOID
FreeNonRecursiveMutex(PNRMUTEX mutex)55 FreeNonRecursiveMutex(PNRMUTEX mutex)
56 {
57     if (mutex) {
58         PyCOND_FINI(&mutex->cv);
59         PyMUTEX_FINI(&mutex->cs);
60         PyMem_RawFree(mutex);
61     }
62 }
63 
64 DWORD
EnterNonRecursiveMutex(PNRMUTEX mutex,DWORD milliseconds)65 EnterNonRecursiveMutex(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 */
79         _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;
85         while (mutex->locked) {
86             _PyTime_t microseconds = _PyTime_AsMicroseconds(nanoseconds, _PyTime_ROUND_TIMEOUT);
87             if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, microseconds) < 0) {
88                 result = WAIT_FAILED;
89                 break;
90             }
91             now = _PyTime_GetPerfCounter();
92             if (target <= now)
93                 break;
94             nanoseconds = target - now;
95         }
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 
107 BOOL
LeaveNonRecursiveMutex(PNRMUTEX mutex)108 LeaveNonRecursiveMutex(PNRMUTEX mutex)
109 {
110     BOOL result;
111     if (PyMUTEX_LOCK(&mutex->cs))
112         return FALSE;
113     mutex->locked = 0;
114     /* condvar APIs return 0 on success. We need to return TRUE on success. */
115     result = !PyCOND_SIGNAL(&mutex->cv);
116     PyMUTEX_UNLOCK(&mutex->cs);
117     return result;
118 }
119 
120 #else /* if ! _PY_USE_CV_LOCKS */
121 
122 /* NR-locks based on a kernel mutex */
123 #define PNRMUTEX HANDLE
124 
125 PNRMUTEX
AllocNonRecursiveMutex()126 AllocNonRecursiveMutex()
127 {
128     return CreateSemaphore(NULL, 1, 1, NULL);
129 }
130 
131 VOID
FreeNonRecursiveMutex(PNRMUTEX mutex)132 FreeNonRecursiveMutex(PNRMUTEX mutex)
133 {
134     /* No in-use check */
135     CloseHandle(mutex);
136 }
137 
138 DWORD
EnterNonRecursiveMutex(PNRMUTEX mutex,DWORD milliseconds)139 EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
140 {
141     return WaitForSingleObjectEx(mutex, milliseconds, FALSE);
142 }
143 
144 BOOL
LeaveNonRecursiveMutex(PNRMUTEX mutex)145 LeaveNonRecursiveMutex(PNRMUTEX mutex)
146 {
147     return ReleaseSemaphore(mutex, 1, NULL);
148 }
149 #endif /* _PY_USE_CV_LOCKS */
150 
151 unsigned long PyThread_get_thread_ident(void);
152 
153 #ifdef PY_HAVE_THREAD_NATIVE_ID
154 unsigned long PyThread_get_thread_native_id(void);
155 #endif
156 
157 /*
158  * Initialization of the C package, should not be needed.
159  */
160 static void
PyThread__init_thread(void)161 PyThread__init_thread(void)
162 {
163 }
164 
165 /*
166  * Thread support.
167  */
168 
169 typedef struct {
170     void (*func)(void*);
171     void *arg;
172 } callobj;
173 
174 /* thunker to call adapt between the function type used by the system's
175 thread start function and the internally used one. */
176 static unsigned __stdcall
bootstrap(void * call)177 bootstrap(void *call)
178 {
179     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;
185 }
186 
187 unsigned long
PyThread_start_new_thread(void (* func)(void *),void * arg)188 PyThread_start_new_thread(void (*func)(void *), void *arg)
189 {
190     HANDLE hThread;
191     unsigned threadID;
192     callobj *obj;
193 
194     dprintf(("%lu: PyThread_start_new_thread called\n",
195              PyThread_get_thread_ident()));
196     if (!initialized)
197         PyThread_init_thread();
198 
199     obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
200     if (!obj)
201         return PYTHREAD_INVALID_THREAD_ID;
202     obj->func = func;
203     obj->arg = arg;
204     PyThreadState *tstate = _PyThreadState_GET();
205     size_t stacksize = tstate ? tstate->interp->pythread_stacksize : 0;
206     hThread = (HANDLE)_beginthreadex(0,
207                       Py_SAFE_DOWNCAST(stacksize, Py_ssize_t, unsigned int),
208                       bootstrap, obj,
209                       0, &threadID);
210     if (hThread == 0) {
211         /* I've seen errno == EAGAIN here, which means "there are
212          * too many threads".
213          */
214         int e = errno;
215         dprintf(("%lu: PyThread_start_new_thread failed, errno %d\n",
216                  PyThread_get_thread_ident(), e));
217         threadID = (unsigned)-1;
218         HeapFree(GetProcessHeap(), 0, obj);
219     }
220     else {
221         dprintf(("%lu: PyThread_start_new_thread succeeded: %p\n",
222                  PyThread_get_thread_ident(), (void*)hThread));
223         CloseHandle(hThread);
224     }
225     return threadID;
226 }
227 
228 /*
229  * Return the thread Id instead of a handle. The Id is said to uniquely identify the
230  * thread in the system
231  */
232 unsigned long
PyThread_get_thread_ident(void)233 PyThread_get_thread_ident(void)
234 {
235     if (!initialized)
236         PyThread_init_thread();
237 
238     return GetCurrentThreadId();
239 }
240 
241 #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  */
247 unsigned long
PyThread_get_thread_native_id(void)248 PyThread_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 
260 void _Py_NO_RETURN
PyThread_exit_thread(void)261 PyThread_exit_thread(void)
262 {
263     dprintf(("%lu: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
264     if (!initialized)
265         exit(0);
266     _endthreadex(0);
267 }
268 
269 /*
270  * Lock support. It has to be implemented as semaphores.
271  * 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  */
274 PyThread_type_lock
PyThread_allocate_lock(void)275 PyThread_allocate_lock(void)
276 {
277     PNRMUTEX aLock;
278 
279     dprintf(("PyThread_allocate_lock called\n"));
280     if (!initialized)
281         PyThread_init_thread();
282 
283     aLock = AllocNonRecursiveMutex() ;
284 
285     dprintf(("%lu: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
286 
287     return (PyThread_type_lock) aLock;
288 }
289 
290 void
PyThread_free_lock(PyThread_type_lock aLock)291 PyThread_free_lock(PyThread_type_lock aLock)
292 {
293     dprintf(("%lu: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
294 
295     FreeNonRecursiveMutex(aLock) ;
296 }
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  */
304 PyLockStatus
PyThread_acquire_lock_timed(PyThread_type_lock aLock,PY_TIMEOUT_T microseconds,int intr_flag)305 PyThread_acquire_lock_timed(PyThread_type_lock aLock,
306                             PY_TIMEOUT_T microseconds, int intr_flag)
307 {
308     /* Fow now, intr_flag does nothing on Windows, and lock acquires are
309      * uninterruptible.  */
310     PyLockStatus success;
311     PY_TIMEOUT_T milliseconds;
312 
313     if (microseconds >= 0) {
314         milliseconds = microseconds / 1000;
315         if (microseconds % 1000 > 0)
316             ++milliseconds;
317         if (milliseconds > PY_DWORD_MAX) {
318             Py_FatalError("Timeout larger than PY_TIMEOUT_MAX");
319         }
320     }
321     else {
322         milliseconds = INFINITE;
323     }
324 
325     dprintf(("%lu: PyThread_acquire_lock_timed(%p, %lld) called\n",
326              PyThread_get_thread_ident(), aLock, microseconds));
327 
328     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     }
335 
336     dprintf(("%lu: PyThread_acquire_lock(%p, %lld) -> %d\n",
337              PyThread_get_thread_ident(), aLock, microseconds, success));
338 
339     return success;
340 }
341 int
PyThread_acquire_lock(PyThread_type_lock aLock,int waitflag)342 PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
343 {
344     return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0);
345 }
346 
347 void
PyThread_release_lock(PyThread_type_lock aLock)348 PyThread_release_lock(PyThread_type_lock aLock)
349 {
350     dprintf(("%lu: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
351 
352     if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
353         dprintf(("%lu: Could not PyThread_release_lock(%p) error: %ld\n", PyThread_get_thread_ident(), aLock, GetLastError()));
354 }
355 
356 /* minimum/maximum thread stack sizes supported */
357 #define THREAD_MIN_STACKSIZE    0x8000          /* 32 KiB */
358 #define THREAD_MAX_STACKSIZE    0x10000000      /* 256 MiB */
359 
360 /* set the thread stack size.
361  * Return 0 if size is valid, -1 otherwise.
362  */
363 static int
_pythread_nt_set_stacksize(size_t size)364 _pythread_nt_set_stacksize(size_t size)
365 {
366     /* set to default */
367     if (size == 0) {
368         _PyInterpreterState_GET()->pythread_stacksize = 0;
369         return 0;
370     }
371 
372     /* valid range? */
373     if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
374         _PyInterpreterState_GET()->pythread_stacksize = size;
375         return 0;
376     }
377 
378     return -1;
379 }
380 
381 #define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)
382 
383 
384 /* Thread Local Storage (TLS) API
385 
386    This API is DEPRECATED since Python 3.7.  See PEP 539 for details.
387 */
388 
389 int
PyThread_create_key(void)390 PyThread_create_key(void)
391 {
392     DWORD result = TlsAlloc();
393     if (result == TLS_OUT_OF_INDEXES)
394         return -1;
395     return (int)result;
396 }
397 
398 void
PyThread_delete_key(int key)399 PyThread_delete_key(int key)
400 {
401     TlsFree(key);
402 }
403 
404 int
PyThread_set_key_value(int key,void * value)405 PyThread_set_key_value(int key, void *value)
406 {
407     BOOL ok = TlsSetValue(key, value);
408     return ok ? 0 : -1;
409 }
410 
411 void *
PyThread_get_key_value(int key)412 PyThread_get_key_value(int key)
413 {
414     /* 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;
424 }
425 
426 void
PyThread_delete_key_value(int key)427 PyThread_delete_key_value(int key)
428 {
429     /* 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);
433 }
434 
435 
436 /* reinitialization of TLS is not necessary after fork when using
437  * the native TLS functions.  And forking isn't supported on Windows either.
438  */
439 void
PyThread_ReInitTLS(void)440 PyThread_ReInitTLS(void)
441 {
442 }
443 
444 
445 /* Thread Specific Storage (TSS) API
446 
447    Platform-specific components of TSS API implementation.
448 */
449 
450 int
PyThread_tss_create(Py_tss_t * key)451 PyThread_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 
469 void
PyThread_tss_delete(Py_tss_t * key)470 PyThread_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 
483 int
PyThread_tss_set(Py_tss_t * key,void * value)484 PyThread_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 
491 void *
PyThread_tss_get(Py_tss_t * key)492 PyThread_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 }
506