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 | { |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 37 | PNRMUTEX m = (PNRMUTEX)PyMem_RawMalloc(sizeof(NRMUTEX)); |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 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: |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 49 | PyMem_RawFree(m); |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 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); |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 59 | PyMem_RawFree(mutex); |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 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) { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 80 | if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, (long long)milliseconds*1000) < 0) { |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 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; |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 110 | } |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 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 | { |
Martin v. Löwis | b26a9b1 | 2013-01-25 14:25:48 +0100 | [diff] [blame] | 133 | return WaitForSingleObjectEx(mutex, milliseconds, FALSE); |
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 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 143 | unsigned 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. */ |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 164 | static unsigned __stdcall |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 165 | bootstrap(void *call) |
| 166 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | callobj *obj = (callobj*)call; |
| 168 | void (*func)(void*) = obj->func; |
| 169 | void *arg = obj->arg; |
| 170 | HeapFree(GetProcessHeap(), 0, obj); |
| 171 | func(arg); |
| 172 | return 0; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 175 | unsigned long |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 176 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 177 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | HANDLE hThread; |
| 179 | unsigned threadID; |
| 180 | callobj *obj; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 181 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 182 | dprintf(("%lu: PyThread_start_new_thread called\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 183 | PyThread_get_thread_ident())); |
| 184 | if (!initialized) |
| 185 | PyThread_init_thread(); |
| 186 | |
| 187 | obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj)); |
| 188 | if (!obj) |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 189 | return PYTHREAD_INVALID_THREAD_ID; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | obj->func = func; |
| 191 | obj->arg = arg; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 192 | PyThreadState *tstate = PyThreadState_GET(); |
| 193 | size_t stacksize = tstate ? tstate->interp->pythread_stacksize : 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | hThread = (HANDLE)_beginthreadex(0, |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 195 | Py_SAFE_DOWNCAST(stacksize, Py_ssize_t, unsigned int), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | bootstrap, obj, |
| 197 | 0, &threadID); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | if (hThread == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | /* I've seen errno == EAGAIN here, which means "there are |
| 200 | * too many threads". |
| 201 | */ |
| 202 | int e = errno; |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 203 | dprintf(("%lu: PyThread_start_new_thread failed, errno %d\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | PyThread_get_thread_ident(), e)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | threadID = (unsigned)-1; |
| 206 | HeapFree(GetProcessHeap(), 0, obj); |
| 207 | } |
| 208 | else { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 209 | dprintf(("%lu: PyThread_start_new_thread succeeded: %p\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | PyThread_get_thread_ident(), (void*)hThread)); |
| 211 | CloseHandle(hThread); |
| 212 | } |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 213 | return threadID; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 217 | * Return the thread Id instead of a handle. The Id is said to uniquely identify the |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 218 | * thread in the system |
| 219 | */ |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 220 | unsigned long |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 221 | PyThread_get_thread_ident(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | if (!initialized) |
| 224 | PyThread_init_thread(); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | return GetCurrentThreadId(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 229 | void |
| 230 | PyThread_exit_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 231 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 232 | dprintf(("%lu: PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | if (!initialized) |
| 234 | exit(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | _endthreadex(0); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 238 | /* |
hui shang | 6027802 | 2018-01-18 07:21:01 +0800 | [diff] [blame] | 239 | * Lock support. It has to be implemented as semaphores. |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 240 | * I [Dag] tried to implement it with mutex but I could find a way to |
| 241 | * tell whether a thread already own the lock or not. |
| 242 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 243 | PyThread_type_lock |
| 244 | PyThread_allocate_lock(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | PNRMUTEX aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | dprintf(("PyThread_allocate_lock called\n")); |
| 249 | if (!initialized) |
| 250 | PyThread_init_thread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | aLock = AllocNonRecursiveMutex() ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 253 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 254 | dprintf(("%lu: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | return (PyThread_type_lock) aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 259 | void |
| 260 | PyThread_free_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 261 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 262 | dprintf(("%lu: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | FreeNonRecursiveMutex(aLock) ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Return 1 on success if the lock was acquired |
| 269 | * |
| 270 | * and 0 if the lock was not acquired. This means a 0 is returned |
| 271 | * if the lock has already been acquired by this thread! |
| 272 | */ |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 273 | PyLockStatus |
| 274 | PyThread_acquire_lock_timed(PyThread_type_lock aLock, |
| 275 | PY_TIMEOUT_T microseconds, int intr_flag) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 276 | { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 277 | /* Fow now, intr_flag does nothing on Windows, and lock acquires are |
| 278 | * uninterruptible. */ |
| 279 | PyLockStatus success; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | PY_TIMEOUT_T milliseconds; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | if (microseconds >= 0) { |
| 283 | milliseconds = microseconds / 1000; |
| 284 | if (microseconds % 1000 > 0) |
| 285 | ++milliseconds; |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 286 | if (milliseconds > PY_DWORD_MAX) { |
| 287 | Py_FatalError("Timeout larger than PY_TIMEOUT_MAX"); |
| 288 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | } |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 290 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | milliseconds = INFINITE; |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 292 | } |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 293 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 294 | dprintf(("%lu: PyThread_acquire_lock_timed(%p, %lld) called\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | PyThread_get_thread_ident(), aLock, microseconds)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 296 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 297 | if (aLock && EnterNonRecursiveMutex((PNRMUTEX)aLock, |
| 298 | (DWORD)milliseconds) == WAIT_OBJECT_0) { |
| 299 | success = PY_LOCK_ACQUIRED; |
| 300 | } |
| 301 | else { |
| 302 | success = PY_LOCK_FAILURE; |
| 303 | } |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 304 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 305 | dprintf(("%lu: PyThread_acquire_lock(%p, %lld) -> %d\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | PyThread_get_thread_ident(), aLock, microseconds, success)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | return success; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 309 | } |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 310 | int |
| 311 | PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
| 312 | { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 313 | return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0); |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 314 | } |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 315 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 316 | void |
| 317 | PyThread_release_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 318 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 319 | dprintf(("%lu: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock))) |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 322 | dprintf(("%lu: 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] | 323 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 324 | |
| 325 | /* minimum/maximum thread stack sizes supported */ |
Victor Stinner | 8c663fd | 2017-11-08 14:44:44 -0800 | [diff] [blame] | 326 | #define THREAD_MIN_STACKSIZE 0x8000 /* 32 KiB */ |
| 327 | #define THREAD_MAX_STACKSIZE 0x10000000 /* 256 MiB */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 328 | |
| 329 | /* set the thread stack size. |
| 330 | * Return 0 if size is valid, -1 otherwise. |
| 331 | */ |
| 332 | static int |
| 333 | _pythread_nt_set_stacksize(size_t size) |
| 334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | /* set to default */ |
| 336 | if (size == 0) { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 337 | PyThreadState_GET()->interp->pythread_stacksize = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | return 0; |
| 339 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | /* valid range? */ |
| 342 | if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 343 | PyThreadState_GET()->interp->pythread_stacksize = size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | return 0; |
| 345 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | #define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x) |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 351 | |
| 352 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 353 | /* Thread Local Storage (TLS) API |
| 354 | |
| 355 | This API is DEPRECATED since Python 3.7. See PEP 539 for details. |
| 356 | */ |
| 357 | |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 358 | int |
| 359 | PyThread_create_key(void) |
| 360 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 361 | DWORD result = TlsAlloc(); |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 362 | if (result == TLS_OUT_OF_INDEXES) |
| 363 | return -1; |
| 364 | return (int)result; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void |
| 368 | PyThread_delete_key(int key) |
| 369 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | TlsFree(key); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 373 | int |
| 374 | PyThread_set_key_value(int key, void *value) |
| 375 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 376 | BOOL ok = TlsSetValue(key, value); |
| 377 | return ok ? 0 : -1; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void * |
| 381 | PyThread_get_key_value(int key) |
| 382 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | /* because TLS is used in the Py_END_ALLOW_THREAD macro, |
| 384 | * it is necessary to preserve the windows error state, because |
| 385 | * it is assumed to be preserved across the call to the macro. |
| 386 | * Ideally, the macro should be fixed, but it is simpler to |
| 387 | * do it here. |
| 388 | */ |
| 389 | DWORD error = GetLastError(); |
| 390 | void *result = TlsGetValue(key); |
| 391 | SetLastError(error); |
| 392 | return result; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void |
| 396 | PyThread_delete_key_value(int key) |
| 397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | /* NULL is used as "key missing", and it is also the default |
| 399 | * given by TlsGetValue() if nothing has been set yet. |
| 400 | */ |
| 401 | TlsSetValue(key, NULL); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 404 | |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 405 | /* reinitialization of TLS is not necessary after fork when using |
| 406 | * the native TLS functions. And forking isn't supported on Windows either. |
| 407 | */ |
| 408 | void |
| 409 | PyThread_ReInitTLS(void) |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 410 | { |
| 411 | } |
| 412 | |
| 413 | |
| 414 | /* Thread Specific Storage (TSS) API |
| 415 | |
| 416 | Platform-specific components of TSS API implementation. |
| 417 | */ |
| 418 | |
| 419 | int |
| 420 | PyThread_tss_create(Py_tss_t *key) |
| 421 | { |
| 422 | assert(key != NULL); |
| 423 | /* If the key has been created, function is silently skipped. */ |
| 424 | if (key->_is_initialized) { |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | DWORD result = TlsAlloc(); |
| 429 | if (result == TLS_OUT_OF_INDEXES) { |
| 430 | return -1; |
| 431 | } |
| 432 | /* In Windows, platform-specific key type is DWORD. */ |
| 433 | key->_key = result; |
| 434 | key->_is_initialized = 1; |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | void |
| 439 | PyThread_tss_delete(Py_tss_t *key) |
| 440 | { |
| 441 | assert(key != NULL); |
| 442 | /* If the key has not been created, function is silently skipped. */ |
| 443 | if (!key->_is_initialized) { |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | TlsFree(key->_key); |
| 448 | key->_key = TLS_OUT_OF_INDEXES; |
| 449 | key->_is_initialized = 0; |
| 450 | } |
| 451 | |
| 452 | int |
| 453 | PyThread_tss_set(Py_tss_t *key, void *value) |
| 454 | { |
| 455 | assert(key != NULL); |
| 456 | BOOL ok = TlsSetValue(key->_key, value); |
| 457 | return ok ? 0 : -1; |
| 458 | } |
| 459 | |
| 460 | void * |
| 461 | PyThread_tss_get(Py_tss_t *key) |
| 462 | { |
| 463 | assert(key != NULL); |
| 464 | /* because TSS is used in the Py_END_ALLOW_THREAD macro, |
| 465 | * it is necessary to preserve the windows error state, because |
| 466 | * it is assumed to be preserved across the call to the macro. |
| 467 | * Ideally, the macro should be fixed, but it is simpler to |
| 468 | * do it here. |
| 469 | */ |
| 470 | DWORD error = GetLastError(); |
| 471 | void *result = TlsGetValue(key->_key); |
| 472 | SetLastError(error); |
| 473 | return result; |
| 474 | } |