blob: 944552992f4f442d8d445a5f953b9bb96ff3b6c9 [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
37EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait)
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 */
43 if (!wait)
44 {
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... */
52 WaitForSingleObject(mutex->hevent, INFINITE) : WAIT_OBJECT_0 ;
53
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 long id;
108 HANDLE done;
109} callobj;
110
111static int
112bootstrap(void *call)
113{
114 callobj *obj = (callobj*)call;
115 /* copy callobj since other thread might free it before we're done */
116 void (*func)(void*) = obj->func;
117 void *arg = obj->arg;
118
119 obj->id = PyThread_get_thread_ident();
120 ReleaseSemaphore(obj->done, 1, NULL);
121 func(arg);
122 return 0;
123}
124
Tim Peters2e7e7df2003-07-04 04:40:45 +0000125long
126PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000127{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000128 Py_uintptr_t rv;
Tim Peters2e7e7df2003-07-04 04:40:45 +0000129 callobj obj;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000130
Tim Peters2e7e7df2003-07-04 04:40:45 +0000131 dprintf(("%ld: PyThread_start_new_thread called\n",
132 PyThread_get_thread_ident()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000133 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000134 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000135
Tim Peters2e7e7df2003-07-04 04:40:45 +0000136 obj.id = -1; /* guilty until proved innocent */
137 obj.func = func;
138 obj.arg = arg;
139 obj.done = CreateSemaphore(NULL, 0, 1, NULL);
140 if (obj.done == NULL)
141 return -1;
Guido van Rossum3c288632001-10-16 21:13:49 +0000142
Guido van Rossum360e4b82007-05-14 22:51:27 +0000143 rv = _beginthread(bootstrap,
144 Py_SAFE_DOWNCAST(_pythread_stacksize,
145 Py_ssize_t, int),
146 &obj);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000147 if (rv == (Py_uintptr_t)-1) {
Tim Peters2e7e7df2003-07-04 04:40:45 +0000148 /* I've seen errno == EAGAIN here, which means "there are
149 * too many threads".
150 */
151 dprintf(("%ld: PyThread_start_new_thread failed: %p errno %d\n",
Guido van Rossumd8faa362007-04-27 19:54:29 +0000152 PyThread_get_thread_ident(), (void*)rv, errno));
Tim Peters2e7e7df2003-07-04 04:40:45 +0000153 obj.id = -1;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000154 }
Tim Peters2e7e7df2003-07-04 04:40:45 +0000155 else {
156 dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n",
Guido van Rossumd8faa362007-04-27 19:54:29 +0000157 PyThread_get_thread_ident(), (void*)rv));
Tim Peters2e7e7df2003-07-04 04:40:45 +0000158 /* wait for thread to initialize, so we can get its id */
159 WaitForSingleObject(obj.done, INFINITE);
160 assert(obj.id != -1);
161 }
162 CloseHandle((HANDLE)obj.done);
163 return obj.id;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000164}
165
166/*
167 * Return the thread Id instead of an handle. The Id is said to uniquely identify the
168 * thread in the system
169 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000170long
171PyThread_get_thread_ident(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000172{
173 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000174 PyThread_init_thread();
Guido van Rossum706262b2000-05-04 18:47:15 +0000175
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000176 return GetCurrentThreadId();
177}
178
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000179static void
180do_PyThread_exit_thread(int no_cleanup)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000181{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000182 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000183 if (!initialized)
184 if (no_cleanup)
185 _exit(0);
186 else
187 exit(0);
Guido van Rossum49b12261997-08-14 20:12:58 +0000188 _endthread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000189}
190
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000191void
192PyThread_exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000193{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000194 do_PyThread_exit_thread(0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000195}
196
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000197void
198PyThread__exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000199{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000200 do_PyThread_exit_thread(1);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000201}
202
203#ifndef NO_EXIT_PROG
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000204static void
205do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000206{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000207 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000208 if (!initialized)
209 if (no_cleanup)
210 _exit(status);
211 else
212 exit(status);
213}
214
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000215void
216PyThread_exit_prog(int status)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000217{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000218 do_PyThread_exit_prog(status, 0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000219}
220
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000221void
222PyThread__exit_prog(int status)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000223{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000224 do_PyThread_exit_prog(status, 1);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000225}
226#endif /* NO_EXIT_PROG */
227
228/*
229 * Lock support. It has too be implemented as semaphores.
230 * I [Dag] tried to implement it with mutex but I could find a way to
231 * tell whether a thread already own the lock or not.
232 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000233PyThread_type_lock
234PyThread_allocate_lock(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000235{
Guido van Rossum706262b2000-05-04 18:47:15 +0000236 PNRMUTEX aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000237
Guido van Rossum65d5b571998-12-21 19:32:43 +0000238 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000239 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000240 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000241
Guido van Rossum706262b2000-05-04 18:47:15 +0000242 aLock = AllocNonRecursiveMutex() ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000243
Fred Drakea44d3532000-06-30 15:01:00 +0000244 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000245
Guido van Rossum65d5b571998-12-21 19:32:43 +0000246 return (PyThread_type_lock) aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000247}
248
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000249void
250PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000251{
Fred Drakea44d3532000-06-30 15:01:00 +0000252 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000253
Guido van Rossum706262b2000-05-04 18:47:15 +0000254 FreeNonRecursiveMutex(aLock) ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000255}
256
257/*
258 * Return 1 on success if the lock was acquired
259 *
260 * and 0 if the lock was not acquired. This means a 0 is returned
261 * if the lock has already been acquired by this thread!
262 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000263int
264PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000265{
Guido van Rossum706262b2000-05-04 18:47:15 +0000266 int success ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000267
Fred Drakea44d3532000-06-30 15:01:00 +0000268 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000269
Georg Brandlaf410b52005-07-08 22:26:13 +0000270 success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag ? INFINITE : 0)) == WAIT_OBJECT_0 ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000271
Fred Drakea44d3532000-06-30 15:01:00 +0000272 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000273
274 return success;
275}
276
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000277void
278PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000279{
Fred Drakea44d3532000-06-30 15:01:00 +0000280 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000281
Guido van Rossum706262b2000-05-04 18:47:15 +0000282 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000283 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 +0000284}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000285
286/* minimum/maximum thread stack sizes supported */
287#define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */
288#define THREAD_MAX_STACKSIZE 0x10000000 /* 256MB */
289
290/* set the thread stack size.
291 * Return 0 if size is valid, -1 otherwise.
292 */
293static int
294_pythread_nt_set_stacksize(size_t size)
295{
296 /* set to default */
297 if (size == 0) {
298 _pythread_stacksize = 0;
299 return 0;
300 }
301
302 /* valid range? */
303 if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
304 _pythread_stacksize = size;
305 return 0;
306 }
307
308 return -1;
309}
310
311#define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)