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