blob: e2e4443d8685f9a0cbd878d2f9019a8c70b14bb7 [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
Guido van Rossum706262b2000-05-04 18:47:15 +000012typedef struct NRMUTEX {
13 LONG owned ;
14 DWORD thread_id ;
15 HANDLE hevent ;
16} NRMUTEX, *PNRMUTEX ;
17
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000018
19BOOL
20InitializeNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +000021{
Guido van Rossum706262b2000-05-04 18:47:15 +000022 mutex->owned = -1 ; /* No threads have entered NonRecursiveMutex */
23 mutex->thread_id = 0 ;
24 mutex->hevent = CreateEvent(NULL, FALSE, FALSE, NULL) ;
25 return mutex->hevent != NULL ; /* TRUE if the mutex is created */
26}
27
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000028VOID
29DeleteNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +000030{
31 /* No in-use check */
32 CloseHandle(mutex->hevent) ;
33 mutex->hevent = NULL ; /* Just in case */
34}
35
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000036DWORD
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000037EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
Guido van Rossum706262b2000-05-04 18:47:15 +000038{
39 /* Assume that the thread waits successfully */
40 DWORD ret ;
41
42 /* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000043 if (milliseconds == 0)
Guido van Rossum706262b2000-05-04 18:47:15 +000044 {
Guido van Rossum360e4b82007-05-14 22:51:27 +000045 if (InterlockedCompareExchange(&mutex->owned, 0, -1) != -1)
Guido van Rossum706262b2000-05-04 18:47:15 +000046 return WAIT_TIMEOUT ;
47 ret = WAIT_OBJECT_0 ;
48 }
49 else
50 ret = InterlockedIncrement(&mutex->owned) ?
51 /* Some thread owns the mutex, let's wait... */
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000052 WaitForSingleObject(mutex->hevent, milliseconds) : WAIT_OBJECT_0 ;
Guido van Rossum706262b2000-05-04 18:47:15 +000053
54 mutex->thread_id = GetCurrentThreadId() ; /* We own it */
55 return ret ;
56}
57
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000058BOOL
59LeaveNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +000060{
61 /* We don't own the mutex */
62 mutex->thread_id = 0 ;
63 return
64 InterlockedDecrement(&mutex->owned) < 0 ||
65 SetEvent(mutex->hevent) ; /* Other threads are waiting, wake one on them up */
66}
67
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000068PNRMUTEX
69AllocNonRecursiveMutex(void)
Guido van Rossum706262b2000-05-04 18:47:15 +000070{
71 PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ;
72 if (mutex && !InitializeNonRecursiveMutex(mutex))
73 {
74 free(mutex) ;
75 mutex = NULL ;
76 }
77 return mutex ;
78}
79
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000080void
81FreeNonRecursiveMutex(PNRMUTEX mutex)
Guido van Rossum706262b2000-05-04 18:47:15 +000082{
83 if (mutex)
84 {
85 DeleteNonRecursiveMutex(mutex) ;
86 free(mutex) ;
87 }
88}
89
Guido van Rossum65d5b571998-12-21 19:32:43 +000090long PyThread_get_thread_ident(void);
Guido van Rossumc3f82b61995-01-17 16:29:31 +000091
92/*
Guido van Rossumc3f82b61995-01-17 16:29:31 +000093 * Initialization of the C package, should not be needed.
94 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000095static void
96PyThread__init_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +000097{
98}
99
100/*
101 * Thread support.
102 */
Guido van Rossum3c288632001-10-16 21:13:49 +0000103
104typedef struct {
105 void (*func)(void*);
Tim Peters2e7e7df2003-07-04 04:40:45 +0000106 void *arg;
Guido van Rossum3c288632001-10-16 21:13:49 +0000107} callobj;
108
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000109/* thunker to call adapt between the function type used by the system's
110thread start function and the internally used one. */
111#if defined(MS_WINCE)
112static DWORD WINAPI
113#else
114static unsigned __stdcall
115#endif
Guido van Rossum3c288632001-10-16 21:13:49 +0000116bootstrap(void *call)
117{
118 callobj *obj = (callobj*)call;
Guido van Rossum3c288632001-10-16 21:13:49 +0000119 void (*func)(void*) = obj->func;
120 void *arg = obj->arg;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000121 HeapFree(GetProcessHeap(), 0, obj);
Guido van Rossum3c288632001-10-16 21:13:49 +0000122 func(arg);
123 return 0;
124}
125
Tim Peters2e7e7df2003-07-04 04:40:45 +0000126long
127PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000128{
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000129 HANDLE hThread;
130 unsigned threadID;
131 callobj *obj;
132
Tim Peters2e7e7df2003-07-04 04:40:45 +0000133 dprintf(("%ld: PyThread_start_new_thread called\n",
134 PyThread_get_thread_ident()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000135 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000136 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000137
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000138 obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
139 if (!obj)
Tim Peters2e7e7df2003-07-04 04:40:45 +0000140 return -1;
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000141 obj->func = func;
142 obj->arg = arg;
143#if defined(MS_WINCE)
144 hThread = CreateThread(NULL,
145 Py_SAFE_DOWNCAST(_pythread_stacksize, Py_ssize_t, SIZE_T),
146 bootstrap, obj, 0, &threadID);
147#else
148 hThread = (HANDLE)_beginthreadex(0,
Guido van Rossum360e4b82007-05-14 22:51:27 +0000149 Py_SAFE_DOWNCAST(_pythread_stacksize,
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000150 Py_ssize_t, unsigned int),
151 bootstrap, obj,
152 0, &threadID);
153#endif
154 if (hThread == 0) {
155#if defined(MS_WINCE)
156 /* Save error in variable, to prevent PyThread_get_thread_ident
157 from clobbering it. */
158 unsigned e = GetLastError();
159 dprintf(("%ld: PyThread_start_new_thread failed, win32 error code %u\n",
160 PyThread_get_thread_ident(), e));
161#else
Tim Peters2e7e7df2003-07-04 04:40:45 +0000162 /* I've seen errno == EAGAIN here, which means "there are
163 * too many threads".
164 */
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000165 int e = errno;
166 dprintf(("%ld: PyThread_start_new_thread failed, errno %d\n",
167 PyThread_get_thread_ident(), e));
168#endif
169 threadID = (unsigned)-1;
170 HeapFree(GetProcessHeap(), 0, obj);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000171 }
Tim Peters2e7e7df2003-07-04 04:40:45 +0000172 else {
173 dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n",
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000174 PyThread_get_thread_ident(), (void*)hThread));
175 CloseHandle(hThread);
Tim Peters2e7e7df2003-07-04 04:40:45 +0000176 }
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000177 return (long) threadID;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000178}
179
180/*
181 * Return the thread Id instead of an handle. The Id is said to uniquely identify the
182 * thread in the system
183 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000184long
185PyThread_get_thread_ident(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000186{
187 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000188 PyThread_init_thread();
Guido van Rossum706262b2000-05-04 18:47:15 +0000189
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000190 return GetCurrentThreadId();
191}
192
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000193void
194PyThread_exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000195{
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000196 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
197 if (!initialized)
198 exit(0);
199#if defined(MS_WINCE)
200 ExitThread(0);
201#else
202 _endthreadex(0);
203#endif
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000204}
205
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000206/*
207 * Lock support. It has too be implemented as semaphores.
208 * I [Dag] tried to implement it with mutex but I could find a way to
209 * tell whether a thread already own the lock or not.
210 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000211PyThread_type_lock
212PyThread_allocate_lock(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000213{
Guido van Rossum706262b2000-05-04 18:47:15 +0000214 PNRMUTEX aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000215
Guido van Rossum65d5b571998-12-21 19:32:43 +0000216 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000217 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000218 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000219
Guido van Rossum706262b2000-05-04 18:47:15 +0000220 aLock = AllocNonRecursiveMutex() ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000221
Fred Drakea44d3532000-06-30 15:01:00 +0000222 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000223
Guido van Rossum65d5b571998-12-21 19:32:43 +0000224 return (PyThread_type_lock) aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000225}
226
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000227void
228PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000229{
Fred Drakea44d3532000-06-30 15:01:00 +0000230 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000231
Guido van Rossum706262b2000-05-04 18:47:15 +0000232 FreeNonRecursiveMutex(aLock) ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000233}
234
235/*
236 * Return 1 on success if the lock was acquired
237 *
238 * and 0 if the lock was not acquired. This means a 0 is returned
239 * if the lock has already been acquired by this thread!
240 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000241int
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000242PyThread_acquire_lock_timed(PyThread_type_lock aLock, PY_TIMEOUT_T microseconds)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000243{
Guido van Rossum706262b2000-05-04 18:47:15 +0000244 int success ;
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000245 PY_TIMEOUT_T milliseconds;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000246
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000247 if (microseconds >= 0) {
248 milliseconds = microseconds / 1000;
249 if (microseconds % 1000 > 0)
250 ++milliseconds;
251 if ((DWORD) milliseconds != milliseconds)
252 Py_FatalError("Timeout too large for a DWORD, "
253 "please check PY_TIMEOUT_MAX");
254 }
255 else
256 milliseconds = INFINITE;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000257
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000258 dprintf(("%ld: PyThread_acquire_lock_timed(%p, %lld) called\n",
259 PyThread_get_thread_ident(), aLock, microseconds));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000260
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000261 success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (DWORD) milliseconds) == WAIT_OBJECT_0 ;
262
263 dprintf(("%ld: PyThread_acquire_lock(%p, %lld) -> %d\n",
264 PyThread_get_thread_ident(), aLock, microseconds, success));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000265
266 return success;
267}
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000268int
269PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
270{
271 return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0);
272}
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000273
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000274void
275PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000276{
Fred Drakea44d3532000-06-30 15:01:00 +0000277 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000278
Guido van Rossum706262b2000-05-04 18:47:15 +0000279 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000280 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 +0000281}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282
283/* minimum/maximum thread stack sizes supported */
284#define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */
285#define THREAD_MAX_STACKSIZE 0x10000000 /* 256MB */
286
287/* set the thread stack size.
288 * Return 0 if size is valid, -1 otherwise.
289 */
290static int
291_pythread_nt_set_stacksize(size_t size)
292{
293 /* set to default */
294 if (size == 0) {
295 _pythread_stacksize = 0;
296 return 0;
297 }
298
299 /* valid range? */
300 if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
301 _pythread_stacksize = size;
302 return 0;
303 }
304
305 return -1;
306}
307
308#define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)
Martin v. Löwis7c2b66c2009-01-12 08:21:03 +0000309
310
311/* use native Windows TLS functions */
312#define Py_HAVE_NATIVE_TLS
313
314#ifdef Py_HAVE_NATIVE_TLS
315int
316PyThread_create_key(void)
317{
318 return (int) TlsAlloc();
319}
320
321void
322PyThread_delete_key(int key)
323{
324 TlsFree(key);
325}
326
327/* We must be careful to emulate the strange semantics implemented in thread.c,
328 * where the value is only set if it hasn't been set before.
329 */
330int
331PyThread_set_key_value(int key, void *value)
332{
333 BOOL ok;
334 void *oldvalue;
335
336 assert(value != NULL);
337 oldvalue = TlsGetValue(key);
338 if (oldvalue != NULL)
339 /* ignore value if already set */
340 return 0;
341 ok = TlsSetValue(key, value);
342 if (!ok)
343 return -1;
344 return 0;
345}
346
347void *
348PyThread_get_key_value(int key)
349{
350 /* because TLS is used in the Py_END_ALLOW_THREAD macro,
351 * it is necessary to preserve the windows error state, because
352 * it is assumed to be preserved across the call to the macro.
353 * Ideally, the macro should be fixed, but it is simpler to
354 * do it here.
355 */
356 DWORD error = GetLastError();
357 void *result = TlsGetValue(key);
358 SetLastError(error);
359 return result;
360}
361
362void
363PyThread_delete_key_value(int key)
364{
365 /* NULL is used as "key missing", and it is also the default
366 * given by TlsGetValue() if nothing has been set yet.
367 */
368 TlsSetValue(key, NULL);
369}
370
371/* reinitialization of TLS is not necessary after fork when using
372 * the native TLS functions. And forking isn't supported on Windows either.
373 */
374void
375PyThread_ReInitTLS(void)
376{}
377
378#endif