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 | |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 12 | #define PNRMUTEX HANDLE |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 13 | |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 14 | PNRMUTEX |
| 15 | AllocNonRecursiveMutex() |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 16 | { |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 17 | return CreateSemaphore(NULL, 1, 1, NULL); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 18 | } |
| 19 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 20 | VOID |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 21 | FreeNonRecursiveMutex(PNRMUTEX mutex) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 22 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 23 | /* No in-use check */ |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 24 | CloseHandle(mutex); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 27 | DWORD |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 28 | EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 29 | { |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 30 | return WaitForSingleObject(mutex, milliseconds); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 33 | BOOL |
| 34 | LeaveNonRecursiveMutex(PNRMUTEX mutex) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 35 | { |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 36 | return ReleaseSemaphore(mutex, 1, NULL); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 39 | long PyThread_get_thread_ident(void); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 40 | |
| 41 | /* |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 42 | * Initialization of the C package, should not be needed. |
| 43 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 44 | static void |
| 45 | PyThread__init_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Thread support. |
| 51 | */ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 52 | |
| 53 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | void (*func)(void*); |
| 55 | void *arg; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 56 | } callobj; |
| 57 | |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 58 | /* thunker to call adapt between the function type used by the system's |
| 59 | thread start function and the internally used one. */ |
| 60 | #if defined(MS_WINCE) |
| 61 | static DWORD WINAPI |
| 62 | #else |
| 63 | static unsigned __stdcall |
| 64 | #endif |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 65 | bootstrap(void *call) |
| 66 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | callobj *obj = (callobj*)call; |
| 68 | void (*func)(void*) = obj->func; |
| 69 | void *arg = obj->arg; |
| 70 | HeapFree(GetProcessHeap(), 0, obj); |
| 71 | func(arg); |
| 72 | return 0; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 75 | long |
| 76 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 77 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | HANDLE hThread; |
| 79 | unsigned threadID; |
| 80 | callobj *obj; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 81 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | dprintf(("%ld: PyThread_start_new_thread called\n", |
| 83 | PyThread_get_thread_ident())); |
| 84 | if (!initialized) |
| 85 | PyThread_init_thread(); |
| 86 | |
| 87 | obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj)); |
| 88 | if (!obj) |
| 89 | return -1; |
| 90 | obj->func = func; |
| 91 | obj->arg = arg; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 92 | #if defined(MS_WINCE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | hThread = CreateThread(NULL, |
| 94 | Py_SAFE_DOWNCAST(_pythread_stacksize, Py_ssize_t, SIZE_T), |
| 95 | bootstrap, obj, 0, &threadID); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 96 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | hThread = (HANDLE)_beginthreadex(0, |
| 98 | Py_SAFE_DOWNCAST(_pythread_stacksize, |
| 99 | Py_ssize_t, unsigned int), |
| 100 | bootstrap, obj, |
| 101 | 0, &threadID); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 102 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | if (hThread == 0) { |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 104 | #if defined(MS_WINCE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | /* Save error in variable, to prevent PyThread_get_thread_ident |
| 106 | from clobbering it. */ |
| 107 | unsigned e = GetLastError(); |
| 108 | dprintf(("%ld: PyThread_start_new_thread failed, win32 error code %u\n", |
| 109 | PyThread_get_thread_ident(), e)); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 110 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | /* I've seen errno == EAGAIN here, which means "there are |
| 112 | * too many threads". |
| 113 | */ |
| 114 | int e = errno; |
| 115 | dprintf(("%ld: PyThread_start_new_thread failed, errno %d\n", |
| 116 | PyThread_get_thread_ident(), e)); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 117 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | threadID = (unsigned)-1; |
| 119 | HeapFree(GetProcessHeap(), 0, obj); |
| 120 | } |
| 121 | else { |
| 122 | dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n", |
| 123 | PyThread_get_thread_ident(), (void*)hThread)); |
| 124 | CloseHandle(hThread); |
| 125 | } |
| 126 | return (long) threadID; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Return the thread Id instead of an handle. The Id is said to uniquely identify the |
| 131 | * thread in the system |
| 132 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 133 | long |
| 134 | PyThread_get_thread_ident(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 135 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | if (!initialized) |
| 137 | PyThread_init_thread(); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | return GetCurrentThreadId(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 142 | void |
| 143 | PyThread_exit_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
| 146 | if (!initialized) |
| 147 | exit(0); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 148 | #if defined(MS_WINCE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | ExitThread(0); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 150 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | _endthreadex(0); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 152 | #endif |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 155 | /* |
| 156 | * Lock support. It has too be implemented as semaphores. |
| 157 | * I [Dag] tried to implement it with mutex but I could find a way to |
| 158 | * tell whether a thread already own the lock or not. |
| 159 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 160 | PyThread_type_lock |
| 161 | PyThread_allocate_lock(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 162 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | PNRMUTEX aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | dprintf(("PyThread_allocate_lock called\n")); |
| 166 | if (!initialized) |
| 167 | PyThread_init_thread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | aLock = AllocNonRecursiveMutex() ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | 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] | 172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 173 | return (PyThread_type_lock) aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 176 | void |
| 177 | PyThread_free_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | 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] | 180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | FreeNonRecursiveMutex(aLock) ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Return 1 on success if the lock was acquired |
| 186 | * |
| 187 | * and 0 if the lock was not acquired. This means a 0 is returned |
| 188 | * if the lock has already been acquired by this thread! |
| 189 | */ |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 190 | PyLockStatus |
| 191 | PyThread_acquire_lock_timed(PyThread_type_lock aLock, |
| 192 | PY_TIMEOUT_T microseconds, int intr_flag) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 193 | { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 194 | /* Fow now, intr_flag does nothing on Windows, and lock acquires are |
| 195 | * uninterruptible. */ |
| 196 | PyLockStatus success; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | PY_TIMEOUT_T milliseconds; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | if (microseconds >= 0) { |
| 200 | milliseconds = microseconds / 1000; |
| 201 | if (microseconds % 1000 > 0) |
| 202 | ++milliseconds; |
| 203 | if ((DWORD) milliseconds != milliseconds) |
| 204 | Py_FatalError("Timeout too large for a DWORD, " |
| 205 | "please check PY_TIMEOUT_MAX"); |
| 206 | } |
| 207 | else |
| 208 | milliseconds = INFINITE; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | dprintf(("%ld: PyThread_acquire_lock_timed(%p, %lld) called\n", |
| 211 | PyThread_get_thread_ident(), aLock, microseconds)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 212 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 213 | if (aLock && EnterNonRecursiveMutex((PNRMUTEX)aLock, |
| 214 | (DWORD)milliseconds) == WAIT_OBJECT_0) { |
| 215 | success = PY_LOCK_ACQUIRED; |
| 216 | } |
| 217 | else { |
| 218 | success = PY_LOCK_FAILURE; |
| 219 | } |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | dprintf(("%ld: PyThread_acquire_lock(%p, %lld) -> %d\n", |
| 222 | PyThread_get_thread_ident(), aLock, microseconds, success)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | return success; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 225 | } |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 226 | int |
| 227 | PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
| 228 | { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 229 | return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0); |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 230 | } |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 231 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 232 | void |
| 233 | PyThread_release_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | 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] | 236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock))) |
| 238 | 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] | 239 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 240 | |
| 241 | /* minimum/maximum thread stack sizes supported */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | #define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */ |
| 243 | #define THREAD_MAX_STACKSIZE 0x10000000 /* 256MB */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 244 | |
| 245 | /* set the thread stack size. |
| 246 | * Return 0 if size is valid, -1 otherwise. |
| 247 | */ |
| 248 | static int |
| 249 | _pythread_nt_set_stacksize(size_t size) |
| 250 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | /* set to default */ |
| 252 | if (size == 0) { |
| 253 | _pythread_stacksize = 0; |
| 254 | return 0; |
| 255 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | /* valid range? */ |
| 258 | if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) { |
| 259 | _pythread_stacksize = size; |
| 260 | return 0; |
| 261 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | #define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x) |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 267 | |
| 268 | |
| 269 | /* use native Windows TLS functions */ |
| 270 | #define Py_HAVE_NATIVE_TLS |
| 271 | |
| 272 | #ifdef Py_HAVE_NATIVE_TLS |
| 273 | int |
| 274 | PyThread_create_key(void) |
| 275 | { |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 276 | DWORD result= TlsAlloc(); |
| 277 | if (result == TLS_OUT_OF_INDEXES) |
| 278 | return -1; |
| 279 | return (int)result; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void |
| 283 | PyThread_delete_key(int key) |
| 284 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | TlsFree(key); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* We must be careful to emulate the strange semantics implemented in thread.c, |
| 289 | * where the value is only set if it hasn't been set before. |
| 290 | */ |
| 291 | int |
| 292 | PyThread_set_key_value(int key, void *value) |
| 293 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | BOOL ok; |
| 295 | void *oldvalue; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 296 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | assert(value != NULL); |
| 298 | oldvalue = TlsGetValue(key); |
| 299 | if (oldvalue != NULL) |
| 300 | /* ignore value if already set */ |
| 301 | return 0; |
| 302 | ok = TlsSetValue(key, value); |
| 303 | if (!ok) |
| 304 | return -1; |
| 305 | return 0; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void * |
| 309 | PyThread_get_key_value(int key) |
| 310 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | /* because TLS is used in the Py_END_ALLOW_THREAD macro, |
| 312 | * it is necessary to preserve the windows error state, because |
| 313 | * it is assumed to be preserved across the call to the macro. |
| 314 | * Ideally, the macro should be fixed, but it is simpler to |
| 315 | * do it here. |
| 316 | */ |
| 317 | DWORD error = GetLastError(); |
| 318 | void *result = TlsGetValue(key); |
| 319 | SetLastError(error); |
| 320 | return result; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void |
| 324 | PyThread_delete_key_value(int key) |
| 325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | /* NULL is used as "key missing", and it is also the default |
| 327 | * given by TlsGetValue() if nothing has been set yet. |
| 328 | */ |
| 329 | TlsSetValue(key, NULL); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* reinitialization of TLS is not necessary after fork when using |
| 333 | * the native TLS functions. And forking isn't supported on Windows either. |
| 334 | */ |
| 335 | void |
| 336 | PyThread_ReInitTLS(void) |
| 337 | {} |
| 338 | |
| 339 | #endif |