blob: 74a6ee802980e4d583689c14194ae1343960b2b6 [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;
107 result = PyCOND_SIGNAL(&mutex->cv);
108 result &= PyMUTEX_UNLOCK(&mutex->cs);
109 return result;
Victor Stinner80aa5652013-07-07 17:17:59 +0200110}
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +0000111
112#else /* if ! _PY_USE_CV_LOCKS */
113
114/* NR-locks based on a kernel mutex */
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200115#define PNRMUTEX HANDLE
Guido van Rossum706262b2000-05-04 18:47:15 +0000116
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200117PNRMUTEX
118AllocNonRecursiveMutex()
Guido van Rossum706262b2000-05-04 18:47:15 +0000119{
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200120 return CreateSemaphore(NULL, 1, 1, NULL);
Guido van Rossum706262b2000-05-04 18:47:15 +0000121}
122
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000123VOID
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200124FreeNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 /* No in-use check */
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200127 CloseHandle(mutex);
Guido van Rossum706262b2000-05-04 18:47:15 +0000128}
129
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000130DWORD
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000131EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
Guido van Rossum706262b2000-05-04 18:47:15 +0000132{
Martin v. Löwisb26a9b12013-01-25 14:25:48 +0100133 return WaitForSingleObjectEx(mutex, milliseconds, FALSE);
Guido van Rossum706262b2000-05-04 18:47:15 +0000134}
135
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000136BOOL
137LeaveNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +0000138{
Antoine Pitrou7899acf2011-03-31 01:00:32 +0200139 return ReleaseSemaphore(mutex, 1, NULL);
Guido van Rossum706262b2000-05-04 18:47:15 +0000140}
Kristján Valur Jónssone75ff352012-06-18 20:30:44 +0000141#endif /* _PY_USE_CV_LOCKS */
Guido van Rossum706262b2000-05-04 18:47:15 +0000142
Guido van Rossum65d5b571998-12-21 19:32:43 +0000143long PyThread_get_thread_ident(void);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000144
145/*
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000146 * Initialization of the C package, should not be needed.
147 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000148static void
149PyThread__init_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000150{
151}
152
153/*
154 * Thread support.
155 */
Guido van Rossum3c288632001-10-16 21:13:49 +0000156
157typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 void (*func)(void*);
159 void *arg;
Guido van Rossum3c288632001-10-16 21:13:49 +0000160} callobj;
161
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000162/* thunker to call adapt between the function type used by the system's
163thread start function and the internally used one. */
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000164static unsigned __stdcall
Guido van Rossum3c288632001-10-16 21:13:49 +0000165bootstrap(void *call)
166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 callobj *obj = (callobj*)call;
168 void (*func)(void*) = obj->func;
169 void *arg = obj->arg;
170 HeapFree(GetProcessHeap(), 0, obj);
171 func(arg);
172 return 0;
Guido van Rossum3c288632001-10-16 21:13:49 +0000173}
174
Tim Peters2e7e7df2003-07-04 04:40:45 +0000175long
176PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 HANDLE hThread;
179 unsigned threadID;
180 callobj *obj;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 dprintf(("%ld: PyThread_start_new_thread called\n",
183 PyThread_get_thread_ident()));
184 if (!initialized)
185 PyThread_init_thread();
186
187 obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
188 if (!obj)
189 return -1;
190 obj->func = func;
191 obj->arg = arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 hThread = (HANDLE)_beginthreadex(0,
193 Py_SAFE_DOWNCAST(_pythread_stacksize,
194 Py_ssize_t, unsigned int),
195 bootstrap, obj,
196 0, &threadID);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (hThread == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 /* I've seen errno == EAGAIN here, which means "there are
199 * too many threads".
200 */
201 int e = errno;
202 dprintf(("%ld: PyThread_start_new_thread failed, errno %d\n",
203 PyThread_get_thread_ident(), e));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 threadID = (unsigned)-1;
205 HeapFree(GetProcessHeap(), 0, obj);
206 }
207 else {
208 dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n",
209 PyThread_get_thread_ident(), (void*)hThread));
210 CloseHandle(hThread);
211 }
212 return (long) threadID;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000213}
214
215/*
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300216 * Return the thread Id instead of a handle. The Id is said to uniquely identify the
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000217 * thread in the system
218 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000219long
220PyThread_get_thread_ident(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 if (!initialized)
223 PyThread_init_thread();
Guido van Rossum706262b2000-05-04 18:47:15 +0000224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 return GetCurrentThreadId();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000226}
227
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000228void
229PyThread_exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
232 if (!initialized)
233 exit(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 _endthreadex(0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000235}
236
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000237/*
238 * Lock support. It has too be implemented as semaphores.
239 * I [Dag] tried to implement it with mutex but I could find a way to
240 * tell whether a thread already own the lock or not.
241 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000242PyThread_type_lock
243PyThread_allocate_lock(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 PNRMUTEX aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 dprintf(("PyThread_allocate_lock called\n"));
248 if (!initialized)
249 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 aLock = AllocNonRecursiveMutex() ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 return (PyThread_type_lock) aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000256}
257
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000258void
259PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 FreeNonRecursiveMutex(aLock) ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000264}
265
266/*
267 * Return 1 on success if the lock was acquired
268 *
269 * and 0 if the lock was not acquired. This means a 0 is returned
270 * if the lock has already been acquired by this thread!
271 */
Antoine Pitrou810023d2010-12-15 22:59:16 +0000272PyLockStatus
273PyThread_acquire_lock_timed(PyThread_type_lock aLock,
274 PY_TIMEOUT_T microseconds, int intr_flag)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000275{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000276 /* Fow now, intr_flag does nothing on Windows, and lock acquires are
277 * uninterruptible. */
278 PyLockStatus success;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 PY_TIMEOUT_T milliseconds;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 if (microseconds >= 0) {
282 milliseconds = microseconds / 1000;
283 if (microseconds % 1000 > 0)
284 ++milliseconds;
285 if ((DWORD) milliseconds != milliseconds)
286 Py_FatalError("Timeout too large for a DWORD, "
287 "please check PY_TIMEOUT_MAX");
288 }
289 else
290 milliseconds = INFINITE;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 dprintf(("%ld: PyThread_acquire_lock_timed(%p, %lld) called\n",
293 PyThread_get_thread_ident(), aLock, microseconds));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000294
Antoine Pitrou810023d2010-12-15 22:59:16 +0000295 if (aLock && EnterNonRecursiveMutex((PNRMUTEX)aLock,
296 (DWORD)milliseconds) == WAIT_OBJECT_0) {
297 success = PY_LOCK_ACQUIRED;
298 }
299 else {
300 success = PY_LOCK_FAILURE;
301 }
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 dprintf(("%ld: PyThread_acquire_lock(%p, %lld) -> %d\n",
304 PyThread_get_thread_ident(), aLock, microseconds, success));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 return success;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000307}
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000308int
309PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
310{
Antoine Pitrou810023d2010-12-15 22:59:16 +0000311 return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0);
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000312}
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000313
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000314void
315PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
320 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %ld\n", PyThread_get_thread_ident(), aLock, GetLastError()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000321}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000322
323/* minimum/maximum thread stack sizes supported */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324#define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */
325#define THREAD_MAX_STACKSIZE 0x10000000 /* 256MB */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000326
327/* set the thread stack size.
328 * Return 0 if size is valid, -1 otherwise.
329 */
330static int
331_pythread_nt_set_stacksize(size_t size)
332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 /* set to default */
334 if (size == 0) {
335 _pythread_stacksize = 0;
336 return 0;
337 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 /* valid range? */
340 if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
341 _pythread_stacksize = size;
342 return 0;
343 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000346}
347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348#define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000349
350
351/* use native Windows TLS functions */
352#define Py_HAVE_NATIVE_TLS
353
354#ifdef Py_HAVE_NATIVE_TLS
355int
356PyThread_create_key(void)
357{
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000358 DWORD result= TlsAlloc();
359 if (result == TLS_OUT_OF_INDEXES)
360 return -1;
361 return (int)result;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000362}
363
364void
365PyThread_delete_key(int key)
366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 TlsFree(key);
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000368}
369
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000370int
371PyThread_set_key_value(int key, void *value)
372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 BOOL ok;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 ok = TlsSetValue(key, value);
376 if (!ok)
377 return -1;
378 return 0;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000379}
380
381void *
382PyThread_get_key_value(int key)
383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 /* because TLS is used in the Py_END_ALLOW_THREAD macro,
385 * it is necessary to preserve the windows error state, because
386 * it is assumed to be preserved across the call to the macro.
387 * Ideally, the macro should be fixed, but it is simpler to
388 * do it here.
389 */
390 DWORD error = GetLastError();
391 void *result = TlsGetValue(key);
392 SetLastError(error);
393 return result;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000394}
395
396void
397PyThread_delete_key_value(int key)
398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 /* NULL is used as "key missing", and it is also the default
400 * given by TlsGetValue() if nothing has been set yet.
401 */
402 TlsSetValue(key, NULL);
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000403}
404
405/* reinitialization of TLS is not necessary after fork when using
406 * the native TLS functions. And forking isn't supported on Windows either.
407 */
408void
409PyThread_ReInitTLS(void)
410{}
411
412#endif