Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */ |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 33 | /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */ |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 35 | #include <windows.h> |
| 36 | #include <limits.h> |
| 37 | #include <process.h> |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 39 | typedef struct NRMUTEX { |
| 40 | LONG owned ; |
| 41 | DWORD thread_id ; |
| 42 | HANDLE hevent ; |
| 43 | } NRMUTEX, *PNRMUTEX ; |
| 44 | |
| 45 | |
| 46 | typedef PVOID WINAPI interlocked_cmp_xchg_t(PVOID *dest, PVOID exc, PVOID comperand) ; |
| 47 | |
| 48 | /* Sorry mate, but we haven't got InterlockedCompareExchange in Win95! */ |
| 49 | static PVOID WINAPI interlocked_cmp_xchg(PVOID *dest, PVOID exc, PVOID comperand) |
| 50 | { |
| 51 | static LONG spinlock = 0 ; |
| 52 | PVOID result ; |
Guido van Rossum | ede8c6e | 2000-05-11 12:53:51 +0000 | [diff] [blame] | 53 | DWORD dwSleep = 0; |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 54 | |
| 55 | /* Acqire spinlock (yielding control to other threads if cant aquire for the moment) */ |
Guido van Rossum | ede8c6e | 2000-05-11 12:53:51 +0000 | [diff] [blame] | 56 | while(InterlockedExchange(&spinlock, 1)) |
| 57 | { |
| 58 | // Using Sleep(0) can cause a priority inversion. |
| 59 | // Sleep(0) only yields the processor if there's |
| 60 | // another thread of the same priority that's |
| 61 | // ready to run. If a high-priority thread is |
| 62 | // trying to acquire the lock, which is held by |
| 63 | // a low-priority thread, then the low-priority |
| 64 | // thread may never get scheduled and hence never |
| 65 | // free the lock. NT attempts to avoid priority |
| 66 | // inversions by temporarily boosting the priority |
| 67 | // of low-priority runnable threads, but the problem |
| 68 | // can still occur if there's a medium-priority |
| 69 | // thread that's always runnable. If Sleep(1) is used, |
| 70 | // then the thread unconditionally yields the CPU. We |
| 71 | // only do this for the second and subsequent even |
| 72 | // iterations, since a millisecond is a long time to wait |
| 73 | // if the thread can be scheduled in again sooner |
| 74 | // (~100,000 instructions). |
| 75 | // Avoid priority inversion: 0, 1, 0, 1,... |
| 76 | Sleep(dwSleep); |
| 77 | dwSleep = !dwSleep; |
| 78 | } |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 79 | result = *dest ; |
| 80 | if (result == comperand) |
| 81 | *dest = exc ; |
| 82 | /* Release spinlock */ |
| 83 | spinlock = 0 ; |
| 84 | return result ; |
| 85 | } ; |
| 86 | |
| 87 | static interlocked_cmp_xchg_t *ixchg ; |
| 88 | BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex) |
| 89 | { |
| 90 | if (!ixchg) |
| 91 | { |
| 92 | /* Sorely, Win95 has no InterlockedCompareExchange API (Win98 has), so we have to use emulation */ |
| 93 | HANDLE kernel = GetModuleHandle("kernel32.dll") ; |
| 94 | if (!kernel || (ixchg = (interlocked_cmp_xchg_t *)GetProcAddress(kernel, "InterlockedCompareExchange")) == NULL) |
| 95 | ixchg = interlocked_cmp_xchg ; |
| 96 | } |
| 97 | |
| 98 | mutex->owned = -1 ; /* No threads have entered NonRecursiveMutex */ |
| 99 | mutex->thread_id = 0 ; |
| 100 | mutex->hevent = CreateEvent(NULL, FALSE, FALSE, NULL) ; |
| 101 | return mutex->hevent != NULL ; /* TRUE if the mutex is created */ |
| 102 | } |
| 103 | |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 104 | #ifdef InterlockedCompareExchange |
| 105 | #undef InterlockedCompareExchange |
| 106 | #endif |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 107 | #define InterlockedCompareExchange(dest,exchange,comperand) (ixchg((dest), (exchange), (comperand))) |
| 108 | |
| 109 | VOID DeleteNonRecursiveMutex(PNRMUTEX mutex) |
| 110 | { |
| 111 | /* No in-use check */ |
| 112 | CloseHandle(mutex->hevent) ; |
| 113 | mutex->hevent = NULL ; /* Just in case */ |
| 114 | } |
| 115 | |
| 116 | DWORD EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait) |
| 117 | { |
| 118 | /* Assume that the thread waits successfully */ |
| 119 | DWORD ret ; |
| 120 | |
| 121 | /* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */ |
| 122 | if (!wait) |
| 123 | { |
| 124 | if (InterlockedCompareExchange((PVOID *)&mutex->owned, (PVOID)0, (PVOID)-1) != (PVOID)-1) |
| 125 | return WAIT_TIMEOUT ; |
| 126 | ret = WAIT_OBJECT_0 ; |
| 127 | } |
| 128 | else |
| 129 | ret = InterlockedIncrement(&mutex->owned) ? |
| 130 | /* Some thread owns the mutex, let's wait... */ |
| 131 | WaitForSingleObject(mutex->hevent, INFINITE) : WAIT_OBJECT_0 ; |
| 132 | |
| 133 | mutex->thread_id = GetCurrentThreadId() ; /* We own it */ |
| 134 | return ret ; |
| 135 | } |
| 136 | |
| 137 | BOOL LeaveNonRecursiveMutex(PNRMUTEX mutex) |
| 138 | { |
| 139 | /* We don't own the mutex */ |
| 140 | mutex->thread_id = 0 ; |
| 141 | return |
| 142 | InterlockedDecrement(&mutex->owned) < 0 || |
| 143 | SetEvent(mutex->hevent) ; /* Other threads are waiting, wake one on them up */ |
| 144 | } |
| 145 | |
| 146 | PNRMUTEX AllocNonRecursiveMutex() |
| 147 | { |
| 148 | PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ; |
| 149 | if (mutex && !InitializeNonRecursiveMutex(mutex)) |
| 150 | { |
| 151 | free(mutex) ; |
| 152 | mutex = NULL ; |
| 153 | } |
| 154 | return mutex ; |
| 155 | } |
| 156 | |
| 157 | void FreeNonRecursiveMutex(PNRMUTEX mutex) |
| 158 | { |
| 159 | if (mutex) |
| 160 | { |
| 161 | DeleteNonRecursiveMutex(mutex) ; |
| 162 | free(mutex) ; |
| 163 | } |
| 164 | } |
| 165 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 166 | long PyThread_get_thread_ident(void); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * Change all headers to pure ANSI as no one will use K&R style on an |
| 170 | * NT |
| 171 | */ |
| 172 | |
| 173 | /* |
| 174 | * Initialization of the C package, should not be needed. |
| 175 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 176 | static void PyThread__init_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 177 | { |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Thread support. |
| 182 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 183 | int PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 184 | { |
Fredrik Lundh | 34a9637 | 2000-06-29 17:25:30 +0000 | [diff] [blame^] | 185 | #if _MSC_VER >= 1200 |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 186 | INT_PTR rv; |
Fredrik Lundh | 34a9637 | 2000-06-29 17:25:30 +0000 | [diff] [blame^] | 187 | #else |
| 188 | unsigned long rv; |
| 189 | #endif |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 190 | int success = 0; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 192 | dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident())); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 193 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 194 | PyThread_init_thread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 196 | rv = _beginthread(func, 0, arg); /* use default stack size */ |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 198 | if (rv != -1) { |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 199 | success = 1; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 200 | dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n", PyThread_get_thread_ident(), rv)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return success; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Return the thread Id instead of an handle. The Id is said to uniquely identify the |
| 208 | * thread in the system |
| 209 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 210 | long PyThread_get_thread_ident(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 211 | { |
| 212 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 213 | PyThread_init_thread(); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 214 | |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 215 | return GetCurrentThreadId(); |
| 216 | } |
| 217 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 218 | static void do_PyThread_exit_thread(int no_cleanup) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 219 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 220 | dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 221 | if (!initialized) |
| 222 | if (no_cleanup) |
| 223 | _exit(0); |
| 224 | else |
| 225 | exit(0); |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 226 | _endthread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 229 | void PyThread_exit_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 230 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 231 | do_PyThread_exit_thread(0); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 234 | void PyThread__exit_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 235 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 236 | do_PyThread_exit_thread(1); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | #ifndef NO_EXIT_PROG |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 240 | static void do_PyThread_exit_prog(int status, int no_cleanup) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 241 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 242 | dprintf(("PyThread_exit_prog(%d) called\n", status)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 243 | if (!initialized) |
| 244 | if (no_cleanup) |
| 245 | _exit(status); |
| 246 | else |
| 247 | exit(status); |
| 248 | } |
| 249 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 250 | void PyThread_exit_prog(int status) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 251 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 252 | do_PyThread_exit_prog(status, 0); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 255 | void PyThread__exit_prog _P1(int status) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 256 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 257 | do_PyThread_exit_prog(status, 1); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 258 | } |
| 259 | #endif /* NO_EXIT_PROG */ |
| 260 | |
| 261 | /* |
| 262 | * Lock support. It has too be implemented as semaphores. |
| 263 | * I [Dag] tried to implement it with mutex but I could find a way to |
| 264 | * tell whether a thread already own the lock or not. |
| 265 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 266 | PyThread_type_lock PyThread_allocate_lock(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 267 | { |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 268 | PNRMUTEX aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 269 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 270 | dprintf(("PyThread_allocate_lock called\n")); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 271 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 272 | PyThread_init_thread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 274 | aLock = AllocNonRecursiveMutex() ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 276 | dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 277 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 278 | return (PyThread_type_lock) aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 281 | void PyThread_free_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 282 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 283 | dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 285 | FreeNonRecursiveMutex(aLock) ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Return 1 on success if the lock was acquired |
| 290 | * |
| 291 | * and 0 if the lock was not acquired. This means a 0 is returned |
| 292 | * if the lock has already been acquired by this thread! |
| 293 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 294 | int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 295 | { |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 296 | int success ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 297 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 298 | dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),(long)aLock, waitflag)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 300 | success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag == 1 ? INFINITE : 0)) == WAIT_OBJECT_0 ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 301 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 302 | dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n", PyThread_get_thread_ident(),(long)aLock, waitflag, success)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 303 | |
| 304 | return success; |
| 305 | } |
| 306 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 307 | void PyThread_release_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 308 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 309 | dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 310 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 311 | if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock))) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 312 | dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n", PyThread_get_thread_ident(), (long)aLock, GetLastError())); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Semaphore support. |
| 317 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 318 | PyThread_type_sema PyThread_allocate_sema(int value) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 319 | { |
| 320 | HANDLE aSemaphore; |
| 321 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 322 | dprintf(("%ld: PyThread_allocate_sema called\n", PyThread_get_thread_ident())); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 323 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 324 | PyThread_init_thread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 325 | |
| 326 | aSemaphore = CreateSemaphore( NULL, /* Security attributes */ |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 327 | value, /* Initial value */ |
| 328 | INT_MAX, /* Maximum value */ |
| 329 | NULL); /* Name of semaphore */ |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 330 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 331 | dprintf(("%ld: PyThread_allocate_sema() -> %lx\n", PyThread_get_thread_ident(), (long)aSemaphore)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 332 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 333 | return (PyThread_type_sema) aSemaphore; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 336 | void PyThread_free_sema(PyThread_type_sema aSemaphore) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 337 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 338 | dprintf(("%ld: PyThread_free_sema(%lx) called\n", PyThread_get_thread_ident(), (long)aSemaphore)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 339 | |
| 340 | CloseHandle((HANDLE) aSemaphore); |
| 341 | } |
| 342 | |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 343 | /* |
| 344 | XXX must do something about waitflag |
| 345 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 346 | int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 347 | { |
| 348 | DWORD waitResult; |
| 349 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 350 | dprintf(("%ld: PyThread_down_sema(%lx) called\n", PyThread_get_thread_ident(), (long)aSemaphore)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 351 | |
| 352 | waitResult = WaitForSingleObject( (HANDLE) aSemaphore, INFINITE); |
| 353 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 354 | dprintf(("%ld: PyThread_down_sema(%lx) return: %l\n", PyThread_get_thread_ident(),(long) aSemaphore, waitResult)); |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 355 | return 0; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 358 | void PyThread_up_sema(PyThread_type_sema aSemaphore) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 359 | { |
| 360 | ReleaseSemaphore( |
| 361 | (HANDLE) aSemaphore, /* Handle of semaphore */ |
| 362 | 1, /* increment count by one */ |
| 363 | NULL); /* not interested in previous count */ |
| 364 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 365 | dprintf(("%ld: PyThread_up_sema(%lx)\n", PyThread_get_thread_ident(), (long)aSemaphore)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 366 | } |