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