blob: 7d2290e6b88ac816792f4fb7cebdc7c59da57ec6 [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>
8#include <process.h>
Guido van Rossumc3f82b61995-01-17 16:29:31 +00009
Guido van Rossum706262b2000-05-04 18:47:15 +000010typedef struct NRMUTEX {
11 LONG owned ;
12 DWORD thread_id ;
13 HANDLE hevent ;
14} NRMUTEX, *PNRMUTEX ;
15
Guido van Rossum706262b2000-05-04 18:47:15 +000016typedef PVOID WINAPI interlocked_cmp_xchg_t(PVOID *dest, PVOID exc, PVOID comperand) ;
17
18/* Sorry mate, but we haven't got InterlockedCompareExchange in Win95! */
19static PVOID WINAPI interlocked_cmp_xchg(PVOID *dest, PVOID exc, PVOID comperand)
20{
21 static LONG spinlock = 0 ;
22 PVOID result ;
Guido van Rossumede8c6e2000-05-11 12:53:51 +000023 DWORD dwSleep = 0;
Guido van Rossum706262b2000-05-04 18:47:15 +000024
25 /* Acqire spinlock (yielding control to other threads if cant aquire for the moment) */
Guido van Rossumede8c6e2000-05-11 12:53:51 +000026 while(InterlockedExchange(&spinlock, 1))
27 {
28 // Using Sleep(0) can cause a priority inversion.
29 // Sleep(0) only yields the processor if there's
30 // another thread of the same priority that's
31 // ready to run. If a high-priority thread is
32 // trying to acquire the lock, which is held by
33 // a low-priority thread, then the low-priority
34 // thread may never get scheduled and hence never
35 // free the lock. NT attempts to avoid priority
36 // inversions by temporarily boosting the priority
37 // of low-priority runnable threads, but the problem
38 // can still occur if there's a medium-priority
39 // thread that's always runnable. If Sleep(1) is used,
40 // then the thread unconditionally yields the CPU. We
41 // only do this for the second and subsequent even
42 // iterations, since a millisecond is a long time to wait
43 // if the thread can be scheduled in again sooner
44 // (~100,000 instructions).
45 // Avoid priority inversion: 0, 1, 0, 1,...
46 Sleep(dwSleep);
47 dwSleep = !dwSleep;
48 }
Guido van Rossum706262b2000-05-04 18:47:15 +000049 result = *dest ;
50 if (result == comperand)
51 *dest = exc ;
52 /* Release spinlock */
53 spinlock = 0 ;
54 return result ;
55} ;
56
57static interlocked_cmp_xchg_t *ixchg ;
58BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex)
59{
60 if (!ixchg)
61 {
62 /* Sorely, Win95 has no InterlockedCompareExchange API (Win98 has), so we have to use emulation */
63 HANDLE kernel = GetModuleHandle("kernel32.dll") ;
64 if (!kernel || (ixchg = (interlocked_cmp_xchg_t *)GetProcAddress(kernel, "InterlockedCompareExchange")) == NULL)
65 ixchg = interlocked_cmp_xchg ;
66 }
67
68 mutex->owned = -1 ; /* No threads have entered NonRecursiveMutex */
69 mutex->thread_id = 0 ;
70 mutex->hevent = CreateEvent(NULL, FALSE, FALSE, NULL) ;
71 return mutex->hevent != NULL ; /* TRUE if the mutex is created */
72}
73
Guido van Rossum582acec2000-06-28 22:07:35 +000074#ifdef InterlockedCompareExchange
75#undef InterlockedCompareExchange
76#endif
Guido van Rossum706262b2000-05-04 18:47:15 +000077#define InterlockedCompareExchange(dest,exchange,comperand) (ixchg((dest), (exchange), (comperand)))
78
79VOID DeleteNonRecursiveMutex(PNRMUTEX mutex)
80{
81 /* No in-use check */
82 CloseHandle(mutex->hevent) ;
83 mutex->hevent = NULL ; /* Just in case */
84}
85
86DWORD EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait)
87{
88 /* Assume that the thread waits successfully */
89 DWORD ret ;
90
91 /* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */
92 if (!wait)
93 {
94 if (InterlockedCompareExchange((PVOID *)&mutex->owned, (PVOID)0, (PVOID)-1) != (PVOID)-1)
95 return WAIT_TIMEOUT ;
96 ret = WAIT_OBJECT_0 ;
97 }
98 else
99 ret = InterlockedIncrement(&mutex->owned) ?
100 /* Some thread owns the mutex, let's wait... */
101 WaitForSingleObject(mutex->hevent, INFINITE) : WAIT_OBJECT_0 ;
102
103 mutex->thread_id = GetCurrentThreadId() ; /* We own it */
104 return ret ;
105}
106
107BOOL LeaveNonRecursiveMutex(PNRMUTEX mutex)
108{
109 /* We don't own the mutex */
110 mutex->thread_id = 0 ;
111 return
112 InterlockedDecrement(&mutex->owned) < 0 ||
113 SetEvent(mutex->hevent) ; /* Other threads are waiting, wake one on them up */
114}
115
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000116PNRMUTEX AllocNonRecursiveMutex(void)
Guido van Rossum706262b2000-05-04 18:47:15 +0000117{
118 PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ;
119 if (mutex && !InitializeNonRecursiveMutex(mutex))
120 {
121 free(mutex) ;
122 mutex = NULL ;
123 }
124 return mutex ;
125}
126
127void FreeNonRecursiveMutex(PNRMUTEX mutex)
128{
129 if (mutex)
130 {
131 DeleteNonRecursiveMutex(mutex) ;
132 free(mutex) ;
133 }
134}
135
Guido van Rossum65d5b571998-12-21 19:32:43 +0000136long PyThread_get_thread_ident(void);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000137
138/*
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000139 * Initialization of the C package, should not be needed.
140 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000141static void PyThread__init_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000142{
143}
144
145/*
146 * Thread support.
147 */
Guido van Rossum3c288632001-10-16 21:13:49 +0000148
149typedef struct {
150 void (*func)(void*);
Tim Peters2e7e7df2003-07-04 04:40:45 +0000151 void *arg;
Guido van Rossum3c288632001-10-16 21:13:49 +0000152 long id;
153 HANDLE done;
154} callobj;
155
156static int
157bootstrap(void *call)
158{
159 callobj *obj = (callobj*)call;
160 /* copy callobj since other thread might free it before we're done */
161 void (*func)(void*) = obj->func;
162 void *arg = obj->arg;
163
164 obj->id = PyThread_get_thread_ident();
165 ReleaseSemaphore(obj->done, 1, NULL);
166 func(arg);
167 return 0;
168}
169
Tim Peters2e7e7df2003-07-04 04:40:45 +0000170long
171PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000172{
Tim Peters79248aa2001-08-29 21:37:10 +0000173 unsigned long rv;
Tim Peters2e7e7df2003-07-04 04:40:45 +0000174 callobj obj;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000175
Tim Peters2e7e7df2003-07-04 04:40:45 +0000176 dprintf(("%ld: PyThread_start_new_thread called\n",
177 PyThread_get_thread_ident()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000178 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000179 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000180
Tim Peters2e7e7df2003-07-04 04:40:45 +0000181 obj.id = -1; /* guilty until proved innocent */
182 obj.func = func;
183 obj.arg = arg;
184 obj.done = CreateSemaphore(NULL, 0, 1, NULL);
185 if (obj.done == NULL)
186 return -1;
Guido van Rossum3c288632001-10-16 21:13:49 +0000187
Tim Peters2e7e7df2003-07-04 04:40:45 +0000188 rv = _beginthread(bootstrap, 0, &obj); /* use default stack size */
189 if (rv == (unsigned long)-1) {
190 /* I've seen errno == EAGAIN here, which means "there are
191 * too many threads".
192 */
193 dprintf(("%ld: PyThread_start_new_thread failed: %p errno %d\n",
194 PyThread_get_thread_ident(), rv, errno));
195 obj.id = -1;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000196 }
Tim Peters2e7e7df2003-07-04 04:40:45 +0000197 else {
198 dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n",
199 PyThread_get_thread_ident(), rv));
200 /* wait for thread to initialize, so we can get its id */
201 WaitForSingleObject(obj.done, INFINITE);
202 assert(obj.id != -1);
203 }
204 CloseHandle((HANDLE)obj.done);
205 return obj.id;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000206}
207
208/*
209 * Return the thread Id instead of an handle. The Id is said to uniquely identify the
210 * thread in the system
211 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000212long PyThread_get_thread_ident(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000213{
214 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000215 PyThread_init_thread();
Guido van Rossum706262b2000-05-04 18:47:15 +0000216
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000217 return GetCurrentThreadId();
218}
219
Guido van Rossum65d5b571998-12-21 19:32:43 +0000220static void do_PyThread_exit_thread(int no_cleanup)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000221{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000222 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000223 if (!initialized)
224 if (no_cleanup)
225 _exit(0);
226 else
227 exit(0);
Guido van Rossum49b12261997-08-14 20:12:58 +0000228 _endthread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000229}
230
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231void PyThread_exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000232{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000233 do_PyThread_exit_thread(0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000234}
235
Guido van Rossum65d5b571998-12-21 19:32:43 +0000236void PyThread__exit_thread(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000237{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000238 do_PyThread_exit_thread(1);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000239}
240
241#ifndef NO_EXIT_PROG
Guido van Rossum65d5b571998-12-21 19:32:43 +0000242static void do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000243{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000244 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000245 if (!initialized)
246 if (no_cleanup)
247 _exit(status);
248 else
249 exit(status);
250}
251
Guido van Rossum65d5b571998-12-21 19:32:43 +0000252void PyThread_exit_prog(int status)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000253{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000254 do_PyThread_exit_prog(status, 0);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000255}
256
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000257void PyThread__exit_prog(int status)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000258{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000259 do_PyThread_exit_prog(status, 1);
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000260}
261#endif /* NO_EXIT_PROG */
262
263/*
264 * Lock support. It has too be implemented as semaphores.
265 * I [Dag] tried to implement it with mutex but I could find a way to
266 * tell whether a thread already own the lock or not.
267 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000268PyThread_type_lock PyThread_allocate_lock(void)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000269{
Guido van Rossum706262b2000-05-04 18:47:15 +0000270 PNRMUTEX aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000271
Guido van Rossum65d5b571998-12-21 19:32:43 +0000272 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000273 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000274 PyThread_init_thread();
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000275
Guido van Rossum706262b2000-05-04 18:47:15 +0000276 aLock = AllocNonRecursiveMutex() ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000277
Fred Drakea44d3532000-06-30 15:01:00 +0000278 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000279
Guido van Rossum65d5b571998-12-21 19:32:43 +0000280 return (PyThread_type_lock) aLock;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000281}
282
Guido van Rossum65d5b571998-12-21 19:32:43 +0000283void PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000284{
Fred Drakea44d3532000-06-30 15:01:00 +0000285 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000286
Guido van Rossum706262b2000-05-04 18:47:15 +0000287 FreeNonRecursiveMutex(aLock) ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000288}
289
290/*
291 * Return 1 on success if the lock was acquired
292 *
293 * and 0 if the lock was not acquired. This means a 0 is returned
294 * if the lock has already been acquired by this thread!
295 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000296int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000297{
Guido van Rossum706262b2000-05-04 18:47:15 +0000298 int success ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000299
Fred Drakea44d3532000-06-30 15:01:00 +0000300 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000301
Guido van Rossum706262b2000-05-04 18:47:15 +0000302 success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag == 1 ? INFINITE : 0)) == WAIT_OBJECT_0 ;
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000303
Fred Drakea44d3532000-06-30 15:01:00 +0000304 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000305
306 return success;
307}
308
Guido van Rossum65d5b571998-12-21 19:32:43 +0000309void PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000310{
Fred Drakea44d3532000-06-30 15:01:00 +0000311 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000312
Guido van Rossum706262b2000-05-04 18:47:15 +0000313 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
Fred Drakea44d3532000-06-30 15:01:00 +0000314 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError()));
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000315}