Victor Stinner | 4a3fe08 | 2020-04-14 14:26:24 +0200 | [diff] [blame] | 1 | #include "pycore_interp.h" // _PyInterpreterState.pythread_stacksize |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 2 | |
| 3 | /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */ |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 4 | /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */ |
Tim Peters | e64ef93 | 2002-02-28 21:34:34 +0000 | [diff] [blame] | 5 | /* Eliminated some memory leaks, gsw@agere.com */ |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 7 | #include <windows.h> |
| 8 | #include <limits.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 10 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11 | #endif |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 12 | |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 13 | /* options */ |
| 14 | #ifndef _PY_USE_CV_LOCKS |
| 15 | #define _PY_USE_CV_LOCKS 1 /* use locks based on cond vars */ |
| 16 | #endif |
| 17 | |
| 18 | /* Now, define a non-recursive mutex using either condition variables |
| 19 | * and critical sections (fast) or using operating system mutexes |
| 20 | * (slow) |
| 21 | */ |
| 22 | |
| 23 | #if _PY_USE_CV_LOCKS |
| 24 | |
| 25 | #include "condvar.h" |
| 26 | |
| 27 | typedef struct _NRMUTEX |
| 28 | { |
| 29 | PyMUTEX_T cs; |
| 30 | PyCOND_T cv; |
| 31 | int locked; |
| 32 | } NRMUTEX; |
| 33 | typedef NRMUTEX *PNRMUTEX; |
| 34 | |
| 35 | PNRMUTEX |
| 36 | AllocNonRecursiveMutex() |
| 37 | { |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 38 | PNRMUTEX m = (PNRMUTEX)PyMem_RawMalloc(sizeof(NRMUTEX)); |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 39 | if (!m) |
| 40 | return NULL; |
| 41 | if (PyCOND_INIT(&m->cv)) |
| 42 | goto fail; |
| 43 | if (PyMUTEX_INIT(&m->cs)) { |
| 44 | PyCOND_FINI(&m->cv); |
| 45 | goto fail; |
| 46 | } |
| 47 | m->locked = 0; |
| 48 | return m; |
| 49 | fail: |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 50 | PyMem_RawFree(m); |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | VOID |
| 55 | FreeNonRecursiveMutex(PNRMUTEX mutex) |
| 56 | { |
| 57 | if (mutex) { |
| 58 | PyCOND_FINI(&mutex->cv); |
| 59 | PyMUTEX_FINI(&mutex->cs); |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 60 | PyMem_RawFree(mutex); |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | DWORD |
| 65 | EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds) |
| 66 | { |
| 67 | DWORD result = WAIT_OBJECT_0; |
| 68 | if (PyMUTEX_LOCK(&mutex->cs)) |
| 69 | return WAIT_FAILED; |
| 70 | if (milliseconds == INFINITE) { |
| 71 | while (mutex->locked) { |
| 72 | if (PyCOND_WAIT(&mutex->cv, &mutex->cs)) { |
| 73 | result = WAIT_FAILED; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | } else if (milliseconds != 0) { |
| 78 | /* wait at least until the target */ |
Miss Islington (bot) | 8673b77 | 2021-06-20 13:12:27 -0700 | [diff] [blame] | 79 | _PyTime_t now = _PyTime_GetPerfCounter(); |
| 80 | if (now <= 0) { |
| 81 | Py_FatalError("_PyTime_GetPerfCounter() == 0"); |
| 82 | } |
| 83 | _PyTime_t nanoseconds = _PyTime_FromNanoseconds((_PyTime_t)milliseconds * 1000000); |
| 84 | _PyTime_t target = now + nanoseconds; |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 85 | while (mutex->locked) { |
Miss Islington (bot) | 8673b77 | 2021-06-20 13:12:27 -0700 | [diff] [blame] | 86 | _PyTime_t microseconds = _PyTime_AsMicroseconds(nanoseconds, _PyTime_ROUND_TIMEOUT); |
| 87 | if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, microseconds) < 0) { |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 88 | result = WAIT_FAILED; |
| 89 | break; |
| 90 | } |
Miss Islington (bot) | 8673b77 | 2021-06-20 13:12:27 -0700 | [diff] [blame] | 91 | now = _PyTime_GetPerfCounter(); |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 92 | if (target <= now) |
| 93 | break; |
Miss Islington (bot) | 8673b77 | 2021-06-20 13:12:27 -0700 | [diff] [blame] | 94 | nanoseconds = target - now; |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | if (!mutex->locked) { |
| 98 | mutex->locked = 1; |
| 99 | result = WAIT_OBJECT_0; |
| 100 | } else if (result == WAIT_OBJECT_0) |
| 101 | result = WAIT_TIMEOUT; |
| 102 | /* else, it is WAIT_FAILED */ |
| 103 | PyMUTEX_UNLOCK(&mutex->cs); /* must ignore result here */ |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | BOOL |
| 108 | LeaveNonRecursiveMutex(PNRMUTEX mutex) |
| 109 | { |
| 110 | BOOL result; |
| 111 | if (PyMUTEX_LOCK(&mutex->cs)) |
| 112 | return FALSE; |
| 113 | mutex->locked = 0; |
native-api | 05e9221 | 2019-02-02 19:22:55 +0300 | [diff] [blame] | 114 | /* condvar APIs return 0 on success. We need to return TRUE on success. */ |
| 115 | result = !PyCOND_SIGNAL(&mutex->cv); |
| 116 | PyMUTEX_UNLOCK(&mutex->cs); |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 117 | return result; |
Victor Stinner | 80aa565 | 2013-07-07 17:17:59 +0200 | [diff] [blame] | 118 | } |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 119 | |
| 120 | #else /* if ! _PY_USE_CV_LOCKS */ |
| 121 | |
| 122 | /* NR-locks based on a kernel mutex */ |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 123 | #define PNRMUTEX HANDLE |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 124 | |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 125 | PNRMUTEX |
| 126 | AllocNonRecursiveMutex() |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 127 | { |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 128 | return CreateSemaphore(NULL, 1, 1, NULL); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 131 | VOID |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 132 | FreeNonRecursiveMutex(PNRMUTEX mutex) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | /* No in-use check */ |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 135 | CloseHandle(mutex); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 138 | DWORD |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 139 | EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 140 | { |
Martin v. Löwis | b26a9b1 | 2013-01-25 14:25:48 +0100 | [diff] [blame] | 141 | return WaitForSingleObjectEx(mutex, milliseconds, FALSE); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 144 | BOOL |
| 145 | LeaveNonRecursiveMutex(PNRMUTEX mutex) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 146 | { |
Antoine Pitrou | 7899acf | 2011-03-31 01:00:32 +0200 | [diff] [blame] | 147 | return ReleaseSemaphore(mutex, 1, NULL); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 148 | } |
Kristján Valur Jónsson | e75ff35 | 2012-06-18 20:30:44 +0000 | [diff] [blame] | 149 | #endif /* _PY_USE_CV_LOCKS */ |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 150 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 151 | unsigned long PyThread_get_thread_ident(void); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 152 | |
Jake Tesler | b121f63 | 2019-05-22 08:43:17 -0700 | [diff] [blame] | 153 | #ifdef PY_HAVE_THREAD_NATIVE_ID |
| 154 | unsigned long PyThread_get_thread_native_id(void); |
| 155 | #endif |
| 156 | |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 157 | /* |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 158 | * Initialization of the C package, should not be needed. |
| 159 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 160 | static void |
| 161 | PyThread__init_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 162 | { |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Thread support. |
| 167 | */ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 168 | |
| 169 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | void (*func)(void*); |
| 171 | void *arg; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 172 | } callobj; |
| 173 | |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 174 | /* thunker to call adapt between the function type used by the system's |
| 175 | thread start function and the internally used one. */ |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 176 | static unsigned __stdcall |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 177 | bootstrap(void *call) |
| 178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | callobj *obj = (callobj*)call; |
| 180 | void (*func)(void*) = obj->func; |
| 181 | void *arg = obj->arg; |
| 182 | HeapFree(GetProcessHeap(), 0, obj); |
| 183 | func(arg); |
| 184 | return 0; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 187 | unsigned long |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 188 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 189 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | HANDLE hThread; |
| 191 | unsigned threadID; |
| 192 | callobj *obj; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 193 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 194 | dprintf(("%lu: PyThread_start_new_thread called\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | PyThread_get_thread_ident())); |
| 196 | if (!initialized) |
| 197 | PyThread_init_thread(); |
| 198 | |
| 199 | obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj)); |
| 200 | if (!obj) |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 201 | return PYTHREAD_INVALID_THREAD_ID; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | obj->func = func; |
| 203 | obj->arg = arg; |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 204 | PyThreadState *tstate = _PyThreadState_GET(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 205 | size_t stacksize = tstate ? tstate->interp->pythread_stacksize : 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | hThread = (HANDLE)_beginthreadex(0, |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 207 | Py_SAFE_DOWNCAST(stacksize, Py_ssize_t, unsigned int), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | bootstrap, obj, |
| 209 | 0, &threadID); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | if (hThread == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | /* I've seen errno == EAGAIN here, which means "there are |
| 212 | * too many threads". |
| 213 | */ |
| 214 | int e = errno; |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 215 | dprintf(("%lu: PyThread_start_new_thread failed, errno %d\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | PyThread_get_thread_ident(), e)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | threadID = (unsigned)-1; |
| 218 | HeapFree(GetProcessHeap(), 0, obj); |
| 219 | } |
| 220 | else { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 221 | dprintf(("%lu: PyThread_start_new_thread succeeded: %p\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | PyThread_get_thread_ident(), (void*)hThread)); |
| 223 | CloseHandle(hThread); |
| 224 | } |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 225 | return threadID; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 229 | * 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] | 230 | * thread in the system |
| 231 | */ |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 232 | unsigned long |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 233 | PyThread_get_thread_ident(void) |
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 | if (!initialized) |
| 236 | PyThread_init_thread(); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | return GetCurrentThreadId(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Jake Tesler | b121f63 | 2019-05-22 08:43:17 -0700 | [diff] [blame] | 241 | #ifdef PY_HAVE_THREAD_NATIVE_ID |
| 242 | /* |
| 243 | * Return the native Thread ID (TID) of the calling thread. |
| 244 | * The native ID of a thread is valid and guaranteed to be unique system-wide |
| 245 | * from the time the thread is created until the thread has been terminated. |
| 246 | */ |
| 247 | unsigned long |
| 248 | PyThread_get_thread_native_id(void) |
| 249 | { |
| 250 | if (!initialized) { |
| 251 | PyThread_init_thread(); |
| 252 | } |
| 253 | |
| 254 | DWORD native_id; |
| 255 | native_id = GetCurrentThreadId(); |
| 256 | return (unsigned long) native_id; |
| 257 | } |
| 258 | #endif |
| 259 | |
Victor Stinner | c664b34 | 2019-05-04 11:48:05 -0400 | [diff] [blame] | 260 | void _Py_NO_RETURN |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 261 | PyThread_exit_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 262 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 263 | dprintf(("%lu: PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | if (!initialized) |
| 265 | exit(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | _endthreadex(0); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 269 | /* |
hui shang | 6027802 | 2018-01-18 07:21:01 +0800 | [diff] [blame] | 270 | * Lock support. It has to be implemented as semaphores. |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 271 | * I [Dag] tried to implement it with mutex but I could find a way to |
| 272 | * tell whether a thread already own the lock or not. |
| 273 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 274 | PyThread_type_lock |
| 275 | PyThread_allocate_lock(void) |
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 | PNRMUTEX aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | dprintf(("PyThread_allocate_lock called\n")); |
| 280 | if (!initialized) |
| 281 | PyThread_init_thread(); |
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 | aLock = AllocNonRecursiveMutex() ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 284 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 285 | 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] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | return (PyThread_type_lock) aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 290 | void |
| 291 | PyThread_free_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 292 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 293 | 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] | 294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | FreeNonRecursiveMutex(aLock) ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Return 1 on success if the lock was acquired |
| 300 | * |
| 301 | * and 0 if the lock was not acquired. This means a 0 is returned |
| 302 | * if the lock has already been acquired by this thread! |
| 303 | */ |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 304 | PyLockStatus |
| 305 | PyThread_acquire_lock_timed(PyThread_type_lock aLock, |
| 306 | PY_TIMEOUT_T microseconds, int intr_flag) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 307 | { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 308 | /* Fow now, intr_flag does nothing on Windows, and lock acquires are |
| 309 | * uninterruptible. */ |
| 310 | PyLockStatus success; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | PY_TIMEOUT_T milliseconds; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | if (microseconds >= 0) { |
| 314 | milliseconds = microseconds / 1000; |
| 315 | if (microseconds % 1000 > 0) |
| 316 | ++milliseconds; |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 317 | if (milliseconds > PY_DWORD_MAX) { |
| 318 | Py_FatalError("Timeout larger than PY_TIMEOUT_MAX"); |
| 319 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | } |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 321 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | milliseconds = INFINITE; |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 323 | } |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 324 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 325 | dprintf(("%lu: PyThread_acquire_lock_timed(%p, %lld) called\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | PyThread_get_thread_ident(), aLock, microseconds)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 328 | if (aLock && EnterNonRecursiveMutex((PNRMUTEX)aLock, |
| 329 | (DWORD)milliseconds) == WAIT_OBJECT_0) { |
| 330 | success = PY_LOCK_ACQUIRED; |
| 331 | } |
| 332 | else { |
| 333 | success = PY_LOCK_FAILURE; |
| 334 | } |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 335 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 336 | dprintf(("%lu: PyThread_acquire_lock(%p, %lld) -> %d\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | PyThread_get_thread_ident(), aLock, microseconds, success)); |
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 | return success; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 340 | } |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 341 | int |
| 342 | PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
| 343 | { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 344 | return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0); |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 345 | } |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 346 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 347 | void |
| 348 | PyThread_release_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 349 | { |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 350 | 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] | 351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock))) |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 353 | 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] | 354 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 355 | |
| 356 | /* minimum/maximum thread stack sizes supported */ |
Victor Stinner | 8c663fd | 2017-11-08 14:44:44 -0800 | [diff] [blame] | 357 | #define THREAD_MIN_STACKSIZE 0x8000 /* 32 KiB */ |
| 358 | #define THREAD_MAX_STACKSIZE 0x10000000 /* 256 MiB */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 359 | |
| 360 | /* set the thread stack size. |
| 361 | * Return 0 if size is valid, -1 otherwise. |
| 362 | */ |
| 363 | static int |
| 364 | _pythread_nt_set_stacksize(size_t size) |
| 365 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | /* set to default */ |
| 367 | if (size == 0) { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 368 | _PyInterpreterState_GET()->pythread_stacksize = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | return 0; |
| 370 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | /* valid range? */ |
| 373 | if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 374 | _PyInterpreterState_GET()->pythread_stacksize = size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | return 0; |
| 376 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | #define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x) |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 382 | |
| 383 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 384 | /* Thread Local Storage (TLS) API |
| 385 | |
| 386 | This API is DEPRECATED since Python 3.7. See PEP 539 for details. |
| 387 | */ |
| 388 | |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 389 | int |
| 390 | PyThread_create_key(void) |
| 391 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 392 | DWORD result = TlsAlloc(); |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 393 | if (result == TLS_OUT_OF_INDEXES) |
| 394 | return -1; |
| 395 | return (int)result; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void |
| 399 | PyThread_delete_key(int key) |
| 400 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | TlsFree(key); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 404 | int |
| 405 | PyThread_set_key_value(int key, void *value) |
| 406 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 407 | BOOL ok = TlsSetValue(key, value); |
| 408 | return ok ? 0 : -1; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void * |
| 412 | PyThread_get_key_value(int key) |
| 413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | /* because TLS is used in the Py_END_ALLOW_THREAD macro, |
| 415 | * it is necessary to preserve the windows error state, because |
| 416 | * it is assumed to be preserved across the call to the macro. |
| 417 | * Ideally, the macro should be fixed, but it is simpler to |
| 418 | * do it here. |
| 419 | */ |
| 420 | DWORD error = GetLastError(); |
| 421 | void *result = TlsGetValue(key); |
| 422 | SetLastError(error); |
| 423 | return result; |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void |
| 427 | PyThread_delete_key_value(int key) |
| 428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | /* NULL is used as "key missing", and it is also the default |
| 430 | * given by TlsGetValue() if nothing has been set yet. |
| 431 | */ |
| 432 | TlsSetValue(key, NULL); |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 435 | |
Martin v. Löwis | 7c2b66c | 2009-01-12 08:21:03 +0000 | [diff] [blame] | 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) |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 441 | { |
| 442 | } |
| 443 | |
| 444 | |
| 445 | /* Thread Specific Storage (TSS) API |
| 446 | |
| 447 | Platform-specific components of TSS API implementation. |
| 448 | */ |
| 449 | |
| 450 | int |
| 451 | PyThread_tss_create(Py_tss_t *key) |
| 452 | { |
| 453 | assert(key != NULL); |
| 454 | /* If the key has been created, function is silently skipped. */ |
| 455 | if (key->_is_initialized) { |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | DWORD result = TlsAlloc(); |
| 460 | if (result == TLS_OUT_OF_INDEXES) { |
| 461 | return -1; |
| 462 | } |
| 463 | /* In Windows, platform-specific key type is DWORD. */ |
| 464 | key->_key = result; |
| 465 | key->_is_initialized = 1; |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | void |
| 470 | PyThread_tss_delete(Py_tss_t *key) |
| 471 | { |
| 472 | assert(key != NULL); |
| 473 | /* If the key has not been created, function is silently skipped. */ |
| 474 | if (!key->_is_initialized) { |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | TlsFree(key->_key); |
| 479 | key->_key = TLS_OUT_OF_INDEXES; |
| 480 | key->_is_initialized = 0; |
| 481 | } |
| 482 | |
| 483 | int |
| 484 | PyThread_tss_set(Py_tss_t *key, void *value) |
| 485 | { |
| 486 | assert(key != NULL); |
| 487 | BOOL ok = TlsSetValue(key->_key, value); |
| 488 | return ok ? 0 : -1; |
| 489 | } |
| 490 | |
| 491 | void * |
| 492 | PyThread_tss_get(Py_tss_t *key) |
| 493 | { |
| 494 | assert(key != NULL); |
| 495 | /* because TSS is used in the Py_END_ALLOW_THREAD macro, |
| 496 | * it is necessary to preserve the windows error state, because |
| 497 | * it is assumed to be preserved across the call to the macro. |
| 498 | * Ideally, the macro should be fixed, but it is simpler to |
| 499 | * do it here. |
| 500 | */ |
| 501 | DWORD error = GetLastError(); |
| 502 | void *result = TlsGetValue(key->_key); |
| 503 | SetLastError(error); |
| 504 | return result; |
| 505 | } |