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