_threadmodule.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. /* Thread module */
  2. /* Interface to Sjoerd's portable C thread library */
  3. #include "Python.h"
  4. #include "pycore_interp.h" // _PyInterpreterState.threads.count
  5. #include "pycore_moduleobject.h" // _PyModule_GetState()
  6. #include "pycore_pylifecycle.h"
  7. #include "pycore_pystate.h" // _PyThreadState_SetCurrent()
  8. #include <stddef.h> // offsetof()
  9. #include "structmember.h" // PyMemberDef
  10. #ifdef HAVE_SIGNAL_H
  11. # include <signal.h> // SIGINT
  12. #endif
  13. // ThreadError is just an alias to PyExc_RuntimeError
  14. #define ThreadError PyExc_RuntimeError
  15. // Forward declarations
  16. static struct PyModuleDef thread_module;
  17. typedef struct {
  18. PyTypeObject *excepthook_type;
  19. PyTypeObject *lock_type;
  20. PyTypeObject *local_type;
  21. PyTypeObject *local_dummy_type;
  22. } thread_module_state;
  23. static inline thread_module_state*
  24. get_thread_state(PyObject *module)
  25. {
  26. void *state = _PyModule_GetState(module);
  27. assert(state != NULL);
  28. return (thread_module_state *)state;
  29. }
  30. /* Lock objects */
  31. typedef struct {
  32. PyObject_HEAD
  33. PyThread_type_lock lock_lock;
  34. PyObject *in_weakreflist;
  35. char locked; /* for sanity checking */
  36. } lockobject;
  37. static int
  38. lock_traverse(lockobject *self, visitproc visit, void *arg)
  39. {
  40. Py_VISIT(Py_TYPE(self));
  41. return 0;
  42. }
  43. static void
  44. lock_dealloc(lockobject *self)
  45. {
  46. PyObject_GC_UnTrack(self);
  47. if (self->in_weakreflist != NULL) {
  48. PyObject_ClearWeakRefs((PyObject *) self);
  49. }
  50. if (self->lock_lock != NULL) {
  51. /* Unlock the lock so it's safe to free it */
  52. if (self->locked)
  53. PyThread_release_lock(self->lock_lock);
  54. PyThread_free_lock(self->lock_lock);
  55. }
  56. PyTypeObject *tp = Py_TYPE(self);
  57. tp->tp_free((PyObject*)self);
  58. Py_DECREF(tp);
  59. }
  60. /* Helper to acquire an interruptible lock with a timeout. If the lock acquire
  61. * is interrupted, signal handlers are run, and if they raise an exception,
  62. * PY_LOCK_INTR is returned. Otherwise, PY_LOCK_ACQUIRED or PY_LOCK_FAILURE
  63. * are returned, depending on whether the lock can be acquired within the
  64. * timeout.
  65. */
  66. static PyLockStatus
  67. acquire_timed(PyThread_type_lock lock, _PyTime_t timeout)
  68. {
  69. PyThreadState *tstate = _PyThreadState_GET();
  70. _PyTime_t endtime = 0;
  71. if (timeout > 0) {
  72. endtime = _PyDeadline_Init(timeout);
  73. }
  74. PyLockStatus r;
  75. do {
  76. _PyTime_t microseconds;
  77. microseconds = _PyTime_AsMicroseconds(timeout, _PyTime_ROUND_CEILING);
  78. /* first a simple non-blocking try without releasing the GIL */
  79. r = PyThread_acquire_lock_timed(lock, 0, 0);
  80. if (r == PY_LOCK_FAILURE && microseconds != 0) {
  81. Py_BEGIN_ALLOW_THREADS
  82. r = PyThread_acquire_lock_timed(lock, microseconds, 1);
  83. Py_END_ALLOW_THREADS
  84. }
  85. if (r == PY_LOCK_INTR) {
  86. /* Run signal handlers if we were interrupted. Propagate
  87. * exceptions from signal handlers, such as KeyboardInterrupt, by
  88. * passing up PY_LOCK_INTR. */
  89. if (_PyEval_MakePendingCalls(tstate) < 0) {
  90. return PY_LOCK_INTR;
  91. }
  92. /* If we're using a timeout, recompute the timeout after processing
  93. * signals, since those can take time. */
  94. if (timeout > 0) {
  95. timeout = _PyDeadline_Get(endtime);
  96. /* Check for negative values, since those mean block forever.
  97. */
  98. if (timeout < 0) {
  99. r = PY_LOCK_FAILURE;
  100. }
  101. }
  102. }
  103. } while (r == PY_LOCK_INTR); /* Retry if we were interrupted. */
  104. return r;
  105. }
  106. static int
  107. lock_acquire_parse_args(PyObject *args, PyObject *kwds,
  108. _PyTime_t *timeout)
  109. {
  110. char *kwlist[] = {"blocking", "timeout", NULL};
  111. int blocking = 1;
  112. PyObject *timeout_obj = NULL;
  113. const _PyTime_t unset_timeout = _PyTime_FromSeconds(-1);
  114. *timeout = unset_timeout ;
  115. if (!PyArg_ParseTupleAndKeywords(args, kwds, "|pO:acquire", kwlist,
  116. &blocking, &timeout_obj))
  117. return -1;
  118. if (timeout_obj
  119. && _PyTime_FromSecondsObject(timeout,
  120. timeout_obj, _PyTime_ROUND_TIMEOUT) < 0)
  121. return -1;
  122. if (!blocking && *timeout != unset_timeout ) {
  123. PyErr_SetString(PyExc_ValueError,
  124. "can't specify a timeout for a non-blocking call");
  125. return -1;
  126. }
  127. if (*timeout < 0 && *timeout != unset_timeout) {
  128. PyErr_SetString(PyExc_ValueError,
  129. "timeout value must be positive");
  130. return -1;
  131. }
  132. if (!blocking)
  133. *timeout = 0;
  134. else if (*timeout != unset_timeout) {
  135. _PyTime_t microseconds;
  136. microseconds = _PyTime_AsMicroseconds(*timeout, _PyTime_ROUND_TIMEOUT);
  137. if (microseconds > PY_TIMEOUT_MAX) {
  138. PyErr_SetString(PyExc_OverflowError,
  139. "timeout value is too large");
  140. return -1;
  141. }
  142. }
  143. return 0;
  144. }
  145. static PyObject *
  146. lock_PyThread_acquire_lock(lockobject *self, PyObject *args, PyObject *kwds)
  147. {
  148. _PyTime_t timeout;
  149. if (lock_acquire_parse_args(args, kwds, &timeout) < 0)
  150. return NULL;
  151. PyLockStatus r = acquire_timed(self->lock_lock, timeout);
  152. if (r == PY_LOCK_INTR) {
  153. return NULL;
  154. }
  155. if (r == PY_LOCK_ACQUIRED)
  156. self->locked = 1;
  157. return PyBool_FromLong(r == PY_LOCK_ACQUIRED);
  158. }
  159. PyDoc_STRVAR(acquire_doc,
  160. "acquire(blocking=True, timeout=-1) -> bool\n\
  161. (acquire_lock() is an obsolete synonym)\n\
  162. \n\
  163. Lock the lock. Without argument, this blocks if the lock is already\n\
  164. locked (even by the same thread), waiting for another thread to release\n\
  165. the lock, and return True once the lock is acquired.\n\
  166. With an argument, this will only block if the argument is true,\n\
  167. and the return value reflects whether the lock is acquired.\n\
  168. The blocking operation is interruptible.");
  169. static PyObject *
  170. lock_PyThread_release_lock(lockobject *self, PyObject *Py_UNUSED(ignored))
  171. {
  172. /* Sanity check: the lock must be locked */
  173. if (!self->locked) {
  174. PyErr_SetString(ThreadError, "release unlocked lock");
  175. return NULL;
  176. }
  177. PyThread_release_lock(self->lock_lock);
  178. self->locked = 0;
  179. Py_RETURN_NONE;
  180. }
  181. PyDoc_STRVAR(release_doc,
  182. "release()\n\
  183. (release_lock() is an obsolete synonym)\n\
  184. \n\
  185. Release the lock, allowing another thread that is blocked waiting for\n\
  186. the lock to acquire the lock. The lock must be in the locked state,\n\
  187. but it needn't be locked by the same thread that unlocks it.");
  188. static PyObject *
  189. lock_locked_lock(lockobject *self, PyObject *Py_UNUSED(ignored))
  190. {
  191. return PyBool_FromLong((long)self->locked);
  192. }
  193. PyDoc_STRVAR(locked_doc,
  194. "locked() -> bool\n\
  195. (locked_lock() is an obsolete synonym)\n\
  196. \n\
  197. Return whether the lock is in the locked state.");
  198. static PyObject *
  199. lock_repr(lockobject *self)
  200. {
  201. return PyUnicode_FromFormat("<%s %s object at %p>",
  202. self->locked ? "locked" : "unlocked", Py_TYPE(self)->tp_name, self);
  203. }
  204. #ifdef HAVE_FORK
  205. static PyObject *
  206. lock__at_fork_reinit(lockobject *self, PyObject *Py_UNUSED(args))
  207. {
  208. if (_PyThread_at_fork_reinit(&self->lock_lock) < 0) {
  209. PyErr_SetString(ThreadError, "failed to reinitialize lock at fork");
  210. return NULL;
  211. }
  212. self->locked = 0;
  213. Py_RETURN_NONE;
  214. }
  215. #endif /* HAVE_FORK */
  216. static PyMethodDef lock_methods[] = {
  217. {"acquire_lock", _PyCFunction_CAST(lock_PyThread_acquire_lock),
  218. METH_VARARGS | METH_KEYWORDS, acquire_doc},
  219. {"acquire", _PyCFunction_CAST(lock_PyThread_acquire_lock),
  220. METH_VARARGS | METH_KEYWORDS, acquire_doc},
  221. {"release_lock", (PyCFunction)lock_PyThread_release_lock,
  222. METH_NOARGS, release_doc},
  223. {"release", (PyCFunction)lock_PyThread_release_lock,
  224. METH_NOARGS, release_doc},
  225. {"locked_lock", (PyCFunction)lock_locked_lock,
  226. METH_NOARGS, locked_doc},
  227. {"locked", (PyCFunction)lock_locked_lock,
  228. METH_NOARGS, locked_doc},
  229. {"__enter__", _PyCFunction_CAST(lock_PyThread_acquire_lock),
  230. METH_VARARGS | METH_KEYWORDS, acquire_doc},
  231. {"__exit__", (PyCFunction)lock_PyThread_release_lock,
  232. METH_VARARGS, release_doc},
  233. #ifdef HAVE_FORK
  234. {"_at_fork_reinit", (PyCFunction)lock__at_fork_reinit,
  235. METH_NOARGS, NULL},
  236. #endif
  237. {NULL, NULL} /* sentinel */
  238. };
  239. PyDoc_STRVAR(lock_doc,
  240. "A lock object is a synchronization primitive. To create a lock,\n\
  241. call threading.Lock(). Methods are:\n\
  242. \n\
  243. acquire() -- lock the lock, possibly blocking until it can be obtained\n\
  244. release() -- unlock of the lock\n\
  245. locked() -- test whether the lock is currently locked\n\
  246. \n\
  247. A lock is not owned by the thread that locked it; another thread may\n\
  248. unlock it. A thread attempting to lock a lock that it has already locked\n\
  249. will block until another thread unlocks it. Deadlocks may ensue.");
  250. static PyMemberDef lock_type_members[] = {
  251. {"__weaklistoffset__", T_PYSSIZET, offsetof(lockobject, in_weakreflist), READONLY},
  252. {NULL},
  253. };
  254. static PyType_Slot lock_type_slots[] = {
  255. {Py_tp_dealloc, (destructor)lock_dealloc},
  256. {Py_tp_repr, (reprfunc)lock_repr},
  257. {Py_tp_doc, (void *)lock_doc},
  258. {Py_tp_methods, lock_methods},
  259. {Py_tp_traverse, lock_traverse},
  260. {Py_tp_members, lock_type_members},
  261. {0, 0}
  262. };
  263. static PyType_Spec lock_type_spec = {
  264. .name = "_thread.lock",
  265. .basicsize = sizeof(lockobject),
  266. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  267. Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE),
  268. .slots = lock_type_slots,
  269. };
  270. /* Recursive lock objects */
  271. typedef struct {
  272. PyObject_HEAD
  273. PyThread_type_lock rlock_lock;
  274. unsigned long rlock_owner;
  275. unsigned long rlock_count;
  276. PyObject *in_weakreflist;
  277. } rlockobject;
  278. static int
  279. rlock_traverse(rlockobject *self, visitproc visit, void *arg)
  280. {
  281. Py_VISIT(Py_TYPE(self));
  282. return 0;
  283. }
  284. static void
  285. rlock_dealloc(rlockobject *self)
  286. {
  287. PyObject_GC_UnTrack(self);
  288. if (self->in_weakreflist != NULL)
  289. PyObject_ClearWeakRefs((PyObject *) self);
  290. /* self->rlock_lock can be NULL if PyThread_allocate_lock() failed
  291. in rlock_new() */
  292. if (self->rlock_lock != NULL) {
  293. /* Unlock the lock so it's safe to free it */
  294. if (self->rlock_count > 0)
  295. PyThread_release_lock(self->rlock_lock);
  296. PyThread_free_lock(self->rlock_lock);
  297. }
  298. PyTypeObject *tp = Py_TYPE(self);
  299. tp->tp_free(self);
  300. Py_DECREF(tp);
  301. }
  302. static PyObject *
  303. rlock_acquire(rlockobject *self, PyObject *args, PyObject *kwds)
  304. {
  305. _PyTime_t timeout;
  306. unsigned long tid;
  307. PyLockStatus r = PY_LOCK_ACQUIRED;
  308. if (lock_acquire_parse_args(args, kwds, &timeout) < 0)
  309. return NULL;
  310. tid = PyThread_get_thread_ident();
  311. if (self->rlock_count > 0 && tid == self->rlock_owner) {
  312. unsigned long count = self->rlock_count + 1;
  313. if (count <= self->rlock_count) {
  314. PyErr_SetString(PyExc_OverflowError,
  315. "Internal lock count overflowed");
  316. return NULL;
  317. }
  318. self->rlock_count = count;
  319. Py_RETURN_TRUE;
  320. }
  321. r = acquire_timed(self->rlock_lock, timeout);
  322. if (r == PY_LOCK_ACQUIRED) {
  323. assert(self->rlock_count == 0);
  324. self->rlock_owner = tid;
  325. self->rlock_count = 1;
  326. }
  327. else if (r == PY_LOCK_INTR) {
  328. return NULL;
  329. }
  330. return PyBool_FromLong(r == PY_LOCK_ACQUIRED);
  331. }
  332. PyDoc_STRVAR(rlock_acquire_doc,
  333. "acquire(blocking=True) -> bool\n\
  334. \n\
  335. Lock the lock. `blocking` indicates whether we should wait\n\
  336. for the lock to be available or not. If `blocking` is False\n\
  337. and another thread holds the lock, the method will return False\n\
  338. immediately. If `blocking` is True and another thread holds\n\
  339. the lock, the method will wait for the lock to be released,\n\
  340. take it and then return True.\n\
  341. (note: the blocking operation is interruptible.)\n\
  342. \n\
  343. In all other cases, the method will return True immediately.\n\
  344. Precisely, if the current thread already holds the lock, its\n\
  345. internal counter is simply incremented. If nobody holds the lock,\n\
  346. the lock is taken and its internal counter initialized to 1.");
  347. static PyObject *
  348. rlock_release(rlockobject *self, PyObject *Py_UNUSED(ignored))
  349. {
  350. unsigned long tid = PyThread_get_thread_ident();
  351. if (self->rlock_count == 0 || self->rlock_owner != tid) {
  352. PyErr_SetString(PyExc_RuntimeError,
  353. "cannot release un-acquired lock");
  354. return NULL;
  355. }
  356. if (--self->rlock_count == 0) {
  357. self->rlock_owner = 0;
  358. PyThread_release_lock(self->rlock_lock);
  359. }
  360. Py_RETURN_NONE;
  361. }
  362. PyDoc_STRVAR(rlock_release_doc,
  363. "release()\n\
  364. \n\
  365. Release the lock, allowing another thread that is blocked waiting for\n\
  366. the lock to acquire the lock. The lock must be in the locked state,\n\
  367. and must be locked by the same thread that unlocks it; otherwise a\n\
  368. `RuntimeError` is raised.\n\
  369. \n\
  370. Do note that if the lock was acquire()d several times in a row by the\n\
  371. current thread, release() needs to be called as many times for the lock\n\
  372. to be available for other threads.");
  373. static PyObject *
  374. rlock_acquire_restore(rlockobject *self, PyObject *args)
  375. {
  376. unsigned long owner;
  377. unsigned long count;
  378. int r = 1;
  379. if (!PyArg_ParseTuple(args, "(kk):_acquire_restore", &count, &owner))
  380. return NULL;
  381. if (!PyThread_acquire_lock(self->rlock_lock, 0)) {
  382. Py_BEGIN_ALLOW_THREADS
  383. r = PyThread_acquire_lock(self->rlock_lock, 1);
  384. Py_END_ALLOW_THREADS
  385. }
  386. if (!r) {
  387. PyErr_SetString(ThreadError, "couldn't acquire lock");
  388. return NULL;
  389. }
  390. assert(self->rlock_count == 0);
  391. self->rlock_owner = owner;
  392. self->rlock_count = count;
  393. Py_RETURN_NONE;
  394. }
  395. PyDoc_STRVAR(rlock_acquire_restore_doc,
  396. "_acquire_restore(state) -> None\n\
  397. \n\
  398. For internal use by `threading.Condition`.");
  399. static PyObject *
  400. rlock_release_save(rlockobject *self, PyObject *Py_UNUSED(ignored))
  401. {
  402. unsigned long owner;
  403. unsigned long count;
  404. if (self->rlock_count == 0) {
  405. PyErr_SetString(PyExc_RuntimeError,
  406. "cannot release un-acquired lock");
  407. return NULL;
  408. }
  409. owner = self->rlock_owner;
  410. count = self->rlock_count;
  411. self->rlock_count = 0;
  412. self->rlock_owner = 0;
  413. PyThread_release_lock(self->rlock_lock);
  414. return Py_BuildValue("kk", count, owner);
  415. }
  416. PyDoc_STRVAR(rlock_release_save_doc,
  417. "_release_save() -> tuple\n\
  418. \n\
  419. For internal use by `threading.Condition`.");
  420. static PyObject *
  421. rlock_recursion_count(rlockobject *self, PyObject *Py_UNUSED(ignored))
  422. {
  423. unsigned long tid = PyThread_get_thread_ident();
  424. return PyLong_FromUnsignedLong(
  425. self->rlock_owner == tid ? self->rlock_count : 0UL);
  426. }
  427. PyDoc_STRVAR(rlock_recursion_count_doc,
  428. "_recursion_count() -> int\n\
  429. \n\
  430. For internal use by reentrancy checks.");
  431. static PyObject *
  432. rlock_is_owned(rlockobject *self, PyObject *Py_UNUSED(ignored))
  433. {
  434. unsigned long tid = PyThread_get_thread_ident();
  435. if (self->rlock_count > 0 && self->rlock_owner == tid) {
  436. Py_RETURN_TRUE;
  437. }
  438. Py_RETURN_FALSE;
  439. }
  440. PyDoc_STRVAR(rlock_is_owned_doc,
  441. "_is_owned() -> bool\n\
  442. \n\
  443. For internal use by `threading.Condition`.");
  444. static PyObject *
  445. rlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  446. {
  447. rlockobject *self = (rlockobject *) type->tp_alloc(type, 0);
  448. if (self == NULL) {
  449. return NULL;
  450. }
  451. self->in_weakreflist = NULL;
  452. self->rlock_owner = 0;
  453. self->rlock_count = 0;
  454. self->rlock_lock = PyThread_allocate_lock();
  455. if (self->rlock_lock == NULL) {
  456. Py_DECREF(self);
  457. PyErr_SetString(ThreadError, "can't allocate lock");
  458. return NULL;
  459. }
  460. return (PyObject *) self;
  461. }
  462. static PyObject *
  463. rlock_repr(rlockobject *self)
  464. {
  465. return PyUnicode_FromFormat("<%s %s object owner=%ld count=%lu at %p>",
  466. self->rlock_count ? "locked" : "unlocked",
  467. Py_TYPE(self)->tp_name, self->rlock_owner,
  468. self->rlock_count, self);
  469. }
  470. #ifdef HAVE_FORK
  471. static PyObject *
  472. rlock__at_fork_reinit(rlockobject *self, PyObject *Py_UNUSED(args))
  473. {
  474. if (_PyThread_at_fork_reinit(&self->rlock_lock) < 0) {
  475. PyErr_SetString(ThreadError, "failed to reinitialize lock at fork");
  476. return NULL;
  477. }
  478. self->rlock_owner = 0;
  479. self->rlock_count = 0;
  480. Py_RETURN_NONE;
  481. }
  482. #endif /* HAVE_FORK */
  483. static PyMethodDef rlock_methods[] = {
  484. {"acquire", _PyCFunction_CAST(rlock_acquire),
  485. METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc},
  486. {"release", (PyCFunction)rlock_release,
  487. METH_NOARGS, rlock_release_doc},
  488. {"_is_owned", (PyCFunction)rlock_is_owned,
  489. METH_NOARGS, rlock_is_owned_doc},
  490. {"_acquire_restore", (PyCFunction)rlock_acquire_restore,
  491. METH_VARARGS, rlock_acquire_restore_doc},
  492. {"_release_save", (PyCFunction)rlock_release_save,
  493. METH_NOARGS, rlock_release_save_doc},
  494. {"_recursion_count", (PyCFunction)rlock_recursion_count,
  495. METH_NOARGS, rlock_recursion_count_doc},
  496. {"__enter__", _PyCFunction_CAST(rlock_acquire),
  497. METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc},
  498. {"__exit__", (PyCFunction)rlock_release,
  499. METH_VARARGS, rlock_release_doc},
  500. #ifdef HAVE_FORK
  501. {"_at_fork_reinit", (PyCFunction)rlock__at_fork_reinit,
  502. METH_NOARGS, NULL},
  503. #endif
  504. {NULL, NULL} /* sentinel */
  505. };
  506. static PyMemberDef rlock_type_members[] = {
  507. {"__weaklistoffset__", T_PYSSIZET, offsetof(rlockobject, in_weakreflist), READONLY},
  508. {NULL},
  509. };
  510. static PyType_Slot rlock_type_slots[] = {
  511. {Py_tp_dealloc, (destructor)rlock_dealloc},
  512. {Py_tp_repr, (reprfunc)rlock_repr},
  513. {Py_tp_methods, rlock_methods},
  514. {Py_tp_alloc, PyType_GenericAlloc},
  515. {Py_tp_new, rlock_new},
  516. {Py_tp_members, rlock_type_members},
  517. {Py_tp_traverse, rlock_traverse},
  518. {0, 0},
  519. };
  520. static PyType_Spec rlock_type_spec = {
  521. .name = "_thread.RLock",
  522. .basicsize = sizeof(rlockobject),
  523. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
  524. Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
  525. .slots = rlock_type_slots,
  526. };
  527. static lockobject *
  528. newlockobject(PyObject *module)
  529. {
  530. thread_module_state *state = get_thread_state(module);
  531. PyTypeObject *type = state->lock_type;
  532. lockobject *self = (lockobject *)type->tp_alloc(type, 0);
  533. if (self == NULL) {
  534. return NULL;
  535. }
  536. self->lock_lock = PyThread_allocate_lock();
  537. self->locked = 0;
  538. self->in_weakreflist = NULL;
  539. if (self->lock_lock == NULL) {
  540. Py_DECREF(self);
  541. PyErr_SetString(ThreadError, "can't allocate lock");
  542. return NULL;
  543. }
  544. return self;
  545. }
  546. /* Thread-local objects */
  547. /* Quick overview:
  548. We need to be able to reclaim reference cycles as soon as possible
  549. (both when a thread is being terminated, or a thread-local object
  550. becomes unreachable from user data). Constraints:
  551. - it must not be possible for thread-state dicts to be involved in
  552. reference cycles (otherwise the cyclic GC will refuse to consider
  553. objects referenced from a reachable thread-state dict, even though
  554. local_dealloc would clear them)
  555. - the death of a thread-state dict must still imply destruction of the
  556. corresponding local dicts in all thread-local objects.
  557. Our implementation uses small "localdummy" objects in order to break
  558. the reference chain. These trivial objects are hashable (using the
  559. default scheme of identity hashing) and weakrefable.
  560. Each thread-state holds a separate localdummy for each local object
  561. (as a /strong reference/),
  562. and each thread-local object holds a dict mapping /weak references/
  563. of localdummies to local dicts.
  564. Therefore:
  565. - only the thread-state dict holds a strong reference to the dummies
  566. - only the thread-local object holds a strong reference to the local dicts
  567. - only outside objects (application- or library-level) hold strong
  568. references to the thread-local objects
  569. - as soon as a thread-state dict is destroyed, the weakref callbacks of all
  570. dummies attached to that thread are called, and destroy the corresponding
  571. local dicts from thread-local objects
  572. - as soon as a thread-local object is destroyed, its local dicts are
  573. destroyed and its dummies are manually removed from all thread states
  574. - the GC can do its work correctly when a thread-local object is dangling,
  575. without any interference from the thread-state dicts
  576. As an additional optimization, each localdummy holds a borrowed reference
  577. to the corresponding localdict. This borrowed reference is only used
  578. by the thread-local object which has created the localdummy, which should
  579. guarantee that the localdict still exists when accessed.
  580. */
  581. typedef struct {
  582. PyObject_HEAD
  583. PyObject *localdict; /* Borrowed reference! */
  584. PyObject *weakreflist; /* List of weak references to self */
  585. } localdummyobject;
  586. static void
  587. localdummy_dealloc(localdummyobject *self)
  588. {
  589. if (self->weakreflist != NULL)
  590. PyObject_ClearWeakRefs((PyObject *) self);
  591. PyTypeObject *tp = Py_TYPE(self);
  592. tp->tp_free((PyObject*)self);
  593. Py_DECREF(tp);
  594. }
  595. static PyMemberDef local_dummy_type_members[] = {
  596. {"__weaklistoffset__", T_PYSSIZET, offsetof(localdummyobject, weakreflist), READONLY},
  597. {NULL},
  598. };
  599. static PyType_Slot local_dummy_type_slots[] = {
  600. {Py_tp_dealloc, (destructor)localdummy_dealloc},
  601. {Py_tp_doc, "Thread-local dummy"},
  602. {Py_tp_members, local_dummy_type_members},
  603. {0, 0}
  604. };
  605. static PyType_Spec local_dummy_type_spec = {
  606. .name = "_thread._localdummy",
  607. .basicsize = sizeof(localdummyobject),
  608. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
  609. Py_TPFLAGS_IMMUTABLETYPE),
  610. .slots = local_dummy_type_slots,
  611. };
  612. typedef struct {
  613. PyObject_HEAD
  614. PyObject *key;
  615. PyObject *args;
  616. PyObject *kw;
  617. PyObject *weakreflist; /* List of weak references to self */
  618. /* A {localdummy weakref -> localdict} dict */
  619. PyObject *dummies;
  620. /* The callback for weakrefs to localdummies */
  621. PyObject *wr_callback;
  622. } localobject;
  623. /* Forward declaration */
  624. static PyObject *_ldict(localobject *self, thread_module_state *state);
  625. static PyObject *_localdummy_destroyed(PyObject *meth_self, PyObject *dummyweakref);
  626. /* Create and register the dummy for the current thread.
  627. Returns a borrowed reference of the corresponding local dict */
  628. static PyObject *
  629. _local_create_dummy(localobject *self, thread_module_state *state)
  630. {
  631. PyObject *ldict = NULL, *wr = NULL;
  632. localdummyobject *dummy = NULL;
  633. PyTypeObject *type = state->local_dummy_type;
  634. PyObject *tdict = PyThreadState_GetDict();
  635. if (tdict == NULL) {
  636. PyErr_SetString(PyExc_SystemError,
  637. "Couldn't get thread-state dictionary");
  638. goto err;
  639. }
  640. ldict = PyDict_New();
  641. if (ldict == NULL) {
  642. goto err;
  643. }
  644. dummy = (localdummyobject *) type->tp_alloc(type, 0);
  645. if (dummy == NULL) {
  646. goto err;
  647. }
  648. dummy->localdict = ldict;
  649. wr = PyWeakref_NewRef((PyObject *) dummy, self->wr_callback);
  650. if (wr == NULL) {
  651. goto err;
  652. }
  653. /* As a side-effect, this will cache the weakref's hash before the
  654. dummy gets deleted */
  655. int r = PyDict_SetItem(self->dummies, wr, ldict);
  656. if (r < 0) {
  657. goto err;
  658. }
  659. Py_CLEAR(wr);
  660. r = PyDict_SetItem(tdict, self->key, (PyObject *) dummy);
  661. if (r < 0) {
  662. goto err;
  663. }
  664. Py_CLEAR(dummy);
  665. Py_DECREF(ldict);
  666. return ldict;
  667. err:
  668. Py_XDECREF(ldict);
  669. Py_XDECREF(wr);
  670. Py_XDECREF(dummy);
  671. return NULL;
  672. }
  673. static PyObject *
  674. local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
  675. {
  676. static PyMethodDef wr_callback_def = {
  677. "_localdummy_destroyed", (PyCFunction) _localdummy_destroyed, METH_O
  678. };
  679. if (type->tp_init == PyBaseObject_Type.tp_init) {
  680. int rc = 0;
  681. if (args != NULL)
  682. rc = PyObject_IsTrue(args);
  683. if (rc == 0 && kw != NULL)
  684. rc = PyObject_IsTrue(kw);
  685. if (rc != 0) {
  686. if (rc > 0) {
  687. PyErr_SetString(PyExc_TypeError,
  688. "Initialization arguments are not supported");
  689. }
  690. return NULL;
  691. }
  692. }
  693. PyObject *module = PyType_GetModuleByDef(type, &thread_module);
  694. thread_module_state *state = get_thread_state(module);
  695. localobject *self = (localobject *)type->tp_alloc(type, 0);
  696. if (self == NULL) {
  697. return NULL;
  698. }
  699. self->args = Py_XNewRef(args);
  700. self->kw = Py_XNewRef(kw);
  701. self->key = PyUnicode_FromFormat("thread.local.%p", self);
  702. if (self->key == NULL) {
  703. goto err;
  704. }
  705. self->dummies = PyDict_New();
  706. if (self->dummies == NULL) {
  707. goto err;
  708. }
  709. /* We use a weak reference to self in the callback closure
  710. in order to avoid spurious reference cycles */
  711. PyObject *wr = PyWeakref_NewRef((PyObject *) self, NULL);
  712. if (wr == NULL) {
  713. goto err;
  714. }
  715. self->wr_callback = PyCFunction_NewEx(&wr_callback_def, wr, NULL);
  716. Py_DECREF(wr);
  717. if (self->wr_callback == NULL) {
  718. goto err;
  719. }
  720. if (_local_create_dummy(self, state) == NULL) {
  721. goto err;
  722. }
  723. return (PyObject *)self;
  724. err:
  725. Py_DECREF(self);
  726. return NULL;
  727. }
  728. static int
  729. local_traverse(localobject *self, visitproc visit, void *arg)
  730. {
  731. Py_VISIT(Py_TYPE(self));
  732. Py_VISIT(self->args);
  733. Py_VISIT(self->kw);
  734. Py_VISIT(self->dummies);
  735. return 0;
  736. }
  737. static int
  738. local_clear(localobject *self)
  739. {
  740. Py_CLEAR(self->args);
  741. Py_CLEAR(self->kw);
  742. Py_CLEAR(self->dummies);
  743. Py_CLEAR(self->wr_callback);
  744. /* Remove all strong references to dummies from the thread states */
  745. if (self->key) {
  746. PyInterpreterState *interp = _PyInterpreterState_GET();
  747. _PyRuntimeState *runtime = &_PyRuntime;
  748. HEAD_LOCK(runtime);
  749. PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
  750. HEAD_UNLOCK(runtime);
  751. while (tstate) {
  752. if (tstate->dict) {
  753. PyObject *v = _PyDict_Pop(tstate->dict, self->key, Py_None);
  754. if (v != NULL) {
  755. Py_DECREF(v);
  756. }
  757. else {
  758. PyErr_Clear();
  759. }
  760. }
  761. HEAD_LOCK(runtime);
  762. tstate = PyThreadState_Next(tstate);
  763. HEAD_UNLOCK(runtime);
  764. }
  765. }
  766. return 0;
  767. }
  768. static void
  769. local_dealloc(localobject *self)
  770. {
  771. /* Weakrefs must be invalidated right now, otherwise they can be used
  772. from code called below, which is very dangerous since Py_REFCNT(self) == 0 */
  773. if (self->weakreflist != NULL) {
  774. PyObject_ClearWeakRefs((PyObject *) self);
  775. }
  776. PyObject_GC_UnTrack(self);
  777. local_clear(self);
  778. Py_XDECREF(self->key);
  779. PyTypeObject *tp = Py_TYPE(self);
  780. tp->tp_free((PyObject*)self);
  781. Py_DECREF(tp);
  782. }
  783. /* Returns a borrowed reference to the local dict, creating it if necessary */
  784. static PyObject *
  785. _ldict(localobject *self, thread_module_state *state)
  786. {
  787. PyObject *tdict = PyThreadState_GetDict();
  788. if (tdict == NULL) {
  789. PyErr_SetString(PyExc_SystemError,
  790. "Couldn't get thread-state dictionary");
  791. return NULL;
  792. }
  793. PyObject *ldict;
  794. PyObject *dummy = PyDict_GetItemWithError(tdict, self->key);
  795. if (dummy == NULL) {
  796. if (PyErr_Occurred()) {
  797. return NULL;
  798. }
  799. ldict = _local_create_dummy(self, state);
  800. if (ldict == NULL)
  801. return NULL;
  802. if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init &&
  803. Py_TYPE(self)->tp_init((PyObject*)self,
  804. self->args, self->kw) < 0) {
  805. /* we need to get rid of ldict from thread so
  806. we create a new one the next time we do an attr
  807. access */
  808. PyDict_DelItem(tdict, self->key);
  809. return NULL;
  810. }
  811. }
  812. else {
  813. assert(Py_IS_TYPE(dummy, state->local_dummy_type));
  814. ldict = ((localdummyobject *) dummy)->localdict;
  815. }
  816. return ldict;
  817. }
  818. static int
  819. local_setattro(localobject *self, PyObject *name, PyObject *v)
  820. {
  821. PyObject *module = PyType_GetModuleByDef(Py_TYPE(self), &thread_module);
  822. thread_module_state *state = get_thread_state(module);
  823. PyObject *ldict = _ldict(self, state);
  824. if (ldict == NULL) {
  825. return -1;
  826. }
  827. int r = PyObject_RichCompareBool(name, &_Py_ID(__dict__), Py_EQ);
  828. if (r == -1) {
  829. return -1;
  830. }
  831. if (r == 1) {
  832. PyErr_Format(PyExc_AttributeError,
  833. "'%.100s' object attribute '%U' is read-only",
  834. Py_TYPE(self)->tp_name, name);
  835. return -1;
  836. }
  837. return _PyObject_GenericSetAttrWithDict((PyObject *)self, name, v, ldict);
  838. }
  839. static PyObject *local_getattro(localobject *, PyObject *);
  840. static PyMemberDef local_type_members[] = {
  841. {"__weaklistoffset__", T_PYSSIZET, offsetof(localobject, weakreflist), READONLY},
  842. {NULL},
  843. };
  844. static PyType_Slot local_type_slots[] = {
  845. {Py_tp_dealloc, (destructor)local_dealloc},
  846. {Py_tp_getattro, (getattrofunc)local_getattro},
  847. {Py_tp_setattro, (setattrofunc)local_setattro},
  848. {Py_tp_doc, "Thread-local data"},
  849. {Py_tp_traverse, (traverseproc)local_traverse},
  850. {Py_tp_clear, (inquiry)local_clear},
  851. {Py_tp_new, local_new},
  852. {Py_tp_members, local_type_members},
  853. {0, 0}
  854. };
  855. static PyType_Spec local_type_spec = {
  856. .name = "_thread._local",
  857. .basicsize = sizeof(localobject),
  858. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
  859. Py_TPFLAGS_IMMUTABLETYPE),
  860. .slots = local_type_slots,
  861. };
  862. static PyObject *
  863. local_getattro(localobject *self, PyObject *name)
  864. {
  865. PyObject *module = PyType_GetModuleByDef(Py_TYPE(self), &thread_module);
  866. thread_module_state *state = get_thread_state(module);
  867. PyObject *ldict = _ldict(self, state);
  868. if (ldict == NULL)
  869. return NULL;
  870. int r = PyObject_RichCompareBool(name, &_Py_ID(__dict__), Py_EQ);
  871. if (r == 1) {
  872. return Py_NewRef(ldict);
  873. }
  874. if (r == -1) {
  875. return NULL;
  876. }
  877. if (!Py_IS_TYPE(self, state->local_type)) {
  878. /* use generic lookup for subtypes */
  879. return _PyObject_GenericGetAttrWithDict((PyObject *)self, name,
  880. ldict, 0);
  881. }
  882. /* Optimization: just look in dict ourselves */
  883. PyObject *value = PyDict_GetItemWithError(ldict, name);
  884. if (value != NULL) {
  885. return Py_NewRef(value);
  886. }
  887. if (PyErr_Occurred()) {
  888. return NULL;
  889. }
  890. /* Fall back on generic to get __class__ and __dict__ */
  891. return _PyObject_GenericGetAttrWithDict(
  892. (PyObject *)self, name, ldict, 0);
  893. }
  894. /* Called when a dummy is destroyed. */
  895. static PyObject *
  896. _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref)
  897. {
  898. assert(PyWeakref_CheckRef(localweakref));
  899. PyObject *obj = PyWeakref_GET_OBJECT(localweakref);
  900. if (obj == Py_None) {
  901. Py_RETURN_NONE;
  902. }
  903. /* If the thread-local object is still alive and not being cleared,
  904. remove the corresponding local dict */
  905. localobject *self = (localobject *)Py_NewRef(obj);
  906. if (self->dummies != NULL) {
  907. PyObject *ldict;
  908. ldict = PyDict_GetItemWithError(self->dummies, dummyweakref);
  909. if (ldict != NULL) {
  910. PyDict_DelItem(self->dummies, dummyweakref);
  911. }
  912. if (PyErr_Occurred())
  913. PyErr_WriteUnraisable(obj);
  914. }
  915. Py_DECREF(obj);
  916. Py_RETURN_NONE;
  917. }
  918. /* Module functions */
  919. struct bootstate {
  920. PyThreadState *tstate;
  921. PyObject *func;
  922. PyObject *args;
  923. PyObject *kwargs;
  924. };
  925. static void
  926. thread_bootstate_free(struct bootstate *boot, int decref)
  927. {
  928. if (decref) {
  929. Py_DECREF(boot->func);
  930. Py_DECREF(boot->args);
  931. Py_XDECREF(boot->kwargs);
  932. }
  933. PyMem_RawFree(boot);
  934. }
  935. static void
  936. thread_run(void *boot_raw)
  937. {
  938. struct bootstate *boot = (struct bootstate *) boot_raw;
  939. PyThreadState *tstate = boot->tstate;
  940. // gh-108987: If _thread.start_new_thread() is called before or while
  941. // Python is being finalized, thread_run() can called *after*.
  942. // _PyRuntimeState_SetFinalizing() is called. At this point, all Python
  943. // threads must exit, except of the thread calling Py_Finalize() whch holds
  944. // the GIL and must not exit.
  945. //
  946. // At this stage, tstate can be a dangling pointer (point to freed memory),
  947. // it's ok to call _PyThreadState_MustExit() with a dangling pointer.
  948. if (_PyThreadState_MustExit(tstate)) {
  949. // Don't call PyThreadState_Clear() nor _PyThreadState_DeleteCurrent().
  950. // These functions are called on tstate indirectly by Py_Finalize()
  951. // which calls _PyInterpreterState_Clear().
  952. //
  953. // Py_DECREF() cannot be called because the GIL is not held: leak
  954. // references on purpose. Python is being finalized anyway.
  955. thread_bootstate_free(boot, 0);
  956. goto exit;
  957. }
  958. _PyThreadState_Bind(tstate);
  959. PyEval_AcquireThread(tstate);
  960. tstate->interp->threads.count++;
  961. PyObject *res = PyObject_Call(boot->func, boot->args, boot->kwargs);
  962. if (res == NULL) {
  963. if (PyErr_ExceptionMatches(PyExc_SystemExit))
  964. /* SystemExit is ignored silently */
  965. PyErr_Clear();
  966. else {
  967. _PyErr_WriteUnraisableMsg("in thread started by", boot->func);
  968. }
  969. }
  970. else {
  971. Py_DECREF(res);
  972. }
  973. thread_bootstate_free(boot, 1);
  974. tstate->interp->threads.count--;
  975. PyThreadState_Clear(tstate);
  976. _PyThreadState_DeleteCurrent(tstate);
  977. exit:
  978. // bpo-44434: Don't call explicitly PyThread_exit_thread(). On Linux with
  979. // the glibc, pthread_exit() can abort the whole process if dlopen() fails
  980. // to open the libgcc_s.so library (ex: EMFILE error).
  981. return;
  982. }
  983. static PyObject *
  984. thread_daemon_threads_allowed(PyObject *module, PyObject *Py_UNUSED(ignored))
  985. {
  986. PyInterpreterState *interp = _PyInterpreterState_Get();
  987. if (interp->feature_flags & Py_RTFLAGS_DAEMON_THREADS) {
  988. Py_RETURN_TRUE;
  989. }
  990. else {
  991. Py_RETURN_FALSE;
  992. }
  993. }
  994. PyDoc_STRVAR(daemon_threads_allowed_doc,
  995. "daemon_threads_allowed()\n\
  996. \n\
  997. Return True if daemon threads are allowed in the current interpreter,\n\
  998. and False otherwise.\n");
  999. static PyObject *
  1000. thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
  1001. {
  1002. PyObject *func, *args, *kwargs = NULL;
  1003. if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
  1004. &func, &args, &kwargs))
  1005. return NULL;
  1006. if (!PyCallable_Check(func)) {
  1007. PyErr_SetString(PyExc_TypeError,
  1008. "first arg must be callable");
  1009. return NULL;
  1010. }
  1011. if (!PyTuple_Check(args)) {
  1012. PyErr_SetString(PyExc_TypeError,
  1013. "2nd arg must be a tuple");
  1014. return NULL;
  1015. }
  1016. if (kwargs != NULL && !PyDict_Check(kwargs)) {
  1017. PyErr_SetString(PyExc_TypeError,
  1018. "optional 3rd arg must be a dictionary");
  1019. return NULL;
  1020. }
  1021. if (PySys_Audit("_thread.start_new_thread", "OOO",
  1022. func, args, kwargs ? kwargs : Py_None) < 0) {
  1023. return NULL;
  1024. }
  1025. PyInterpreterState *interp = _PyInterpreterState_GET();
  1026. if (!_PyInterpreterState_HasFeature(interp, Py_RTFLAGS_THREADS)) {
  1027. PyErr_SetString(PyExc_RuntimeError,
  1028. "thread is not supported for isolated subinterpreters");
  1029. return NULL;
  1030. }
  1031. if (interp->finalizing) {
  1032. PyErr_SetString(PyExc_RuntimeError,
  1033. "can't create new thread at interpreter shutdown");
  1034. return NULL;
  1035. }
  1036. // gh-109795: Use PyMem_RawMalloc() instead of PyMem_Malloc(),
  1037. // because it should be possible to call thread_bootstate_free()
  1038. // without holding the GIL.
  1039. struct bootstate *boot = PyMem_RawMalloc(sizeof(struct bootstate));
  1040. if (boot == NULL) {
  1041. return PyErr_NoMemory();
  1042. }
  1043. boot->tstate = _PyThreadState_New(interp);
  1044. if (boot->tstate == NULL) {
  1045. PyMem_RawFree(boot);
  1046. if (!PyErr_Occurred()) {
  1047. return PyErr_NoMemory();
  1048. }
  1049. return NULL;
  1050. }
  1051. boot->func = Py_NewRef(func);
  1052. boot->args = Py_NewRef(args);
  1053. boot->kwargs = Py_XNewRef(kwargs);
  1054. unsigned long ident = PyThread_start_new_thread(thread_run, (void*) boot);
  1055. if (ident == PYTHREAD_INVALID_THREAD_ID) {
  1056. PyErr_SetString(ThreadError, "can't start new thread");
  1057. PyThreadState_Clear(boot->tstate);
  1058. thread_bootstate_free(boot, 1);
  1059. return NULL;
  1060. }
  1061. return PyLong_FromUnsignedLong(ident);
  1062. }
  1063. PyDoc_STRVAR(start_new_doc,
  1064. "start_new_thread(function, args[, kwargs])\n\
  1065. (start_new() is an obsolete synonym)\n\
  1066. \n\
  1067. Start a new thread and return its identifier. The thread will call the\n\
  1068. function with positional arguments from the tuple args and keyword arguments\n\
  1069. taken from the optional dictionary kwargs. The thread exits when the\n\
  1070. function returns; the return value is ignored. The thread will also exit\n\
  1071. when the function raises an unhandled exception; a stack trace will be\n\
  1072. printed unless the exception is SystemExit.\n");
  1073. static PyObject *
  1074. thread_PyThread_exit_thread(PyObject *self, PyObject *Py_UNUSED(ignored))
  1075. {
  1076. PyErr_SetNone(PyExc_SystemExit);
  1077. return NULL;
  1078. }
  1079. PyDoc_STRVAR(exit_doc,
  1080. "exit()\n\
  1081. (exit_thread() is an obsolete synonym)\n\
  1082. \n\
  1083. This is synonymous to ``raise SystemExit''. It will cause the current\n\
  1084. thread to exit silently unless the exception is caught.");
  1085. static PyObject *
  1086. thread_PyThread_interrupt_main(PyObject *self, PyObject *args)
  1087. {
  1088. int signum = SIGINT;
  1089. if (!PyArg_ParseTuple(args, "|i:signum", &signum)) {
  1090. return NULL;
  1091. }
  1092. if (PyErr_SetInterruptEx(signum)) {
  1093. PyErr_SetString(PyExc_ValueError, "signal number out of range");
  1094. return NULL;
  1095. }
  1096. Py_RETURN_NONE;
  1097. }
  1098. PyDoc_STRVAR(interrupt_doc,
  1099. "interrupt_main(signum=signal.SIGINT, /)\n\
  1100. \n\
  1101. Simulate the arrival of the given signal in the main thread,\n\
  1102. where the corresponding signal handler will be executed.\n\
  1103. If *signum* is omitted, SIGINT is assumed.\n\
  1104. A subthread can use this function to interrupt the main thread.\n\
  1105. \n\
  1106. Note: the default signal handler for SIGINT raises ``KeyboardInterrupt``."
  1107. );
  1108. static lockobject *newlockobject(PyObject *module);
  1109. static PyObject *
  1110. thread_PyThread_allocate_lock(PyObject *module, PyObject *Py_UNUSED(ignored))
  1111. {
  1112. return (PyObject *) newlockobject(module);
  1113. }
  1114. PyDoc_STRVAR(allocate_doc,
  1115. "allocate_lock() -> lock object\n\
  1116. (allocate() is an obsolete synonym)\n\
  1117. \n\
  1118. Create a new lock object. See help(type(threading.Lock())) for\n\
  1119. information about locks.");
  1120. static PyObject *
  1121. thread_get_ident(PyObject *self, PyObject *Py_UNUSED(ignored))
  1122. {
  1123. unsigned long ident = PyThread_get_thread_ident();
  1124. if (ident == PYTHREAD_INVALID_THREAD_ID) {
  1125. PyErr_SetString(ThreadError, "no current thread ident");
  1126. return NULL;
  1127. }
  1128. return PyLong_FromUnsignedLong(ident);
  1129. }
  1130. PyDoc_STRVAR(get_ident_doc,
  1131. "get_ident() -> integer\n\
  1132. \n\
  1133. Return a non-zero integer that uniquely identifies the current thread\n\
  1134. amongst other threads that exist simultaneously.\n\
  1135. This may be used to identify per-thread resources.\n\
  1136. Even though on some platforms threads identities may appear to be\n\
  1137. allocated consecutive numbers starting at 1, this behavior should not\n\
  1138. be relied upon, and the number should be seen purely as a magic cookie.\n\
  1139. A thread's identity may be reused for another thread after it exits.");
  1140. #ifdef PY_HAVE_THREAD_NATIVE_ID
  1141. static PyObject *
  1142. thread_get_native_id(PyObject *self, PyObject *Py_UNUSED(ignored))
  1143. {
  1144. unsigned long native_id = PyThread_get_thread_native_id();
  1145. return PyLong_FromUnsignedLong(native_id);
  1146. }
  1147. PyDoc_STRVAR(get_native_id_doc,
  1148. "get_native_id() -> integer\n\
  1149. \n\
  1150. Return a non-negative integer identifying the thread as reported\n\
  1151. by the OS (kernel). This may be used to uniquely identify a\n\
  1152. particular thread within a system.");
  1153. #endif
  1154. static PyObject *
  1155. thread__count(PyObject *self, PyObject *Py_UNUSED(ignored))
  1156. {
  1157. PyInterpreterState *interp = _PyInterpreterState_GET();
  1158. return PyLong_FromLong(interp->threads.count);
  1159. }
  1160. PyDoc_STRVAR(_count_doc,
  1161. "_count() -> integer\n\
  1162. \n\
  1163. \
  1164. Return the number of currently running Python threads, excluding\n\
  1165. the main thread. The returned number comprises all threads created\n\
  1166. through `start_new_thread()` as well as `threading.Thread`, and not\n\
  1167. yet finished.\n\
  1168. \n\
  1169. This function is meant for internal and specialized purposes only.\n\
  1170. In most applications `threading.enumerate()` should be used instead.");
  1171. static void
  1172. release_sentinel(void *wr_raw)
  1173. {
  1174. PyObject *wr = _PyObject_CAST(wr_raw);
  1175. /* Tricky: this function is called when the current thread state
  1176. is being deleted. Therefore, only simple C code can safely
  1177. execute here. */
  1178. PyObject *obj = PyWeakref_GET_OBJECT(wr);
  1179. lockobject *lock;
  1180. if (obj != Py_None) {
  1181. lock = (lockobject *) obj;
  1182. if (lock->locked) {
  1183. PyThread_release_lock(lock->lock_lock);
  1184. lock->locked = 0;
  1185. }
  1186. }
  1187. /* Deallocating a weakref with a NULL callback only calls
  1188. PyObject_GC_Del(), which can't call any Python code. */
  1189. Py_DECREF(wr);
  1190. }
  1191. static PyObject *
  1192. thread__set_sentinel(PyObject *module, PyObject *Py_UNUSED(ignored))
  1193. {
  1194. PyObject *wr;
  1195. PyThreadState *tstate = _PyThreadState_GET();
  1196. lockobject *lock;
  1197. if (tstate->on_delete_data != NULL) {
  1198. /* We must support the re-creation of the lock from a
  1199. fork()ed child. */
  1200. assert(tstate->on_delete == &release_sentinel);
  1201. wr = (PyObject *) tstate->on_delete_data;
  1202. tstate->on_delete = NULL;
  1203. tstate->on_delete_data = NULL;
  1204. Py_DECREF(wr);
  1205. }
  1206. lock = newlockobject(module);
  1207. if (lock == NULL)
  1208. return NULL;
  1209. /* The lock is owned by whoever called _set_sentinel(), but the weakref
  1210. hangs to the thread state. */
  1211. wr = PyWeakref_NewRef((PyObject *) lock, NULL);
  1212. if (wr == NULL) {
  1213. Py_DECREF(lock);
  1214. return NULL;
  1215. }
  1216. tstate->on_delete_data = (void *) wr;
  1217. tstate->on_delete = &release_sentinel;
  1218. return (PyObject *) lock;
  1219. }
  1220. PyDoc_STRVAR(_set_sentinel_doc,
  1221. "_set_sentinel() -> lock\n\
  1222. \n\
  1223. Set a sentinel lock that will be released when the current thread\n\
  1224. state is finalized (after it is untied from the interpreter).\n\
  1225. \n\
  1226. This is a private API for the threading module.");
  1227. static PyObject *
  1228. thread_stack_size(PyObject *self, PyObject *args)
  1229. {
  1230. size_t old_size;
  1231. Py_ssize_t new_size = 0;
  1232. int rc;
  1233. if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
  1234. return NULL;
  1235. if (new_size < 0) {
  1236. PyErr_SetString(PyExc_ValueError,
  1237. "size must be 0 or a positive value");
  1238. return NULL;
  1239. }
  1240. old_size = PyThread_get_stacksize();
  1241. rc = PyThread_set_stacksize((size_t) new_size);
  1242. if (rc == -1) {
  1243. PyErr_Format(PyExc_ValueError,
  1244. "size not valid: %zd bytes",
  1245. new_size);
  1246. return NULL;
  1247. }
  1248. if (rc == -2) {
  1249. PyErr_SetString(ThreadError,
  1250. "setting stack size not supported");
  1251. return NULL;
  1252. }
  1253. return PyLong_FromSsize_t((Py_ssize_t) old_size);
  1254. }
  1255. PyDoc_STRVAR(stack_size_doc,
  1256. "stack_size([size]) -> size\n\
  1257. \n\
  1258. Return the thread stack size used when creating new threads. The\n\
  1259. optional size argument specifies the stack size (in bytes) to be used\n\
  1260. for subsequently created threads, and must be 0 (use platform or\n\
  1261. configured default) or a positive integer value of at least 32,768 (32k).\n\
  1262. If changing the thread stack size is unsupported, a ThreadError\n\
  1263. exception is raised. If the specified size is invalid, a ValueError\n\
  1264. exception is raised, and the stack size is unmodified. 32k bytes\n\
  1265. currently the minimum supported stack size value to guarantee\n\
  1266. sufficient stack space for the interpreter itself.\n\
  1267. \n\
  1268. Note that some platforms may have particular restrictions on values for\n\
  1269. the stack size, such as requiring a minimum stack size larger than 32 KiB or\n\
  1270. requiring allocation in multiples of the system memory page size\n\
  1271. - platform documentation should be referred to for more information\n\
  1272. (4 KiB pages are common; using multiples of 4096 for the stack size is\n\
  1273. the suggested approach in the absence of more specific information).");
  1274. static int
  1275. thread_excepthook_file(PyObject *file, PyObject *exc_type, PyObject *exc_value,
  1276. PyObject *exc_traceback, PyObject *thread)
  1277. {
  1278. /* print(f"Exception in thread {thread.name}:", file=file) */
  1279. if (PyFile_WriteString("Exception in thread ", file) < 0) {
  1280. return -1;
  1281. }
  1282. PyObject *name = NULL;
  1283. if (thread != Py_None) {
  1284. if (_PyObject_LookupAttr(thread, &_Py_ID(name), &name) < 0) {
  1285. return -1;
  1286. }
  1287. }
  1288. if (name != NULL) {
  1289. if (PyFile_WriteObject(name, file, Py_PRINT_RAW) < 0) {
  1290. Py_DECREF(name);
  1291. return -1;
  1292. }
  1293. Py_DECREF(name);
  1294. }
  1295. else {
  1296. unsigned long ident = PyThread_get_thread_ident();
  1297. PyObject *str = PyUnicode_FromFormat("%lu", ident);
  1298. if (str != NULL) {
  1299. if (PyFile_WriteObject(str, file, Py_PRINT_RAW) < 0) {
  1300. Py_DECREF(str);
  1301. return -1;
  1302. }
  1303. Py_DECREF(str);
  1304. }
  1305. else {
  1306. PyErr_Clear();
  1307. if (PyFile_WriteString("<failed to get thread name>", file) < 0) {
  1308. return -1;
  1309. }
  1310. }
  1311. }
  1312. if (PyFile_WriteString(":\n", file) < 0) {
  1313. return -1;
  1314. }
  1315. /* Display the traceback */
  1316. _PyErr_Display(file, exc_type, exc_value, exc_traceback);
  1317. /* Call file.flush() */
  1318. PyObject *res = PyObject_CallMethodNoArgs(file, &_Py_ID(flush));
  1319. if (!res) {
  1320. return -1;
  1321. }
  1322. Py_DECREF(res);
  1323. return 0;
  1324. }
  1325. PyDoc_STRVAR(ExceptHookArgs__doc__,
  1326. "ExceptHookArgs\n\
  1327. \n\
  1328. Type used to pass arguments to threading.excepthook.");
  1329. static PyStructSequence_Field ExceptHookArgs_fields[] = {
  1330. {"exc_type", "Exception type"},
  1331. {"exc_value", "Exception value"},
  1332. {"exc_traceback", "Exception traceback"},
  1333. {"thread", "Thread"},
  1334. {0}
  1335. };
  1336. static PyStructSequence_Desc ExceptHookArgs_desc = {
  1337. .name = "_thread._ExceptHookArgs",
  1338. .doc = ExceptHookArgs__doc__,
  1339. .fields = ExceptHookArgs_fields,
  1340. .n_in_sequence = 4
  1341. };
  1342. static PyObject *
  1343. thread_excepthook(PyObject *module, PyObject *args)
  1344. {
  1345. thread_module_state *state = get_thread_state(module);
  1346. if (!Py_IS_TYPE(args, state->excepthook_type)) {
  1347. PyErr_SetString(PyExc_TypeError,
  1348. "_thread.excepthook argument type "
  1349. "must be ExceptHookArgs");
  1350. return NULL;
  1351. }
  1352. /* Borrowed reference */
  1353. PyObject *exc_type = PyStructSequence_GET_ITEM(args, 0);
  1354. if (exc_type == PyExc_SystemExit) {
  1355. /* silently ignore SystemExit */
  1356. Py_RETURN_NONE;
  1357. }
  1358. /* Borrowed references */
  1359. PyObject *exc_value = PyStructSequence_GET_ITEM(args, 1);
  1360. PyObject *exc_tb = PyStructSequence_GET_ITEM(args, 2);
  1361. PyObject *thread = PyStructSequence_GET_ITEM(args, 3);
  1362. PyThreadState *tstate = _PyThreadState_GET();
  1363. PyObject *file = _PySys_GetAttr(tstate, &_Py_ID(stderr));
  1364. if (file == NULL || file == Py_None) {
  1365. if (thread == Py_None) {
  1366. /* do nothing if sys.stderr is None and thread is None */
  1367. Py_RETURN_NONE;
  1368. }
  1369. file = PyObject_GetAttrString(thread, "_stderr");
  1370. if (file == NULL) {
  1371. return NULL;
  1372. }
  1373. if (file == Py_None) {
  1374. Py_DECREF(file);
  1375. /* do nothing if sys.stderr is None and sys.stderr was None
  1376. when the thread was created */
  1377. Py_RETURN_NONE;
  1378. }
  1379. }
  1380. else {
  1381. Py_INCREF(file);
  1382. }
  1383. int res = thread_excepthook_file(file, exc_type, exc_value, exc_tb,
  1384. thread);
  1385. Py_DECREF(file);
  1386. if (res < 0) {
  1387. return NULL;
  1388. }
  1389. Py_RETURN_NONE;
  1390. }
  1391. PyDoc_STRVAR(excepthook_doc,
  1392. "excepthook(exc_type, exc_value, exc_traceback, thread)\n\
  1393. \n\
  1394. Handle uncaught Thread.run() exception.");
  1395. static PyObject *
  1396. thread__is_main_interpreter(PyObject *module, PyObject *Py_UNUSED(ignored))
  1397. {
  1398. PyInterpreterState *interp = _PyInterpreterState_GET();
  1399. return PyBool_FromLong(_Py_IsMainInterpreter(interp));
  1400. }
  1401. PyDoc_STRVAR(thread__is_main_interpreter_doc,
  1402. "_is_main_interpreter()\n\
  1403. \n\
  1404. Return True if the current interpreter is the main Python interpreter.");
  1405. static PyMethodDef thread_methods[] = {
  1406. {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
  1407. METH_VARARGS, start_new_doc},
  1408. {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
  1409. METH_VARARGS, start_new_doc},
  1410. {"daemon_threads_allowed", (PyCFunction)thread_daemon_threads_allowed,
  1411. METH_NOARGS, daemon_threads_allowed_doc},
  1412. {"allocate_lock", thread_PyThread_allocate_lock,
  1413. METH_NOARGS, allocate_doc},
  1414. {"allocate", thread_PyThread_allocate_lock,
  1415. METH_NOARGS, allocate_doc},
  1416. {"exit_thread", thread_PyThread_exit_thread,
  1417. METH_NOARGS, exit_doc},
  1418. {"exit", thread_PyThread_exit_thread,
  1419. METH_NOARGS, exit_doc},
  1420. {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,
  1421. METH_VARARGS, interrupt_doc},
  1422. {"get_ident", thread_get_ident,
  1423. METH_NOARGS, get_ident_doc},
  1424. #ifdef PY_HAVE_THREAD_NATIVE_ID
  1425. {"get_native_id", thread_get_native_id,
  1426. METH_NOARGS, get_native_id_doc},
  1427. #endif
  1428. {"_count", thread__count,
  1429. METH_NOARGS, _count_doc},
  1430. {"stack_size", (PyCFunction)thread_stack_size,
  1431. METH_VARARGS, stack_size_doc},
  1432. {"_set_sentinel", thread__set_sentinel,
  1433. METH_NOARGS, _set_sentinel_doc},
  1434. {"_excepthook", thread_excepthook,
  1435. METH_O, excepthook_doc},
  1436. {"_is_main_interpreter", thread__is_main_interpreter,
  1437. METH_NOARGS, thread__is_main_interpreter_doc},
  1438. {NULL, NULL} /* sentinel */
  1439. };
  1440. /* Initialization function */
  1441. static int
  1442. thread_module_exec(PyObject *module)
  1443. {
  1444. thread_module_state *state = get_thread_state(module);
  1445. PyObject *d = PyModule_GetDict(module);
  1446. // Initialize the C thread library
  1447. PyThread_init_thread();
  1448. // Lock
  1449. state->lock_type = (PyTypeObject *)PyType_FromSpec(&lock_type_spec);
  1450. if (state->lock_type == NULL) {
  1451. return -1;
  1452. }
  1453. if (PyDict_SetItemString(d, "LockType", (PyObject *)state->lock_type) < 0) {
  1454. return -1;
  1455. }
  1456. // RLock
  1457. PyTypeObject *rlock_type = (PyTypeObject *)PyType_FromSpec(&rlock_type_spec);
  1458. if (rlock_type == NULL) {
  1459. return -1;
  1460. }
  1461. if (PyModule_AddType(module, rlock_type) < 0) {
  1462. Py_DECREF(rlock_type);
  1463. return -1;
  1464. }
  1465. Py_DECREF(rlock_type);
  1466. // Local dummy
  1467. state->local_dummy_type = (PyTypeObject *)PyType_FromSpec(&local_dummy_type_spec);
  1468. if (state->local_dummy_type == NULL) {
  1469. return -1;
  1470. }
  1471. // Local
  1472. state->local_type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &local_type_spec, NULL);
  1473. if (state->local_type == NULL) {
  1474. return -1;
  1475. }
  1476. if (PyModule_AddType(module, state->local_type) < 0) {
  1477. return -1;
  1478. }
  1479. // Add module attributes
  1480. if (PyDict_SetItemString(d, "error", ThreadError) < 0) {
  1481. return -1;
  1482. }
  1483. // _ExceptHookArgs type
  1484. state->excepthook_type = PyStructSequence_NewType(&ExceptHookArgs_desc);
  1485. if (state->excepthook_type == NULL) {
  1486. return -1;
  1487. }
  1488. if (PyModule_AddType(module, state->excepthook_type) < 0) {
  1489. return -1;
  1490. }
  1491. // TIMEOUT_MAX
  1492. double timeout_max = (double)PY_TIMEOUT_MAX * 1e-6;
  1493. double time_max = _PyTime_AsSecondsDouble(_PyTime_MAX);
  1494. timeout_max = Py_MIN(timeout_max, time_max);
  1495. // Round towards minus infinity
  1496. timeout_max = floor(timeout_max);
  1497. if (_PyModule_Add(module, "TIMEOUT_MAX",
  1498. PyFloat_FromDouble(timeout_max)) < 0) {
  1499. return -1;
  1500. }
  1501. return 0;
  1502. }
  1503. static int
  1504. thread_module_traverse(PyObject *module, visitproc visit, void *arg)
  1505. {
  1506. thread_module_state *state = get_thread_state(module);
  1507. Py_VISIT(state->excepthook_type);
  1508. Py_VISIT(state->lock_type);
  1509. Py_VISIT(state->local_type);
  1510. Py_VISIT(state->local_dummy_type);
  1511. return 0;
  1512. }
  1513. static int
  1514. thread_module_clear(PyObject *module)
  1515. {
  1516. thread_module_state *state = get_thread_state(module);
  1517. Py_CLEAR(state->excepthook_type);
  1518. Py_CLEAR(state->lock_type);
  1519. Py_CLEAR(state->local_type);
  1520. Py_CLEAR(state->local_dummy_type);
  1521. return 0;
  1522. }
  1523. static void
  1524. thread_module_free(void *module)
  1525. {
  1526. thread_module_clear((PyObject *)module);
  1527. }
  1528. PyDoc_STRVAR(thread_doc,
  1529. "This module provides primitive operations to write multi-threaded programs.\n\
  1530. The 'threading' module provides a more convenient interface.");
  1531. static PyModuleDef_Slot thread_module_slots[] = {
  1532. {Py_mod_exec, thread_module_exec},
  1533. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  1534. {0, NULL}
  1535. };
  1536. static struct PyModuleDef thread_module = {
  1537. PyModuleDef_HEAD_INIT,
  1538. .m_name = "_thread",
  1539. .m_doc = thread_doc,
  1540. .m_size = sizeof(thread_module_state),
  1541. .m_methods = thread_methods,
  1542. .m_traverse = thread_module_traverse,
  1543. .m_clear = thread_module_clear,
  1544. .m_free = thread_module_free,
  1545. .m_slots = thread_module_slots,
  1546. };
  1547. PyMODINIT_FUNC
  1548. PyInit__thread(void)
  1549. {
  1550. return PyModuleDef_Init(&thread_module);
  1551. }