2 /* Thread and interpreter state structures and their interfaces */
6 /* --------------------------------------------------------------------------
9 Always use malloc() and free() directly in this file. A number of these
10 functions are advertised as safe to call when the GIL isn't held, and in
11 a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging
12 obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid
13 the expense of doing their own locking).
14 -------------------------------------------------------------------------- */
28 static PyThread_type_lock head_mutex
= NULL
; /* Protects interp->tstate_head */
29 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
30 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
31 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
37 /* The single PyInterpreterState used by this process'
38 GILState implementation
40 static PyInterpreterState
*autoInterpreterState
= NULL
;
41 static int autoTLSkey
= 0;
43 #define HEAD_INIT() /* Nothing */
44 #define HEAD_LOCK() /* Nothing */
45 #define HEAD_UNLOCK() /* Nothing */
48 static PyInterpreterState
*interp_head
= NULL
;
50 PyThreadState
*_PyThreadState_Current
= NULL
;
51 PyThreadFrameGetter _PyThreadState_GetFrame
= NULL
;
54 static void _PyGILState_NoteThreadState(PyThreadState
* tstate
);
59 PyInterpreterState_New(void)
61 PyInterpreterState
*interp
= (PyInterpreterState
*)
62 malloc(sizeof(PyInterpreterState
));
67 if (head_mutex
== NULL
)
68 Py_FatalError("Can't initialize threads for interpreter");
70 interp
->modules
= NULL
;
71 interp
->modules_reloading
= NULL
;
72 interp
->modules_by_index
= NULL
;
73 interp
->sysdict
= NULL
;
74 interp
->builtins
= NULL
;
75 interp
->tstate_head
= NULL
;
76 interp
->codec_search_path
= NULL
;
77 interp
->codec_search_cache
= NULL
;
78 interp
->codec_error_registry
= NULL
;
79 interp
->codecs_initialized
= 0;
82 interp
->dlopenflags
= RTLD_NOW
;
84 interp
->dlopenflags
= RTLD_LAZY
;
92 interp
->next
= interp_head
;
102 PyInterpreterState_Clear(PyInterpreterState
*interp
)
106 for (p
= interp
->tstate_head
; p
!= NULL
; p
= p
->next
)
107 PyThreadState_Clear(p
);
109 Py_CLEAR(interp
->codec_search_path
);
110 Py_CLEAR(interp
->codec_search_cache
);
111 Py_CLEAR(interp
->codec_error_registry
);
112 Py_CLEAR(interp
->modules
);
113 Py_CLEAR(interp
->modules_by_index
);
114 Py_CLEAR(interp
->modules_reloading
);
115 Py_CLEAR(interp
->sysdict
);
116 Py_CLEAR(interp
->builtins
);
121 zapthreads(PyInterpreterState
*interp
)
124 /* No need to lock the mutex here because this should only happen
125 when the threads are all really dead (XXX famous last words). */
126 while ((p
= interp
->tstate_head
) != NULL
) {
127 PyThreadState_Delete(p
);
133 PyInterpreterState_Delete(PyInterpreterState
*interp
)
135 PyInterpreterState
**p
;
138 for (p
= &interp_head
; ; p
= &(*p
)->next
) {
141 "PyInterpreterState_Delete: invalid interp");
145 if (interp
->tstate_head
!= NULL
)
146 Py_FatalError("PyInterpreterState_Delete: remaining threads");
153 /* Default implementation for _PyThreadState_GetFrame */
154 static struct _frame
*
155 threadstate_getframe(PyThreadState
*self
)
160 static PyThreadState
*
161 new_threadstate(PyInterpreterState
*interp
, int init
)
163 PyThreadState
*tstate
= (PyThreadState
*)malloc(sizeof(PyThreadState
));
165 if (_PyThreadState_GetFrame
== NULL
)
166 _PyThreadState_GetFrame
= threadstate_getframe
;
168 if (tstate
!= NULL
) {
169 tstate
->interp
= interp
;
171 tstate
->frame
= NULL
;
172 tstate
->recursion_depth
= 0;
173 tstate
->overflowed
= 0;
174 tstate
->recursion_critical
= 0;
176 tstate
->use_tracing
= 0;
177 tstate
->tick_counter
= 0;
178 tstate
->gilstate_counter
= 0;
179 tstate
->async_exc
= NULL
;
181 tstate
->thread_id
= PyThread_get_thread_ident();
183 tstate
->thread_id
= 0;
188 tstate
->curexc_type
= NULL
;
189 tstate
->curexc_value
= NULL
;
190 tstate
->curexc_traceback
= NULL
;
192 tstate
->exc_type
= NULL
;
193 tstate
->exc_value
= NULL
;
194 tstate
->exc_traceback
= NULL
;
196 tstate
->c_profilefunc
= NULL
;
197 tstate
->c_tracefunc
= NULL
;
198 tstate
->c_profileobj
= NULL
;
199 tstate
->c_traceobj
= NULL
;
202 _PyThreadState_Init(tstate
);
205 tstate
->next
= interp
->tstate_head
;
206 interp
->tstate_head
= tstate
;
214 PyThreadState_New(PyInterpreterState
*interp
)
216 return new_threadstate(interp
, 1);
220 _PyThreadState_Prealloc(PyInterpreterState
*interp
)
222 return new_threadstate(interp
, 0);
226 _PyThreadState_Init(PyThreadState
*tstate
)
229 _PyGILState_NoteThreadState(tstate
);
234 PyState_FindModule(struct PyModuleDef
* m
)
236 Py_ssize_t index
= m
->m_base
.m_index
;
237 PyInterpreterState
*state
= PyThreadState_GET()->interp
;
241 if (state
->modules_by_index
== NULL
)
243 if (index
> PyList_GET_SIZE(state
->modules_by_index
))
245 res
= PyList_GET_ITEM(state
->modules_by_index
, index
);
246 return res
==Py_None
? NULL
: res
;
250 _PyState_AddModule(PyObject
* module
, struct PyModuleDef
* def
)
252 PyInterpreterState
*state
= PyThreadState_GET()->interp
;
255 if (!state
->modules_by_index
) {
256 state
->modules_by_index
= PyList_New(0);
257 if (!state
->modules_by_index
)
260 while(PyList_GET_SIZE(state
->modules_by_index
) <= def
->m_base
.m_index
)
261 if (PyList_Append(state
->modules_by_index
, Py_None
) < 0)
264 return PyList_SetItem(state
->modules_by_index
,
265 def
->m_base
.m_index
, module
);
269 PyThreadState_Clear(PyThreadState
*tstate
)
271 if (Py_VerboseFlag
&& tstate
->frame
!= NULL
)
273 "PyThreadState_Clear: warning: thread still has a frame\n");
275 Py_CLEAR(tstate
->frame
);
277 Py_CLEAR(tstate
->dict
);
278 Py_CLEAR(tstate
->async_exc
);
280 Py_CLEAR(tstate
->curexc_type
);
281 Py_CLEAR(tstate
->curexc_value
);
282 Py_CLEAR(tstate
->curexc_traceback
);
284 Py_CLEAR(tstate
->exc_type
);
285 Py_CLEAR(tstate
->exc_value
);
286 Py_CLEAR(tstate
->exc_traceback
);
288 tstate
->c_profilefunc
= NULL
;
289 tstate
->c_tracefunc
= NULL
;
290 Py_CLEAR(tstate
->c_profileobj
);
291 Py_CLEAR(tstate
->c_traceobj
);
295 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
297 tstate_delete_common(PyThreadState
*tstate
)
299 PyInterpreterState
*interp
;
301 PyThreadState
*prev_p
= NULL
;
303 Py_FatalError("PyThreadState_Delete: NULL tstate");
304 interp
= tstate
->interp
;
306 Py_FatalError("PyThreadState_Delete: NULL interp");
308 for (p
= &interp
->tstate_head
; ; p
= &(*p
)->next
) {
311 "PyThreadState_Delete: invalid tstate");
314 /* Sanity check. These states should never happen but if
315 * they do we must abort. Otherwise we'll end up spinning in
316 * in a tight loop with the lock held. A similar check is done
317 * in thread.c find_key(). */
320 "PyThreadState_Delete: small circular list(!)"
321 " and tstate not found.");
323 if ((*p
)->next
== interp
->tstate_head
)
325 "PyThreadState_Delete: circular list(!) and"
326 " tstate not found.");
335 PyThreadState_Delete(PyThreadState
*tstate
)
337 if (tstate
== _PyThreadState_Current
)
338 Py_FatalError("PyThreadState_Delete: tstate is still current");
339 tstate_delete_common(tstate
);
341 if (autoTLSkey
&& PyThread_get_key_value(autoTLSkey
) == tstate
)
342 PyThread_delete_key_value(autoTLSkey
);
343 #endif /* WITH_THREAD */
349 PyThreadState_DeleteCurrent()
351 PyThreadState
*tstate
= _PyThreadState_Current
;
354 "PyThreadState_DeleteCurrent: no current tstate");
355 _PyThreadState_Current
= NULL
;
356 tstate_delete_common(tstate
);
357 if (autoTLSkey
&& PyThread_get_key_value(autoTLSkey
) == tstate
)
358 PyThread_delete_key_value(autoTLSkey
);
359 PyEval_ReleaseLock();
361 #endif /* WITH_THREAD */
365 PyThreadState_Get(void)
367 if (_PyThreadState_Current
== NULL
)
368 Py_FatalError("PyThreadState_Get: no current thread");
370 return _PyThreadState_Current
;
375 PyThreadState_Swap(PyThreadState
*newts
)
377 PyThreadState
*oldts
= _PyThreadState_Current
;
379 _PyThreadState_Current
= newts
;
380 /* It should not be possible for more than one thread state
381 to be used for a thread. Check this the best we can in debug
384 #if defined(Py_DEBUG) && defined(WITH_THREAD)
386 /* This can be called from PyEval_RestoreThread(). Similar
387 to it, we need to ensure errno doesn't change.
390 PyThreadState
*check
= PyGILState_GetThisThreadState();
391 if (check
&& check
->interp
== newts
->interp
&& check
!= newts
)
392 Py_FatalError("Invalid thread state for this thread");
399 /* An extension mechanism to store arbitrary additional per-thread state.
400 PyThreadState_GetDict() returns a dictionary that can be used to hold such
401 state; the caller should pick a unique key and store its state there. If
402 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
403 and the caller should assume no per-thread state is available. */
406 PyThreadState_GetDict(void)
408 if (_PyThreadState_Current
== NULL
)
411 if (_PyThreadState_Current
->dict
== NULL
) {
413 _PyThreadState_Current
->dict
= d
= PyDict_New();
417 return _PyThreadState_Current
->dict
;
421 /* Asynchronously raise an exception in a thread.
422 Requested by Just van Rossum and Alex Martelli.
423 To prevent naive misuse, you must write your own extension
424 to call this, or use ctypes. Must be called with the GIL held.
425 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
426 match any known thread id). Can be called with exc=NULL to clear an
427 existing async exception. This raises no exceptions. */
430 PyThreadState_SetAsyncExc(long id
, PyObject
*exc
) {
431 PyThreadState
*tstate
= PyThreadState_GET();
432 PyInterpreterState
*interp
= tstate
->interp
;
435 /* Although the GIL is held, a few C API functions can be called
436 * without the GIL held, and in particular some that create and
437 * destroy thread and interpreter states. Those can mutate the
438 * list of thread states we're traversing, so to prevent that we lock
439 * head_mutex for the duration.
442 for (p
= interp
->tstate_head
; p
!= NULL
; p
= p
->next
) {
443 if (p
->thread_id
== id
) {
444 /* Tricky: we need to decref the current value
445 * (if any) in p->async_exc, but that can in turn
446 * allow arbitrary Python code to run, including
447 * perhaps calls to this function. To prevent
448 * deadlock, we need to release head_mutex before
451 PyObject
*old_exc
= p
->async_exc
;
464 /* Routines for advanced debuggers, requested by David Beazley.
465 Don't use unless you know what you are doing! */
468 PyInterpreterState_Head(void)
474 PyInterpreterState_Next(PyInterpreterState
*interp
) {
479 PyInterpreterState_ThreadHead(PyInterpreterState
*interp
) {
480 return interp
->tstate_head
;
484 PyThreadState_Next(PyThreadState
*tstate
) {
488 /* The implementation of sys._current_frames(). This is intended to be
489 called with the GIL held, as it will be when called via
490 sys._current_frames(). It's possible it would work fine even without
491 the GIL held, but haven't thought enough about that.
494 _PyThread_CurrentFrames(void)
497 PyInterpreterState
*i
;
499 result
= PyDict_New();
503 /* for i in all interpreters:
504 * for t in all of i's thread states:
505 * if t's frame isn't NULL, map t's id to its frame
506 * Because these lists can mutute even when the GIL is held, we
507 * need to grab head_mutex for the duration.
510 for (i
= interp_head
; i
!= NULL
; i
= i
->next
) {
512 for (t
= i
->tstate_head
; t
!= NULL
; t
= t
->next
) {
515 struct _frame
*frame
= t
->frame
;
518 id
= PyLong_FromLong(t
->thread_id
);
521 stat
= PyDict_SetItem(result
, id
, (PyObject
*)frame
);
536 /* Python "auto thread state" API. */
539 /* Keep this as a static, as it is not reliable! It can only
540 ever be compared to the state for the *current* thread.
541 * If not equal, then it doesn't matter that the actual
542 value may change immediately after comparison, as it can't
543 possibly change to the current thread's state.
544 * If equal, then the current thread holds the lock, so the value can't
545 change until we yield the lock.
548 PyThreadState_IsCurrent(PyThreadState
*tstate
)
550 /* Must be the tstate for this thread */
551 assert(PyGILState_GetThisThreadState()==tstate
);
552 /* On Windows at least, simple reads and writes to 32 bit values
555 return tstate
== _PyThreadState_Current
;
558 /* Internal initialization/finalization functions called by
559 Py_Initialize/Py_Finalize
562 _PyGILState_Init(PyInterpreterState
*i
, PyThreadState
*t
)
564 assert(i
&& t
); /* must init with valid states */
565 autoTLSkey
= PyThread_create_key();
566 autoInterpreterState
= i
;
567 assert(PyThread_get_key_value(autoTLSkey
) == NULL
);
568 assert(t
->gilstate_counter
== 0);
570 _PyGILState_NoteThreadState(t
);
574 _PyGILState_Fini(void)
576 PyThread_delete_key(autoTLSkey
);
578 autoInterpreterState
= NULL
;
581 /* When a thread state is created for a thread by some mechanism other than
582 PyGILState_Ensure, it's important that the GILState machinery knows about
583 it so it doesn't try to create another thread state for the thread (this is
584 a better fix for SF bug #1010677 than the first one attempted).
587 _PyGILState_NoteThreadState(PyThreadState
* tstate
)
589 /* If autoTLSkey is 0, this must be the very first threadstate created
590 in Py_Initialize(). Don't do anything for now (we'll be back here
591 when _PyGILState_Init is called). */
595 /* Stick the thread state for this thread in thread local storage.
597 The only situation where you can legitimately have more than one
598 thread state for an OS level thread is when there are multiple
601 a) You shouldn't really be using the PyGILState_ APIs anyway,
604 b) The slightly odd way PyThread_set_key_value works (see
605 comments by its implementation) means that the first thread
606 state created for that given OS level thread will "win",
607 which seems reasonable behaviour.
609 if (PyThread_set_key_value(autoTLSkey
, (void *)tstate
) < 0)
610 Py_FatalError("Couldn't create autoTLSkey mapping");
612 /* PyGILState_Release must not try to delete this thread state. */
613 tstate
->gilstate_counter
= 1;
616 /* The public functions */
618 PyGILState_GetThisThreadState(void)
620 if (autoInterpreterState
== NULL
|| autoTLSkey
== 0)
622 return (PyThreadState
*)PyThread_get_key_value(autoTLSkey
);
626 PyGILState_Ensure(void)
630 /* Note that we do not auto-init Python here - apart from
631 potential races with 2 threads auto-initializing, pep-311
632 spells out other issues. Embedders are expected to have
633 called Py_Initialize() and usually PyEval_InitThreads().
635 assert(autoInterpreterState
); /* Py_Initialize() hasn't been called! */
636 tcur
= (PyThreadState
*)PyThread_get_key_value(autoTLSkey
);
638 /* Create a new thread state for this thread */
639 tcur
= PyThreadState_New(autoInterpreterState
);
641 Py_FatalError("Couldn't create thread-state for new thread");
642 /* This is our thread state! We'll need to delete it in the
643 matching call to PyGILState_Release(). */
644 tcur
->gilstate_counter
= 0;
645 current
= 0; /* new thread state is never current */
648 current
= PyThreadState_IsCurrent(tcur
);
650 PyEval_RestoreThread(tcur
);
651 /* Update our counter in the thread-state - no need for locks:
652 - tcur will remain valid as we hold the GIL.
653 - the counter is safe as we are the only thread "allowed"
656 ++tcur
->gilstate_counter
;
657 return current
? PyGILState_LOCKED
: PyGILState_UNLOCKED
;
661 PyGILState_Release(PyGILState_STATE oldstate
)
663 PyThreadState
*tcur
= (PyThreadState
*)PyThread_get_key_value(
666 Py_FatalError("auto-releasing thread-state, "
667 "but no thread-state for this thread");
668 /* We must hold the GIL and have our thread state current */
669 /* XXX - remove the check - the assert should be fine,
670 but while this is very new (April 2003), the extra check
671 by release-only users can't hurt.
673 if (! PyThreadState_IsCurrent(tcur
))
674 Py_FatalError("This thread state must be current when releasing");
675 assert(PyThreadState_IsCurrent(tcur
));
676 --tcur
->gilstate_counter
;
677 assert(tcur
->gilstate_counter
>= 0); /* illegal counter value */
679 /* If we're going to destroy this thread-state, we must
680 * clear it while the GIL is held, as destructors may run.
682 if (tcur
->gilstate_counter
== 0) {
683 /* can't have been locked when we created it */
684 assert(oldstate
== PyGILState_UNLOCKED
);
685 PyThreadState_Clear(tcur
);
686 /* Delete the thread-state. Note this releases the GIL too!
687 * It's vital that the GIL be held here, to avoid shutdown
688 * races; see bugs 225673 and 1061968 (that nasty bug has a
689 * habit of coming back).
691 PyThreadState_DeleteCurrent();
693 /* Release the lock if necessary */
694 else if (oldstate
== PyGILState_UNLOCKED
)
702 #endif /* WITH_THREAD */