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 */ |
Tim Peters | e64ef93 | 2002-02-28 21:34:34 +0000 | [diff] [blame] | 4 | /* Eliminated some memory leaks, gsw@agere.com */ |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 6 | #include <windows.h> |
| 7 | #include <limits.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 9 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10 | #endif |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 11 | |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 12 | /* options */ |
| 13 | #ifndef _PY_USE_CV_LOCKS |
| 14 | #define _PY_USE_CV_LOCKS 1 /* use locks based on cond vars */ |
| 15 | #endif |
| 16 | |
| 17 | /* Now, define a non-recursive mutex using either condition variables |
| 18 | * and critical sections (fast) or using operating system mutexes |
| 19 | * (slow) |
| 20 | */ |
| 21 | |
| 22 | #if _PY_USE_CV_LOCKS |
| 23 | |
| 24 | #include "condvar.h" |
| 25 | |
| 26 | typedef struct _NRMUTEX |
| 27 | { |
| 28 | PyMUTEX_T cs; |
| 29 | PyCOND_T cv; |
| 30 | int locked; |
| 31 | } NRMUTEX; |
| 32 | typedef NRMUTEX *PNRMUTEX; |
| 33 | |
| 34 | PNRMUTEX |
| 35 | AllocNonRecursiveMutex() |
| 36 | { |
| 37 | PNRMUTEX m = (PNRMUTEX)malloc(sizeof(NRMUTEX)); |
| 38 | if (!m) |
| 39 | return NULL; |
| 40 | if (PyCOND_INIT(&m->cv)) |
| 41 | goto fail; |
| 42 | if (PyMUTEX_INIT(&m->cs)) { |
| 43 | PyCOND_FINI(&m->cv); |
| 44 | goto fail; |
| 45 | } |
| 46 | m->locked = 0; |
| 47 | return m; |
| 48 | fail: |
| 49 | free(m); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | VOID |
| 54 | FreeNonRecursiveMutex(PNRMUTEX mutex) |
| 55 | { |
| 56 | if (mutex) { |
| 57 | PyCOND_FINI(&mutex->cv); |
| 58 | PyMUTEX_FINI(&mutex->cs); |
| 59 | free(mutex); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | DWORD |
| 64 | EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds) |
| 65 | { |
| 66 | DWORD result = WAIT_OBJECT_0; |
| 67 | if (PyMUTEX_LOCK(&mutex->cs)) |
| 68 | return WAIT_FAILED; |
| 69 | if (milliseconds == INFINITE) { |
| 70 | while (mutex->locked) { |
| 71 | if (PyCOND_WAIT(&mutex->cv, &mutex->cs)) { |
| 72 | result = WAIT_FAILED; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } else if (milliseconds != 0) { |
| 77 | /* wait at least until the target */ |
| 78 | DWORD now, target = GetTickCount() + milliseconds; |
| 79 | while (mutex->locked) { |
| 80 | if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, milliseconds*1000) < 0) { |
| 81 | result = WAIT_FAILED; |
| 82 | break; |
| 83 | } |
| 84 | now = GetTickCount(); |
| 85 | if (target <= now) |
| 86 | break; |
| 87 | milliseconds = target-now; |
| 88 | } |
| 89 | } |
| 90 | if (!mutex->locked) { |
| 91 | mutex->locked = 1; |
| 92 | result = WAIT_OBJECT_0; |
| 93 | } else if (result == WAIT_OBJECT_0) |
| 94 | result = WAIT_TIMEOUT; |
| 95 | /* else, it is WAIT_FAILED */ |
| 96 | PyMUTEX_UNLOCK(&mutex->cs); /* must ignore result here */ |
| 97 | return result; |
| 98 | } |
| 99 | |
| 100 | BOOL |
| 101 | LeaveNonRecursiveMutex(PNRMUTEX mutex) |
| 102 | { |
| 103 | BOOL result; |
| 104 | if (PyMUTEX_LOCK(&mutex->cs)) |
| 105 | return FALSE; |
| 106 | mutex->locked = 0; |
| 107 | result = PyCOND_SIGNAL(&mutex->cv); |
| 108 | result &= PyMUTEX_UNLOCK(&mutex->cs); |
| 109 | return result; |
| 110 | } |
| 111 | |
| 112 | #else /* if ! _PY_USE_CV_LOCKS */ |
| 113 | |
| 114 | /* NR-locks based on a kernel mutex */ |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 115 | #define PNRMUTEX HANDLE |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 117 | PNRMUTEX |
| 118 | AllocNonRecursiveMutex() |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 119 | { |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 120 | return CreateSemaphore(NULL, 1, 1, NULL); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 123 | VOID |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 124 | FreeNonRecursiveMutex(PNRMUTEX mutex) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | /* No in-use check */ |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 127 | CloseHandle(mutex); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 130 | DWORD |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 131 | EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 132 | { |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 133 | return WaitForSingleObject(mutex, milliseconds); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 136 | BOOL |
| 137 | LeaveNonRecursiveMutex(PNRMUTEX mutex) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 138 | { |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 139 | return ReleaseSemaphore(mutex, 1, NULL); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 140 | } |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 141 | #endif /* _PY_USE_CV_LOCKS */ |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 143 | long PyThread_get_thread_ident(void); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 144 | |
| 145 | /* |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 146 | * Initialization of the C package, should not be needed. |
| 147 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 148 | static void |
| 149 | PyThread__init_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 150 | { |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Thread support. |
| 155 | */ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 156 | |
| 157 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | void (*func)(void*); |
| 159 | void *arg; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 160 | } callobj; |
| 161 | |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 162 | /* thunker to call adapt between the function type used by the system's |
| 163 | thread start function and the internally used one. */ |
| 164 | #if defined(MS_WINCE) |
| 165 | static DWORD WINAPI |
| 166 | #else |
| 167 | static unsigned __stdcall |
| 168 | #endif |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 169 | bootstrap(void *call) |
| 170 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | callobj *obj = (callobj*)call; |
| 172 | void (*func)(void*) = obj->func; |
| 173 | void *arg = obj->arg; |
| 174 | HeapFree(GetProcessHeap(), 0, obj); |
| 175 | func(arg); |
| 176 | return 0; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 179 | long |
| 180 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | HANDLE hThread; |
| 183 | unsigned threadID; |
| 184 | callobj *obj; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | dprintf(("%ld: PyThread_start_new_thread called\n", |
| 187 | PyThread_get_thread_ident())); |
| 188 | if (!initialized) |
| 189 | PyThread_init_thread(); |
| 190 | |
| 191 | obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj)); |
| 192 | if (!obj) |
| 193 | return -1; |
| 194 | obj->func = func; |
| 195 | obj->arg = arg; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 196 | #if defined(MS_WINCE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | hThread = CreateThread(NULL, |
| 198 | Py_SAFE_DOWNCAST(_pythread_stacksize, Py_ssize_t, SIZE_T), |
| 199 | bootstrap, obj, 0, &threadID); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 200 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | hThread = (HANDLE)_beginthreadex(0, |
| 202 | Py_SAFE_DOWNCAST(_pythread_stacksize, |
| 203 | Py_ssize_t, unsigned int), |
| 204 | bootstrap, obj, |
| 205 | 0, &threadID); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 206 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | if (hThread == 0) { |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 208 | #if defined(MS_WINCE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | /* Save error in variable, to prevent PyThread_get_thread_ident |
| 210 | from clobbering it. */ |
| 211 | unsigned e = GetLastError(); |
| 212 | dprintf(("%ld: PyThread_start_new_thread failed, win32 error code %u\n", |
| 213 | PyThread_get_thread_ident(), e)); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 214 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | /* I've seen errno == EAGAIN here, which means "there are |
| 216 | * too many threads". |
| 217 | */ |
| 218 | int e = errno; |
| 219 | dprintf(("%ld: PyThread_start_new_thread failed, errno %d\n", |
| 220 | PyThread_get_thread_ident(), e)); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 221 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | threadID = (unsigned)-1; |
| 223 | HeapFree(GetProcessHeap(), 0, obj); |
| 224 | } |
| 225 | else { |
| 226 | dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n", |
| 227 | PyThread_get_thread_ident(), (void*)hThread)); |
| 228 | CloseHandle(hThread); |
| 229 | } |
| 230 | return (long) threadID; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Return the thread Id instead of an handle. The Id is said to uniquely identify the |
| 235 | * thread in the system |
| 236 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 237 | long |
| 238 | PyThread_get_thread_ident(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | if (!initialized) |
| 241 | PyThread_init_thread(); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | return GetCurrentThreadId(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 246 | void |
| 247 | PyThread_exit_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 248 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
| 250 | if (!initialized) |
| 251 | exit(0); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 252 | #if defined(MS_WINCE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | ExitThread(0); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 254 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | _endthreadex(0); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 256 | #endif |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 259 | /* |
| 260 | * Lock support. It has too be implemented as semaphores. |
| 261 | * I [Dag] tried to implement it with mutex but I could find a way to |
| 262 | * tell whether a thread already own the lock or not. |
| 263 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 264 | PyThread_type_lock |
| 265 | PyThread_allocate_lock(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 266 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | PNRMUTEX aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | dprintf(("PyThread_allocate_lock called\n")); |
| 270 | if (!initialized) |
| 271 | PyThread_init_thread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | aLock = AllocNonRecursiveMutex() ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | 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] | 276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | return (PyThread_type_lock) aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 280 | void |
| 281 | PyThread_free_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | 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] | 284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | */ |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 294 | PyLockStatus |
| 295 | PyThread_acquire_lock_timed(PyThread_type_lock aLock, |
| 296 | PY_TIMEOUT_T microseconds, int intr_flag) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 297 | { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 298 | /* Fow now, intr_flag does nothing on Windows, and lock acquires are |
| 299 | * uninterruptible. */ |
| 300 | PyLockStatus success; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | PY_TIMEOUT_T milliseconds; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | if (microseconds >= 0) { |
| 304 | milliseconds = microseconds / 1000; |
| 305 | if (microseconds % 1000 > 0) |
| 306 | ++milliseconds; |
| 307 | if ((DWORD) milliseconds != milliseconds) |
| 308 | Py_FatalError("Timeout too large for a DWORD, " |
| 309 | "please check PY_TIMEOUT_MAX"); |
| 310 | } |
| 311 | else |
| 312 | milliseconds = INFINITE; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | dprintf(("%ld: PyThread_acquire_lock_timed(%p, %lld) called\n", |
| 315 | PyThread_get_thread_ident(), aLock, microseconds)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 316 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 317 | if (aLock && EnterNonRecursiveMutex((PNRMUTEX)aLock, |
| 318 | (DWORD)milliseconds) == WAIT_OBJECT_0) { |
| 319 | success = PY_LOCK_ACQUIRED; |
| 320 | } |
| 321 | else { |
| 322 | success = PY_LOCK_FAILURE; |
| 323 | } |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | dprintf(("%ld: PyThread_acquire_lock(%p, %lld) -> %d\n", |
| 326 | PyThread_get_thread_ident(), aLock, microseconds, success)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | return success; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 329 | } |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 330 | int |
| 331 | PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
| 332 | { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 333 | return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0); |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 334 | } |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 335 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 336 | void |
| 337 | PyThread_release_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | 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] | 340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock))) |
| 342 | dprintf(("%ld: Could not PyThread_release_lock(%p) error: %ld\n", PyThread_get_thread_ident(), aLock, GetLastError())); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 343 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 344 | |
| 345 | /* minimum/maximum thread stack sizes supported */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | #define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */ |
| 347 | #define THREAD_MAX_STACKSIZE 0x10000000 /* 256MB */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 348 | |
| 349 | /* set the thread stack size. |
| 350 | * Return 0 if size is valid, -1 otherwise. |
| 351 | */ |
| 352 | static int |
| 353 | _pythread_nt_set_stacksize(size_t size) |
| 354 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | /* set to default */ |
| 356 | if (size == 0) { |
| 357 | _pythread_stacksize = 0; |
| 358 | return 0; |
| 359 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | /* valid range? */ |
| 362 | if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) { |
| 363 | _pythread_stacksize = size; |
| 364 | return 0; |
| 365 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | #define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x) |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 371 | |
| 372 | |
| 373 | /* use native Windows TLS functions */ |
| 374 | #define Py_HAVE_NATIVE_TLS |
| 375 | |
| 376 | #ifdef Py_HAVE_NATIVE_TLS |
| 377 | int |
| 378 | PyThread_create_key(void) |
| 379 | { |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 380 | DWORD result= TlsAlloc(); |
| 381 | if (result == TLS_OUT_OF_INDEXES) |
| 382 | return -1; |
| 383 | return (int)result; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void |
| 387 | PyThread_delete_key(int key) |
| 388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | TlsFree(key); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /* We must be careful to emulate the strange semantics implemented in thread.c, |
| 393 | * where the value is only set if it hasn't been set before. |
| 394 | */ |
| 395 | int |
| 396 | PyThread_set_key_value(int key, void *value) |
| 397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | BOOL ok; |
| 399 | void *oldvalue; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 400 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | assert(value != NULL); |
| 402 | oldvalue = TlsGetValue(key); |
| 403 | if (oldvalue != NULL) |
| 404 | /* ignore value if already set */ |
| 405 | return 0; |
| 406 | ok = TlsSetValue(key, value); |
| 407 | if (!ok) |
| 408 | return -1; |
| 409 | return 0; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void * |
| 413 | PyThread_get_key_value(int key) |
| 414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | /* because TLS is used in the Py_END_ALLOW_THREAD macro, |
| 416 | * it is necessary to preserve the windows error state, because |
| 417 | * it is assumed to be preserved across the call to the macro. |
| 418 | * Ideally, the macro should be fixed, but it is simpler to |
| 419 | * do it here. |
| 420 | */ |
| 421 | DWORD error = GetLastError(); |
| 422 | void *result = TlsGetValue(key); |
| 423 | SetLastError(error); |
| 424 | return result; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void |
| 428 | PyThread_delete_key_value(int key) |
| 429 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | /* NULL is used as "key missing", and it is also the default |
| 431 | * given by TlsGetValue() if nothing has been set yet. |
| 432 | */ |
| 433 | TlsSetValue(key, NULL); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* reinitialization of TLS is not necessary after fork when using |
| 437 | * the native TLS functions. And forking isn't supported on Windows either. |
| 438 | */ |
| 439 | void |
| 440 | PyThread_ReInitTLS(void) |
| 441 | {} |
| 442 | |
| 443 | #endif |