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 | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 9 | typedef struct NRMUTEX { |
| 10 | LONG owned ; |
| 11 | DWORD thread_id ; |
| 12 | HANDLE hevent ; |
| 13 | } NRMUTEX, *PNRMUTEX ; |
| 14 | |
| 15 | |
| 16 | typedef PVOID WINAPI interlocked_cmp_xchg_t(PVOID *dest, PVOID exc, PVOID comperand) ; |
| 17 | |
| 18 | /* Sorry mate, but we haven't got InterlockedCompareExchange in Win95! */ |
| 19 | static PVOID WINAPI interlocked_cmp_xchg(PVOID *dest, PVOID exc, PVOID comperand) |
| 20 | { |
| 21 | static LONG spinlock = 0 ; |
| 22 | PVOID result ; |
Guido van Rossum | ede8c6e | 2000-05-11 12:53:51 +0000 | [diff] [blame] | 23 | DWORD dwSleep = 0; |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 24 | |
| 25 | /* 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] | 26 | 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 Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 49 | result = *dest ; |
| 50 | if (result == comperand) |
| 51 | *dest = exc ; |
| 52 | /* Release spinlock */ |
| 53 | spinlock = 0 ; |
| 54 | return result ; |
| 55 | } ; |
| 56 | |
| 57 | static interlocked_cmp_xchg_t *ixchg ; |
| 58 | BOOL 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 Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 74 | #ifdef InterlockedCompareExchange |
| 75 | #undef InterlockedCompareExchange |
| 76 | #endif |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 77 | #define InterlockedCompareExchange(dest,exchange,comperand) (ixchg((dest), (exchange), (comperand))) |
| 78 | |
| 79 | VOID DeleteNonRecursiveMutex(PNRMUTEX mutex) |
| 80 | { |
| 81 | /* No in-use check */ |
| 82 | CloseHandle(mutex->hevent) ; |
| 83 | mutex->hevent = NULL ; /* Just in case */ |
| 84 | } |
| 85 | |
| 86 | DWORD 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 | |
| 107 | BOOL 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 Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 116 | PNRMUTEX AllocNonRecursiveMutex(void) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 117 | { |
| 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 | |
| 127 | void FreeNonRecursiveMutex(PNRMUTEX mutex) |
| 128 | { |
| 129 | if (mutex) |
| 130 | { |
| 131 | DeleteNonRecursiveMutex(mutex) ; |
| 132 | free(mutex) ; |
| 133 | } |
| 134 | } |
| 135 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 136 | long PyThread_get_thread_ident(void); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * Change all headers to pure ANSI as no one will use K&R style on an |
| 140 | * NT |
| 141 | */ |
| 142 | |
| 143 | /* |
| 144 | * Initialization of the C package, should not be needed. |
| 145 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 146 | static void PyThread__init_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 147 | { |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Thread support. |
| 152 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 153 | int PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 154 | { |
Fredrik Lundh | 6947d0b | 2000-08-07 20:16:28 +0000 | [diff] [blame] | 155 | uintptr_t rv; |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 156 | int success = 0; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 158 | 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] | 159 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 160 | PyThread_init_thread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 162 | rv = _beginthread(func, 0, arg); /* use default stack size */ |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 164 | if (rv != -1) { |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 165 | success = 1; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 166 | 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] | 167 | } |
| 168 | |
| 169 | return success; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Return the thread Id instead of an handle. The Id is said to uniquely identify the |
| 174 | * thread in the system |
| 175 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 176 | long PyThread_get_thread_ident(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 177 | { |
| 178 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 179 | PyThread_init_thread(); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 181 | return GetCurrentThreadId(); |
| 182 | } |
| 183 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 184 | static void do_PyThread_exit_thread(int no_cleanup) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 185 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 186 | dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 187 | if (!initialized) |
| 188 | if (no_cleanup) |
| 189 | _exit(0); |
| 190 | else |
| 191 | exit(0); |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 192 | _endthread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 195 | void PyThread_exit_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 196 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 197 | do_PyThread_exit_thread(0); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 200 | void PyThread__exit_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 201 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 202 | do_PyThread_exit_thread(1); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | #ifndef NO_EXIT_PROG |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 206 | static void do_PyThread_exit_prog(int status, int no_cleanup) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 207 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 208 | dprintf(("PyThread_exit_prog(%d) called\n", status)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 209 | if (!initialized) |
| 210 | if (no_cleanup) |
| 211 | _exit(status); |
| 212 | else |
| 213 | exit(status); |
| 214 | } |
| 215 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 216 | void PyThread_exit_prog(int status) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 217 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 218 | do_PyThread_exit_prog(status, 0); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 221 | void PyThread__exit_prog(int status) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 222 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 223 | do_PyThread_exit_prog(status, 1); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 224 | } |
| 225 | #endif /* NO_EXIT_PROG */ |
| 226 | |
| 227 | /* |
| 228 | * Lock support. It has too be implemented as semaphores. |
| 229 | * I [Dag] tried to implement it with mutex but I could find a way to |
| 230 | * tell whether a thread already own the lock or not. |
| 231 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 232 | PyThread_type_lock PyThread_allocate_lock(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 233 | { |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 234 | PNRMUTEX aLock; |
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 | dprintf(("PyThread_allocate_lock called\n")); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 237 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 238 | PyThread_init_thread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 240 | aLock = AllocNonRecursiveMutex() ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 241 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 242 | 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] | 243 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 244 | return (PyThread_type_lock) aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 247 | void PyThread_free_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 248 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 249 | 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] | 250 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 251 | FreeNonRecursiveMutex(aLock) ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Return 1 on success if the lock was acquired |
| 256 | * |
| 257 | * and 0 if the lock was not acquired. This means a 0 is returned |
| 258 | * if the lock has already been acquired by this thread! |
| 259 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 260 | int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 261 | { |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 262 | int success ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 263 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 264 | 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] | 265 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 266 | 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] | 267 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 268 | 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] | 269 | |
| 270 | return success; |
| 271 | } |
| 272 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 273 | void PyThread_release_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 274 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 275 | 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] | 276 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 277 | if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock))) |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 278 | 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] | 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Semaphore support. |
| 283 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 284 | PyThread_type_sema PyThread_allocate_sema(int value) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 285 | { |
| 286 | HANDLE aSemaphore; |
| 287 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 288 | dprintf(("%ld: PyThread_allocate_sema called\n", PyThread_get_thread_ident())); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 289 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 290 | PyThread_init_thread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 291 | |
| 292 | aSemaphore = CreateSemaphore( NULL, /* Security attributes */ |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 293 | value, /* Initial value */ |
| 294 | INT_MAX, /* Maximum value */ |
| 295 | NULL); /* Name of semaphore */ |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 296 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 297 | 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] | 298 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 299 | return (PyThread_type_sema) aSemaphore; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 302 | void PyThread_free_sema(PyThread_type_sema aSemaphore) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 303 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 304 | 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] | 305 | |
| 306 | CloseHandle((HANDLE) aSemaphore); |
| 307 | } |
| 308 | |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 309 | /* |
| 310 | XXX must do something about waitflag |
| 311 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 312 | int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 313 | { |
| 314 | DWORD waitResult; |
| 315 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 316 | 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] | 317 | |
| 318 | waitResult = WaitForSingleObject( (HANDLE) aSemaphore, INFINITE); |
| 319 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 320 | 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] | 321 | return 0; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 324 | void PyThread_up_sema(PyThread_type_sema aSemaphore) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 325 | { |
| 326 | ReleaseSemaphore( |
| 327 | (HANDLE) aSemaphore, /* Handle of semaphore */ |
| 328 | 1, /* increment count by one */ |
| 329 | NULL); /* not interested in previous count */ |
| 330 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 331 | 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] | 332 | } |