thread_nt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #include "pycore_interp.h" // _PyInterpreterState.threads.stacksize
  2. /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */
  3. /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */
  4. /* Eliminated some memory leaks, gsw@agere.com */
  5. #include <windows.h>
  6. #include <limits.h>
  7. #ifdef HAVE_PROCESS_H
  8. #include <process.h>
  9. #endif
  10. /* options */
  11. #ifndef _PY_USE_CV_LOCKS
  12. #define _PY_USE_CV_LOCKS 1 /* use locks based on cond vars */
  13. #endif
  14. /* Now, define a non-recursive mutex using either condition variables
  15. * and critical sections (fast) or using operating system mutexes
  16. * (slow)
  17. */
  18. #if _PY_USE_CV_LOCKS
  19. #include "condvar.h"
  20. typedef struct _NRMUTEX
  21. {
  22. PyMUTEX_T cs;
  23. PyCOND_T cv;
  24. int locked;
  25. } NRMUTEX;
  26. typedef NRMUTEX *PNRMUTEX;
  27. static PNRMUTEX
  28. AllocNonRecursiveMutex(void)
  29. {
  30. PNRMUTEX m = (PNRMUTEX)PyMem_RawMalloc(sizeof(NRMUTEX));
  31. if (!m)
  32. return NULL;
  33. if (PyCOND_INIT(&m->cv))
  34. goto fail;
  35. if (PyMUTEX_INIT(&m->cs)) {
  36. PyCOND_FINI(&m->cv);
  37. goto fail;
  38. }
  39. m->locked = 0;
  40. return m;
  41. fail:
  42. PyMem_RawFree(m);
  43. return NULL;
  44. }
  45. static VOID
  46. FreeNonRecursiveMutex(PNRMUTEX mutex)
  47. {
  48. if (mutex) {
  49. PyCOND_FINI(&mutex->cv);
  50. PyMUTEX_FINI(&mutex->cs);
  51. PyMem_RawFree(mutex);
  52. }
  53. }
  54. static DWORD
  55. EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
  56. {
  57. DWORD result = WAIT_OBJECT_0;
  58. if (PyMUTEX_LOCK(&mutex->cs))
  59. return WAIT_FAILED;
  60. if (milliseconds == INFINITE) {
  61. while (mutex->locked) {
  62. if (PyCOND_WAIT(&mutex->cv, &mutex->cs)) {
  63. result = WAIT_FAILED;
  64. break;
  65. }
  66. }
  67. } else if (milliseconds != 0) {
  68. /* wait at least until the deadline */
  69. _PyTime_t nanoseconds = _PyTime_FromNanoseconds((_PyTime_t)milliseconds * 1000000);
  70. _PyTime_t deadline = _PyTime_Add(_PyTime_GetPerfCounter(), nanoseconds);
  71. while (mutex->locked) {
  72. _PyTime_t microseconds = _PyTime_AsMicroseconds(nanoseconds,
  73. _PyTime_ROUND_TIMEOUT);
  74. if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, microseconds) < 0) {
  75. result = WAIT_FAILED;
  76. break;
  77. }
  78. nanoseconds = deadline - _PyTime_GetPerfCounter();
  79. if (nanoseconds <= 0) {
  80. break;
  81. }
  82. }
  83. }
  84. if (!mutex->locked) {
  85. mutex->locked = 1;
  86. result = WAIT_OBJECT_0;
  87. } else if (result == WAIT_OBJECT_0)
  88. result = WAIT_TIMEOUT;
  89. /* else, it is WAIT_FAILED */
  90. PyMUTEX_UNLOCK(&mutex->cs); /* must ignore result here */
  91. return result;
  92. }
  93. static BOOL
  94. LeaveNonRecursiveMutex(PNRMUTEX mutex)
  95. {
  96. BOOL result;
  97. if (PyMUTEX_LOCK(&mutex->cs))
  98. return FALSE;
  99. mutex->locked = 0;
  100. /* condvar APIs return 0 on success. We need to return TRUE on success. */
  101. result = !PyCOND_SIGNAL(&mutex->cv);
  102. PyMUTEX_UNLOCK(&mutex->cs);
  103. return result;
  104. }
  105. #else /* if ! _PY_USE_CV_LOCKS */
  106. /* NR-locks based on a kernel mutex */
  107. #define PNRMUTEX HANDLE
  108. static PNRMUTEX
  109. AllocNonRecursiveMutex(void)
  110. {
  111. return CreateSemaphore(NULL, 1, 1, NULL);
  112. }
  113. static VOID
  114. FreeNonRecursiveMutex(PNRMUTEX mutex)
  115. {
  116. /* No in-use check */
  117. CloseHandle(mutex);
  118. }
  119. static DWORD
  120. EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
  121. {
  122. return WaitForSingleObjectEx(mutex, milliseconds, FALSE);
  123. }
  124. static BOOL
  125. LeaveNonRecursiveMutex(PNRMUTEX mutex)
  126. {
  127. return ReleaseSemaphore(mutex, 1, NULL);
  128. }
  129. #endif /* _PY_USE_CV_LOCKS */
  130. unsigned long PyThread_get_thread_ident(void);
  131. #ifdef PY_HAVE_THREAD_NATIVE_ID
  132. unsigned long PyThread_get_thread_native_id(void);
  133. #endif
  134. /*
  135. * Initialization for the current runtime.
  136. */
  137. static void
  138. PyThread__init_thread(void)
  139. {
  140. // Initialization of the C package should not be needed.
  141. }
  142. /*
  143. * Thread support.
  144. */
  145. typedef struct {
  146. void (*func)(void*);
  147. void *arg;
  148. } callobj;
  149. /* thunker to call adapt between the function type used by the system's
  150. thread start function and the internally used one. */
  151. static unsigned __stdcall
  152. bootstrap(void *call)
  153. {
  154. callobj *obj = (callobj*)call;
  155. void (*func)(void*) = obj->func;
  156. void *arg = obj->arg;
  157. HeapFree(GetProcessHeap(), 0, obj);
  158. func(arg);
  159. return 0;
  160. }
  161. unsigned long
  162. PyThread_start_new_thread(void (*func)(void *), void *arg)
  163. {
  164. HANDLE hThread;
  165. unsigned threadID;
  166. callobj *obj;
  167. if (!initialized)
  168. PyThread_init_thread();
  169. obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
  170. if (!obj)
  171. return PYTHREAD_INVALID_THREAD_ID;
  172. obj->func = func;
  173. obj->arg = arg;
  174. PyThreadState *tstate = _PyThreadState_GET();
  175. size_t stacksize = tstate ? tstate->interp->threads.stacksize : 0;
  176. hThread = (HANDLE)_beginthreadex(0,
  177. Py_SAFE_DOWNCAST(stacksize, Py_ssize_t, unsigned int),
  178. bootstrap, obj,
  179. 0, &threadID);
  180. if (hThread == 0) {
  181. /* I've seen errno == EAGAIN here, which means "there are
  182. * too many threads".
  183. */
  184. int e = errno;
  185. threadID = (unsigned)-1;
  186. HeapFree(GetProcessHeap(), 0, obj);
  187. }
  188. else {
  189. CloseHandle(hThread);
  190. }
  191. return threadID;
  192. }
  193. /*
  194. * Return the thread Id instead of a handle. The Id is said to uniquely identify the
  195. * thread in the system
  196. */
  197. unsigned long
  198. PyThread_get_thread_ident(void)
  199. {
  200. if (!initialized)
  201. PyThread_init_thread();
  202. return GetCurrentThreadId();
  203. }
  204. #ifdef PY_HAVE_THREAD_NATIVE_ID
  205. /*
  206. * Return the native Thread ID (TID) of the calling thread.
  207. * The native ID of a thread is valid and guaranteed to be unique system-wide
  208. * from the time the thread is created until the thread has been terminated.
  209. */
  210. unsigned long
  211. PyThread_get_thread_native_id(void)
  212. {
  213. if (!initialized) {
  214. PyThread_init_thread();
  215. }
  216. DWORD native_id;
  217. native_id = GetCurrentThreadId();
  218. return (unsigned long) native_id;
  219. }
  220. #endif
  221. void _Py_NO_RETURN
  222. PyThread_exit_thread(void)
  223. {
  224. if (!initialized)
  225. exit(0);
  226. _endthreadex(0);
  227. }
  228. /*
  229. * Lock support. It has to be implemented as semaphores.
  230. * I [Dag] tried to implement it with mutex but I could find a way to
  231. * tell whether a thread already own the lock or not.
  232. */
  233. PyThread_type_lock
  234. PyThread_allocate_lock(void)
  235. {
  236. PNRMUTEX mutex;
  237. if (!initialized)
  238. PyThread_init_thread();
  239. mutex = AllocNonRecursiveMutex() ;
  240. PyThread_type_lock aLock = (PyThread_type_lock) mutex;
  241. assert(aLock);
  242. return aLock;
  243. }
  244. void
  245. PyThread_free_lock(PyThread_type_lock aLock)
  246. {
  247. FreeNonRecursiveMutex(aLock) ;
  248. }
  249. // WaitForSingleObject() accepts timeout in milliseconds in the range
  250. // [0; 0xFFFFFFFE] (DWORD type). INFINITE value (0xFFFFFFFF) means no
  251. // timeout. 0xFFFFFFFE milliseconds is around 49.7 days.
  252. const DWORD TIMEOUT_MS_MAX = 0xFFFFFFFE;
  253. /*
  254. * Return 1 on success if the lock was acquired
  255. *
  256. * and 0 if the lock was not acquired. This means a 0 is returned
  257. * if the lock has already been acquired by this thread!
  258. */
  259. PyLockStatus
  260. PyThread_acquire_lock_timed(PyThread_type_lock aLock,
  261. PY_TIMEOUT_T microseconds, int intr_flag)
  262. {
  263. assert(aLock);
  264. /* Fow now, intr_flag does nothing on Windows, and lock acquires are
  265. * uninterruptible. */
  266. PyLockStatus success;
  267. PY_TIMEOUT_T milliseconds;
  268. if (microseconds >= 0) {
  269. milliseconds = microseconds / 1000;
  270. // Round milliseconds away from zero
  271. if (microseconds % 1000 > 0) {
  272. milliseconds++;
  273. }
  274. if (milliseconds > (PY_TIMEOUT_T)TIMEOUT_MS_MAX) {
  275. // bpo-41710: PyThread_acquire_lock_timed() cannot report timeout
  276. // overflow to the caller, so clamp the timeout to
  277. // [0, TIMEOUT_MS_MAX] milliseconds.
  278. //
  279. // _thread.Lock.acquire() and _thread.RLock.acquire() raise an
  280. // OverflowError if microseconds is greater than PY_TIMEOUT_MAX.
  281. milliseconds = TIMEOUT_MS_MAX;
  282. }
  283. assert(milliseconds != INFINITE);
  284. }
  285. else {
  286. milliseconds = INFINITE;
  287. }
  288. if (EnterNonRecursiveMutex((PNRMUTEX)aLock,
  289. (DWORD)milliseconds) == WAIT_OBJECT_0) {
  290. success = PY_LOCK_ACQUIRED;
  291. }
  292. else {
  293. success = PY_LOCK_FAILURE;
  294. }
  295. return success;
  296. }
  297. int
  298. PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
  299. {
  300. return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0);
  301. }
  302. void
  303. PyThread_release_lock(PyThread_type_lock aLock)
  304. {
  305. assert(aLock);
  306. (void)LeaveNonRecursiveMutex((PNRMUTEX) aLock);
  307. }
  308. /* minimum/maximum thread stack sizes supported */
  309. #define THREAD_MIN_STACKSIZE 0x8000 /* 32 KiB */
  310. #define THREAD_MAX_STACKSIZE 0x10000000 /* 256 MiB */
  311. /* set the thread stack size.
  312. * Return 0 if size is valid, -1 otherwise.
  313. */
  314. static int
  315. _pythread_nt_set_stacksize(size_t size)
  316. {
  317. /* set to default */
  318. if (size == 0) {
  319. _PyInterpreterState_GET()->threads.stacksize = 0;
  320. return 0;
  321. }
  322. /* valid range? */
  323. if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
  324. _PyInterpreterState_GET()->threads.stacksize = size;
  325. return 0;
  326. }
  327. return -1;
  328. }
  329. #define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)
  330. /* Thread Local Storage (TLS) API
  331. This API is DEPRECATED since Python 3.7. See PEP 539 for details.
  332. */
  333. int
  334. PyThread_create_key(void)
  335. {
  336. DWORD result = TlsAlloc();
  337. if (result == TLS_OUT_OF_INDEXES)
  338. return -1;
  339. return (int)result;
  340. }
  341. void
  342. PyThread_delete_key(int key)
  343. {
  344. TlsFree(key);
  345. }
  346. int
  347. PyThread_set_key_value(int key, void *value)
  348. {
  349. BOOL ok = TlsSetValue(key, value);
  350. return ok ? 0 : -1;
  351. }
  352. void *
  353. PyThread_get_key_value(int key)
  354. {
  355. /* because TLS is used in the Py_END_ALLOW_THREAD macro,
  356. * it is necessary to preserve the windows error state, because
  357. * it is assumed to be preserved across the call to the macro.
  358. * Ideally, the macro should be fixed, but it is simpler to
  359. * do it here.
  360. */
  361. DWORD error = GetLastError();
  362. void *result = TlsGetValue(key);
  363. SetLastError(error);
  364. return result;
  365. }
  366. void
  367. PyThread_delete_key_value(int key)
  368. {
  369. /* NULL is used as "key missing", and it is also the default
  370. * given by TlsGetValue() if nothing has been set yet.
  371. */
  372. TlsSetValue(key, NULL);
  373. }
  374. /* reinitialization of TLS is not necessary after fork when using
  375. * the native TLS functions. And forking isn't supported on Windows either.
  376. */
  377. void
  378. PyThread_ReInitTLS(void)
  379. {
  380. }
  381. /* Thread Specific Storage (TSS) API
  382. Platform-specific components of TSS API implementation.
  383. */
  384. int
  385. PyThread_tss_create(Py_tss_t *key)
  386. {
  387. assert(key != NULL);
  388. /* If the key has been created, function is silently skipped. */
  389. if (key->_is_initialized) {
  390. return 0;
  391. }
  392. DWORD result = TlsAlloc();
  393. if (result == TLS_OUT_OF_INDEXES) {
  394. return -1;
  395. }
  396. /* In Windows, platform-specific key type is DWORD. */
  397. key->_key = result;
  398. key->_is_initialized = 1;
  399. return 0;
  400. }
  401. void
  402. PyThread_tss_delete(Py_tss_t *key)
  403. {
  404. assert(key != NULL);
  405. /* If the key has not been created, function is silently skipped. */
  406. if (!key->_is_initialized) {
  407. return;
  408. }
  409. TlsFree(key->_key);
  410. key->_key = TLS_OUT_OF_INDEXES;
  411. key->_is_initialized = 0;
  412. }
  413. int
  414. PyThread_tss_set(Py_tss_t *key, void *value)
  415. {
  416. assert(key != NULL);
  417. BOOL ok = TlsSetValue(key->_key, value);
  418. return ok ? 0 : -1;
  419. }
  420. void *
  421. PyThread_tss_get(Py_tss_t *key)
  422. {
  423. assert(key != NULL);
  424. /* because TSS is used in the Py_END_ALLOW_THREAD macro,
  425. * it is necessary to preserve the windows error state, because
  426. * it is assumed to be preserved across the call to the macro.
  427. * Ideally, the macro should be fixed, but it is simpler to
  428. * do it here.
  429. */
  430. DWORD error = GetLastError();
  431. void *result = TlsGetValue(key->_key);
  432. SetLastError(error);
  433. return result;
  434. }