thread_pthread.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. #include "pycore_interp.h" // _PyInterpreterState.threads.stacksize
  2. /* Posix threads interface */
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
  6. #define destructor xxdestructor
  7. #endif
  8. #ifndef HAVE_PTHREAD_STUBS
  9. # include <pthread.h>
  10. #endif
  11. #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
  12. #undef destructor
  13. #endif
  14. #include <signal.h>
  15. #if defined(__linux__)
  16. # include <sys/syscall.h> /* syscall(SYS_gettid) */
  17. #elif defined(__FreeBSD__)
  18. # include <pthread_np.h> /* pthread_getthreadid_np() */
  19. #elif defined(__OpenBSD__)
  20. # include <unistd.h> /* getthrid() */
  21. #elif defined(_AIX)
  22. # include <sys/thread.h> /* thread_self() */
  23. #elif defined(__NetBSD__)
  24. # include <lwp.h> /* _lwp_self() */
  25. #elif defined(__DragonFly__)
  26. # error #include <sys/lwp.h> /* lwp_gettid() */
  27. #endif
  28. /* The POSIX spec requires that use of pthread_attr_setstacksize
  29. be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */
  30. #ifdef _POSIX_THREAD_ATTR_STACKSIZE
  31. #ifndef THREAD_STACK_SIZE
  32. #define THREAD_STACK_SIZE 0 /* use default stack size */
  33. #endif
  34. /* The default stack size for new threads on BSD is small enough that
  35. * we'll get hard crashes instead of 'maximum recursion depth exceeded'
  36. * exceptions.
  37. *
  38. * The default stack size below is the empirically determined minimal stack
  39. * sizes where a simple recursive function doesn't cause a hard crash.
  40. *
  41. * For macOS the value of THREAD_STACK_SIZE is determined in configure.ac
  42. * as it also depends on the other configure options like chosen sanitizer
  43. * runtimes.
  44. */
  45. #if defined(__FreeBSD__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
  46. #undef THREAD_STACK_SIZE
  47. #define THREAD_STACK_SIZE 0x400000
  48. #endif
  49. #if defined(_AIX) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
  50. #undef THREAD_STACK_SIZE
  51. #define THREAD_STACK_SIZE 0x200000
  52. #endif
  53. /* bpo-38852: test_threading.test_recursion_limit() checks that 1000 recursive
  54. Python calls (default recursion limit) doesn't crash, but raise a regular
  55. RecursionError exception. In debug mode, Python function calls allocates
  56. more memory on the stack, so use a stack of 8 MiB. */
  57. #if defined(__ANDROID__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
  58. # ifdef Py_DEBUG
  59. # undef THREAD_STACK_SIZE
  60. # define THREAD_STACK_SIZE 0x800000
  61. # endif
  62. #endif
  63. #if defined(__VXWORKS__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
  64. #undef THREAD_STACK_SIZE
  65. #define THREAD_STACK_SIZE 0x100000
  66. #endif
  67. /* for safety, ensure a viable minimum stacksize */
  68. #define THREAD_STACK_MIN 0x8000 /* 32 KiB */
  69. #else /* !_POSIX_THREAD_ATTR_STACKSIZE */
  70. #ifdef THREAD_STACK_SIZE
  71. #error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
  72. #endif
  73. #endif
  74. /* The POSIX spec says that implementations supporting the sem_*
  75. family of functions must indicate this by defining
  76. _POSIX_SEMAPHORES. */
  77. #ifdef _POSIX_SEMAPHORES
  78. /* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
  79. we need to add 0 to make it work there as well. */
  80. #if (_POSIX_SEMAPHORES+0) == -1
  81. #define HAVE_BROKEN_POSIX_SEMAPHORES
  82. #else
  83. #include <semaphore.h>
  84. #include <errno.h>
  85. #endif
  86. #endif
  87. /* Whether or not to use semaphores directly rather than emulating them with
  88. * mutexes and condition variables:
  89. */
  90. #if (defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES) && \
  91. (defined(HAVE_SEM_TIMEDWAIT) || defined(HAVE_SEM_CLOCKWAIT)))
  92. # define USE_SEMAPHORES
  93. #else
  94. # undef USE_SEMAPHORES
  95. #endif
  96. /* On platforms that don't use standard POSIX threads pthread_sigmask()
  97. * isn't present. DEC threads uses sigprocmask() instead as do most
  98. * other UNIX International compliant systems that don't have the full
  99. * pthread implementation.
  100. */
  101. #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
  102. # define SET_THREAD_SIGMASK pthread_sigmask
  103. #else
  104. # define SET_THREAD_SIGMASK sigprocmask
  105. #endif
  106. /*
  107. * pthread_cond support
  108. */
  109. #define condattr_monotonic _PyRuntime.threads._condattr_monotonic.ptr
  110. static void
  111. init_condattr(void)
  112. {
  113. #ifdef CONDATTR_MONOTONIC
  114. # define ca _PyRuntime.threads._condattr_monotonic.val
  115. // XXX We need to check the return code?
  116. pthread_condattr_init(&ca);
  117. // XXX We need to run pthread_condattr_destroy() during runtime fini.
  118. if (pthread_condattr_setclock(&ca, CLOCK_MONOTONIC) == 0) {
  119. condattr_monotonic = &ca; // Use monotonic clock
  120. }
  121. # undef ca
  122. #endif // CONDATTR_MONOTONIC
  123. }
  124. int
  125. _PyThread_cond_init(PyCOND_T *cond)
  126. {
  127. return pthread_cond_init(cond, condattr_monotonic);
  128. }
  129. void
  130. _PyThread_cond_after(long long us, struct timespec *abs)
  131. {
  132. _PyTime_t timeout = _PyTime_FromMicrosecondsClamp(us);
  133. _PyTime_t t;
  134. #ifdef CONDATTR_MONOTONIC
  135. if (condattr_monotonic) {
  136. t = _PyTime_GetMonotonicClock();
  137. }
  138. else
  139. #endif
  140. {
  141. t = _PyTime_GetSystemClock();
  142. }
  143. t = _PyTime_Add(t, timeout);
  144. _PyTime_AsTimespec_clamp(t, abs);
  145. }
  146. /* A pthread mutex isn't sufficient to model the Python lock type
  147. * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
  148. * following are undefined:
  149. * -> a thread tries to lock a mutex it already has locked
  150. * -> a thread tries to unlock a mutex locked by a different thread
  151. * pthread mutexes are designed for serializing threads over short pieces
  152. * of code anyway, so wouldn't be an appropriate implementation of
  153. * Python's locks regardless.
  154. *
  155. * The pthread_lock struct implements a Python lock as a "locked?" bit
  156. * and a <condition, mutex> pair. In general, if the bit can be acquired
  157. * instantly, it is, else the pair is used to block the thread until the
  158. * bit is cleared. 9 May 1994 tim@ksr.com
  159. */
  160. typedef struct {
  161. char locked; /* 0=unlocked, 1=locked */
  162. /* a <cond, mutex> pair to handle an acquire of a locked lock */
  163. pthread_cond_t lock_released;
  164. pthread_mutex_t mut;
  165. } pthread_lock;
  166. #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
  167. #define CHECK_STATUS_PTHREAD(name) if (status != 0) { fprintf(stderr, \
  168. "%s: %s\n", name, strerror(status)); error = 1; }
  169. /*
  170. * Initialization for the current runtime.
  171. */
  172. static void
  173. PyThread__init_thread(void)
  174. {
  175. // The library is only initialized once in the process,
  176. // regardless of how many times the Python runtime is initialized.
  177. static int lib_initialized = 0;
  178. if (!lib_initialized) {
  179. lib_initialized = 1;
  180. #if defined(_AIX) && defined(__GNUC__)
  181. extern void pthread_init(void);
  182. pthread_init();
  183. #endif
  184. }
  185. init_condattr();
  186. }
  187. /*
  188. * Thread support.
  189. */
  190. /* bpo-33015: pythread_callback struct and pythread_wrapper() cast
  191. "void func(void *)" to "void* func(void *)": always return NULL.
  192. PyThread_start_new_thread() uses "void func(void *)" type, whereas
  193. pthread_create() requires a void* return value. */
  194. typedef struct {
  195. void (*func) (void *);
  196. void *arg;
  197. } pythread_callback;
  198. static void *
  199. pythread_wrapper(void *arg)
  200. {
  201. /* copy func and func_arg and free the temporary structure */
  202. pythread_callback *callback = arg;
  203. void (*func)(void *) = callback->func;
  204. void *func_arg = callback->arg;
  205. PyMem_RawFree(arg);
  206. func(func_arg);
  207. return NULL;
  208. }
  209. unsigned long
  210. PyThread_start_new_thread(void (*func)(void *), void *arg)
  211. {
  212. pthread_t th;
  213. int status;
  214. #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
  215. pthread_attr_t attrs;
  216. #endif
  217. #if defined(THREAD_STACK_SIZE)
  218. size_t tss;
  219. #endif
  220. if (!initialized)
  221. PyThread_init_thread();
  222. #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
  223. if (pthread_attr_init(&attrs) != 0)
  224. return PYTHREAD_INVALID_THREAD_ID;
  225. #endif
  226. #if defined(THREAD_STACK_SIZE)
  227. PyThreadState *tstate = _PyThreadState_GET();
  228. size_t stacksize = tstate ? tstate->interp->threads.stacksize : 0;
  229. tss = (stacksize != 0) ? stacksize : THREAD_STACK_SIZE;
  230. if (tss != 0) {
  231. if (pthread_attr_setstacksize(&attrs, tss) != 0) {
  232. pthread_attr_destroy(&attrs);
  233. return PYTHREAD_INVALID_THREAD_ID;
  234. }
  235. }
  236. #endif
  237. #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
  238. pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
  239. #endif
  240. pythread_callback *callback = PyMem_RawMalloc(sizeof(pythread_callback));
  241. if (callback == NULL) {
  242. return PYTHREAD_INVALID_THREAD_ID;
  243. }
  244. callback->func = func;
  245. callback->arg = arg;
  246. status = pthread_create(&th,
  247. #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
  248. &attrs,
  249. #else
  250. (pthread_attr_t*)NULL,
  251. #endif
  252. pythread_wrapper, callback);
  253. #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
  254. pthread_attr_destroy(&attrs);
  255. #endif
  256. if (status != 0) {
  257. PyMem_RawFree(callback);
  258. return PYTHREAD_INVALID_THREAD_ID;
  259. }
  260. pthread_detach(th);
  261. #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
  262. return (unsigned long) th;
  263. #else
  264. return (unsigned long) *(unsigned long *) &th;
  265. #endif
  266. }
  267. /* XXX This implementation is considered (to quote Tim Peters) "inherently
  268. hosed" because:
  269. - It does not guarantee the promise that a non-zero integer is returned.
  270. - The cast to unsigned long is inherently unsafe.
  271. - It is not clear that the 'volatile' (for AIX?) are any longer necessary.
  272. */
  273. unsigned long
  274. PyThread_get_thread_ident(void)
  275. {
  276. volatile pthread_t threadid;
  277. if (!initialized)
  278. PyThread_init_thread();
  279. threadid = pthread_self();
  280. return (unsigned long) threadid;
  281. }
  282. #ifdef PY_HAVE_THREAD_NATIVE_ID
  283. unsigned long
  284. PyThread_get_thread_native_id(void)
  285. {
  286. if (!initialized)
  287. PyThread_init_thread();
  288. #ifdef __APPLE__
  289. uint64_t native_id;
  290. (void) pthread_threadid_np(NULL, &native_id);
  291. #elif defined(__linux__)
  292. pid_t native_id;
  293. native_id = syscall(SYS_gettid);
  294. #elif defined(__FreeBSD__)
  295. int native_id;
  296. native_id = pthread_getthreadid_np();
  297. #elif defined(__OpenBSD__)
  298. pid_t native_id;
  299. native_id = getthrid();
  300. #elif defined(_AIX)
  301. tid_t native_id;
  302. native_id = thread_self();
  303. #elif defined(__NetBSD__)
  304. lwpid_t native_id;
  305. native_id = _lwp_self();
  306. #elif defined(__DragonFly__)
  307. lwpid_t native_id;
  308. native_id = lwp_gettid();
  309. #endif
  310. return (unsigned long) native_id;
  311. }
  312. #endif
  313. void _Py_NO_RETURN
  314. PyThread_exit_thread(void)
  315. {
  316. if (!initialized)
  317. exit(0);
  318. #if defined(__wasi__)
  319. /*
  320. * wasi-threads doesn't have pthread_exit right now
  321. * cf. https://github.com/WebAssembly/wasi-threads/issues/7
  322. */
  323. abort();
  324. #else
  325. pthread_exit(0);
  326. #endif
  327. }
  328. #ifdef USE_SEMAPHORES
  329. /*
  330. * Lock support.
  331. */
  332. PyThread_type_lock
  333. PyThread_allocate_lock(void)
  334. {
  335. sem_t *lock;
  336. int status, error = 0;
  337. if (!initialized)
  338. PyThread_init_thread();
  339. lock = (sem_t *)PyMem_RawMalloc(sizeof(sem_t));
  340. if (lock) {
  341. status = sem_init(lock,0,1);
  342. CHECK_STATUS("sem_init");
  343. if (error) {
  344. PyMem_RawFree((void *)lock);
  345. lock = NULL;
  346. }
  347. }
  348. return (PyThread_type_lock)lock;
  349. }
  350. void
  351. PyThread_free_lock(PyThread_type_lock lock)
  352. {
  353. sem_t *thelock = (sem_t *)lock;
  354. int status, error = 0;
  355. (void) error; /* silence unused-but-set-variable warning */
  356. if (!thelock)
  357. return;
  358. status = sem_destroy(thelock);
  359. CHECK_STATUS("sem_destroy");
  360. PyMem_RawFree((void *)thelock);
  361. }
  362. /*
  363. * As of February 2002, Cygwin thread implementations mistakenly report error
  364. * codes in the return value of the sem_ calls (like the pthread_ functions).
  365. * Correct implementations return -1 and put the code in errno. This supports
  366. * either.
  367. */
  368. static int
  369. fix_status(int status)
  370. {
  371. return (status == -1) ? errno : status;
  372. }
  373. PyLockStatus
  374. PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
  375. int intr_flag)
  376. {
  377. PyLockStatus success;
  378. sem_t *thelock = (sem_t *)lock;
  379. int status, error = 0;
  380. (void) error; /* silence unused-but-set-variable warning */
  381. _PyTime_t timeout; // relative timeout
  382. if (microseconds >= 0) {
  383. // bpo-41710: PyThread_acquire_lock_timed() cannot report timeout
  384. // overflow to the caller, so clamp the timeout to
  385. // [_PyTime_MIN, _PyTime_MAX].
  386. //
  387. // _PyTime_MAX nanoseconds is around 292.3 years.
  388. //
  389. // _thread.Lock.acquire() and _thread.RLock.acquire() raise an
  390. // OverflowError if microseconds is greater than PY_TIMEOUT_MAX.
  391. timeout = _PyTime_FromMicrosecondsClamp(microseconds);
  392. }
  393. else {
  394. timeout = _PyTime_FromNanoseconds(-1);
  395. }
  396. #ifdef HAVE_SEM_CLOCKWAIT
  397. struct timespec abs_timeout;
  398. // Local scope for deadline
  399. {
  400. _PyTime_t deadline = _PyTime_Add(_PyTime_GetMonotonicClock(), timeout);
  401. _PyTime_AsTimespec_clamp(deadline, &abs_timeout);
  402. }
  403. #else
  404. _PyTime_t deadline = 0;
  405. if (timeout > 0 && !intr_flag) {
  406. deadline = _PyDeadline_Init(timeout);
  407. }
  408. #endif
  409. while (1) {
  410. if (timeout > 0) {
  411. #ifdef HAVE_SEM_CLOCKWAIT
  412. status = fix_status(sem_clockwait(thelock, CLOCK_MONOTONIC,
  413. &abs_timeout));
  414. #else
  415. _PyTime_t abs_time = _PyTime_Add(_PyTime_GetSystemClock(),
  416. timeout);
  417. struct timespec ts;
  418. _PyTime_AsTimespec_clamp(abs_time, &ts);
  419. status = fix_status(sem_timedwait(thelock, &ts));
  420. #endif
  421. }
  422. else if (timeout == 0) {
  423. status = fix_status(sem_trywait(thelock));
  424. }
  425. else {
  426. status = fix_status(sem_wait(thelock));
  427. }
  428. /* Retry if interrupted by a signal, unless the caller wants to be
  429. notified. */
  430. if (intr_flag || status != EINTR) {
  431. break;
  432. }
  433. // sem_clockwait() uses an absolute timeout, there is no need
  434. // to recompute the relative timeout.
  435. #ifndef HAVE_SEM_CLOCKWAIT
  436. if (timeout > 0) {
  437. /* wait interrupted by a signal (EINTR): recompute the timeout */
  438. timeout = _PyDeadline_Get(deadline);
  439. if (timeout < 0) {
  440. status = ETIMEDOUT;
  441. break;
  442. }
  443. }
  444. #endif
  445. }
  446. /* Don't check the status if we're stopping because of an interrupt. */
  447. if (!(intr_flag && status == EINTR)) {
  448. if (timeout > 0) {
  449. if (status != ETIMEDOUT) {
  450. #ifdef HAVE_SEM_CLOCKWAIT
  451. CHECK_STATUS("sem_clockwait");
  452. #else
  453. CHECK_STATUS("sem_timedwait");
  454. #endif
  455. }
  456. }
  457. else if (timeout == 0) {
  458. if (status != EAGAIN) {
  459. CHECK_STATUS("sem_trywait");
  460. }
  461. }
  462. else {
  463. CHECK_STATUS("sem_wait");
  464. }
  465. }
  466. if (status == 0) {
  467. success = PY_LOCK_ACQUIRED;
  468. } else if (intr_flag && status == EINTR) {
  469. success = PY_LOCK_INTR;
  470. } else {
  471. success = PY_LOCK_FAILURE;
  472. }
  473. return success;
  474. }
  475. void
  476. PyThread_release_lock(PyThread_type_lock lock)
  477. {
  478. sem_t *thelock = (sem_t *)lock;
  479. int status, error = 0;
  480. (void) error; /* silence unused-but-set-variable warning */
  481. status = sem_post(thelock);
  482. CHECK_STATUS("sem_post");
  483. }
  484. #else /* USE_SEMAPHORES */
  485. /*
  486. * Lock support.
  487. */
  488. PyThread_type_lock
  489. PyThread_allocate_lock(void)
  490. {
  491. pthread_lock *lock;
  492. int status, error = 0;
  493. if (!initialized)
  494. PyThread_init_thread();
  495. lock = (pthread_lock *) PyMem_RawCalloc(1, sizeof(pthread_lock));
  496. if (lock) {
  497. lock->locked = 0;
  498. status = pthread_mutex_init(&lock->mut, NULL);
  499. CHECK_STATUS_PTHREAD("pthread_mutex_init");
  500. /* Mark the pthread mutex underlying a Python mutex as
  501. pure happens-before. We can't simply mark the
  502. Python-level mutex as a mutex because it can be
  503. acquired and released in different threads, which
  504. will cause errors. */
  505. _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(&lock->mut);
  506. status = _PyThread_cond_init(&lock->lock_released);
  507. CHECK_STATUS_PTHREAD("pthread_cond_init");
  508. if (error) {
  509. PyMem_RawFree((void *)lock);
  510. lock = 0;
  511. }
  512. }
  513. return (PyThread_type_lock) lock;
  514. }
  515. void
  516. PyThread_free_lock(PyThread_type_lock lock)
  517. {
  518. pthread_lock *thelock = (pthread_lock *)lock;
  519. int status, error = 0;
  520. (void) error; /* silence unused-but-set-variable warning */
  521. /* some pthread-like implementations tie the mutex to the cond
  522. * and must have the cond destroyed first.
  523. */
  524. status = pthread_cond_destroy( &thelock->lock_released );
  525. CHECK_STATUS_PTHREAD("pthread_cond_destroy");
  526. status = pthread_mutex_destroy( &thelock->mut );
  527. CHECK_STATUS_PTHREAD("pthread_mutex_destroy");
  528. PyMem_RawFree((void *)thelock);
  529. }
  530. PyLockStatus
  531. PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
  532. int intr_flag)
  533. {
  534. PyLockStatus success = PY_LOCK_FAILURE;
  535. pthread_lock *thelock = (pthread_lock *)lock;
  536. int status, error = 0;
  537. if (microseconds == 0) {
  538. status = pthread_mutex_trylock( &thelock->mut );
  539. if (status != EBUSY) {
  540. CHECK_STATUS_PTHREAD("pthread_mutex_trylock[1]");
  541. }
  542. }
  543. else {
  544. status = pthread_mutex_lock( &thelock->mut );
  545. CHECK_STATUS_PTHREAD("pthread_mutex_lock[1]");
  546. }
  547. if (status != 0) {
  548. goto done;
  549. }
  550. if (thelock->locked == 0) {
  551. success = PY_LOCK_ACQUIRED;
  552. goto unlock;
  553. }
  554. if (microseconds == 0) {
  555. goto unlock;
  556. }
  557. struct timespec abs_timeout;
  558. if (microseconds > 0) {
  559. _PyThread_cond_after(microseconds, &abs_timeout);
  560. }
  561. // Continue trying until we get the lock
  562. // mut must be locked by me -- part of the condition protocol
  563. while (1) {
  564. if (microseconds > 0) {
  565. status = pthread_cond_timedwait(&thelock->lock_released,
  566. &thelock->mut, &abs_timeout);
  567. if (status == 1) {
  568. break;
  569. }
  570. if (status == ETIMEDOUT) {
  571. break;
  572. }
  573. CHECK_STATUS_PTHREAD("pthread_cond_timedwait");
  574. }
  575. else {
  576. status = pthread_cond_wait(
  577. &thelock->lock_released,
  578. &thelock->mut);
  579. CHECK_STATUS_PTHREAD("pthread_cond_wait");
  580. }
  581. if (intr_flag && status == 0 && thelock->locked) {
  582. // We were woken up, but didn't get the lock. We probably received
  583. // a signal. Return PY_LOCK_INTR to allow the caller to handle
  584. // it and retry.
  585. success = PY_LOCK_INTR;
  586. break;
  587. }
  588. if (status == 0 && !thelock->locked) {
  589. success = PY_LOCK_ACQUIRED;
  590. break;
  591. }
  592. // Wait got interrupted by a signal: retry
  593. }
  594. unlock:
  595. if (success == PY_LOCK_ACQUIRED) {
  596. thelock->locked = 1;
  597. }
  598. status = pthread_mutex_unlock( &thelock->mut );
  599. CHECK_STATUS_PTHREAD("pthread_mutex_unlock[1]");
  600. done:
  601. if (error) {
  602. success = PY_LOCK_FAILURE;
  603. }
  604. return success;
  605. }
  606. void
  607. PyThread_release_lock(PyThread_type_lock lock)
  608. {
  609. pthread_lock *thelock = (pthread_lock *)lock;
  610. int status, error = 0;
  611. (void) error; /* silence unused-but-set-variable warning */
  612. status = pthread_mutex_lock( &thelock->mut );
  613. CHECK_STATUS_PTHREAD("pthread_mutex_lock[3]");
  614. thelock->locked = 0;
  615. /* wake up someone (anyone, if any) waiting on the lock */
  616. status = pthread_cond_signal( &thelock->lock_released );
  617. CHECK_STATUS_PTHREAD("pthread_cond_signal");
  618. status = pthread_mutex_unlock( &thelock->mut );
  619. CHECK_STATUS_PTHREAD("pthread_mutex_unlock[3]");
  620. }
  621. #endif /* USE_SEMAPHORES */
  622. int
  623. _PyThread_at_fork_reinit(PyThread_type_lock *lock)
  624. {
  625. PyThread_type_lock new_lock = PyThread_allocate_lock();
  626. if (new_lock == NULL) {
  627. return -1;
  628. }
  629. /* bpo-6721, bpo-40089: The old lock can be in an inconsistent state.
  630. fork() can be called in the middle of an operation on the lock done by
  631. another thread. So don't call PyThread_free_lock(*lock).
  632. Leak memory on purpose. Don't release the memory either since the
  633. address of a mutex is relevant. Putting two mutexes at the same address
  634. can lead to problems. */
  635. *lock = new_lock;
  636. return 0;
  637. }
  638. int
  639. PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
  640. {
  641. return PyThread_acquire_lock_timed(lock, waitflag ? -1 : 0, /*intr_flag=*/0);
  642. }
  643. /* set the thread stack size.
  644. * Return 0 if size is valid, -1 if size is invalid,
  645. * -2 if setting stack size is not supported.
  646. */
  647. static int
  648. _pythread_pthread_set_stacksize(size_t size)
  649. {
  650. #if defined(THREAD_STACK_SIZE)
  651. pthread_attr_t attrs;
  652. size_t tss_min;
  653. int rc = 0;
  654. #endif
  655. /* set to default */
  656. if (size == 0) {
  657. _PyInterpreterState_GET()->threads.stacksize = 0;
  658. return 0;
  659. }
  660. #if defined(THREAD_STACK_SIZE)
  661. #if defined(PTHREAD_STACK_MIN)
  662. tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
  663. : THREAD_STACK_MIN;
  664. #else
  665. tss_min = THREAD_STACK_MIN;
  666. #endif
  667. if (size >= tss_min) {
  668. /* validate stack size by setting thread attribute */
  669. if (pthread_attr_init(&attrs) == 0) {
  670. rc = pthread_attr_setstacksize(&attrs, size);
  671. pthread_attr_destroy(&attrs);
  672. if (rc == 0) {
  673. _PyInterpreterState_GET()->threads.stacksize = size;
  674. return 0;
  675. }
  676. }
  677. }
  678. return -1;
  679. #else
  680. return -2;
  681. #endif
  682. }
  683. #define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
  684. /* Thread Local Storage (TLS) API
  685. This API is DEPRECATED since Python 3.7. See PEP 539 for details.
  686. */
  687. /* Issue #25658: On platforms where native TLS key is defined in a way that
  688. cannot be safely cast to int, PyThread_create_key returns immediately a
  689. failure status and other TLS functions all are no-ops. This indicates
  690. clearly that the old API is not supported on platforms where it cannot be
  691. used reliably, and that no effort will be made to add such support.
  692. Note: PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT will be unnecessary after
  693. removing this API.
  694. */
  695. int
  696. PyThread_create_key(void)
  697. {
  698. #ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT
  699. pthread_key_t key;
  700. int fail = pthread_key_create(&key, NULL);
  701. if (fail)
  702. return -1;
  703. if (key > INT_MAX) {
  704. /* Issue #22206: handle integer overflow */
  705. pthread_key_delete(key);
  706. errno = ENOMEM;
  707. return -1;
  708. }
  709. return (int)key;
  710. #else
  711. return -1; /* never return valid key value. */
  712. #endif
  713. }
  714. void
  715. PyThread_delete_key(int key)
  716. {
  717. #ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT
  718. pthread_key_delete(key);
  719. #endif
  720. }
  721. void
  722. PyThread_delete_key_value(int key)
  723. {
  724. #ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT
  725. pthread_setspecific(key, NULL);
  726. #endif
  727. }
  728. int
  729. PyThread_set_key_value(int key, void *value)
  730. {
  731. #ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT
  732. int fail = pthread_setspecific(key, value);
  733. return fail ? -1 : 0;
  734. #else
  735. return -1;
  736. #endif
  737. }
  738. void *
  739. PyThread_get_key_value(int key)
  740. {
  741. #ifdef PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT
  742. return pthread_getspecific(key);
  743. #else
  744. return NULL;
  745. #endif
  746. }
  747. void
  748. PyThread_ReInitTLS(void)
  749. {
  750. }
  751. /* Thread Specific Storage (TSS) API
  752. Platform-specific components of TSS API implementation.
  753. */
  754. int
  755. PyThread_tss_create(Py_tss_t *key)
  756. {
  757. assert(key != NULL);
  758. /* If the key has been created, function is silently skipped. */
  759. if (key->_is_initialized) {
  760. return 0;
  761. }
  762. int fail = pthread_key_create(&(key->_key), NULL);
  763. if (fail) {
  764. return -1;
  765. }
  766. key->_is_initialized = 1;
  767. return 0;
  768. }
  769. void
  770. PyThread_tss_delete(Py_tss_t *key)
  771. {
  772. assert(key != NULL);
  773. /* If the key has not been created, function is silently skipped. */
  774. if (!key->_is_initialized) {
  775. return;
  776. }
  777. pthread_key_delete(key->_key);
  778. /* pthread has not provided the defined invalid value for the key. */
  779. key->_is_initialized = 0;
  780. }
  781. int
  782. PyThread_tss_set(Py_tss_t *key, void *value)
  783. {
  784. assert(key != NULL);
  785. int fail = pthread_setspecific(key->_key, value);
  786. return fail ? -1 : 0;
  787. }
  788. void *
  789. PyThread_tss_get(Py_tss_t *key)
  790. {
  791. assert(key != NULL);
  792. return pthread_getspecific(key->_key);
  793. }