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
->sysdict
= NULL
;
72 interp
->builtins
= NULL
;
73 interp
->tstate_head
= NULL
;
74 interp
->codec_search_path
= NULL
;
75 interp
->codec_search_cache
= NULL
;
76 interp
->codec_error_registry
= NULL
;
79 interp
->dlopenflags
= RTLD_NOW
;
81 interp
->dlopenflags
= RTLD_LAZY
;
89 interp
->next
= interp_head
;
99 PyInterpreterState_Clear(PyInterpreterState
*interp
)
103 for (p
= interp
->tstate_head
; p
!= NULL
; p
= p
->next
)
104 PyThreadState_Clear(p
);
106 Py_CLEAR(interp
->codec_search_path
);
107 Py_CLEAR(interp
->codec_search_cache
);
108 Py_CLEAR(interp
->codec_error_registry
);
109 Py_CLEAR(interp
->modules
);
110 Py_CLEAR(interp
->sysdict
);
111 Py_CLEAR(interp
->builtins
);
116 zapthreads(PyInterpreterState
*interp
)
119 /* No need to lock the mutex here because this should only happen
120 when the threads are all really dead (XXX famous last words). */
121 while ((p
= interp
->tstate_head
) != NULL
) {
122 PyThreadState_Delete(p
);
128 PyInterpreterState_Delete(PyInterpreterState
*interp
)
130 PyInterpreterState
**p
;
133 for (p
= &interp_head
; ; p
= &(*p
)->next
) {
136 "PyInterpreterState_Delete: invalid interp");
140 if (interp
->tstate_head
!= NULL
)
141 Py_FatalError("PyInterpreterState_Delete: remaining threads");
148 /* Default implementation for _PyThreadState_GetFrame */
149 static struct _frame
*
150 threadstate_getframe(PyThreadState
*self
)
156 PyThreadState_New(PyInterpreterState
*interp
)
158 PyThreadState
*tstate
= (PyThreadState
*)malloc(sizeof(PyThreadState
));
160 if (_PyThreadState_GetFrame
== NULL
)
161 _PyThreadState_GetFrame
= threadstate_getframe
;
163 if (tstate
!= NULL
) {
164 tstate
->interp
= interp
;
166 tstate
->frame
= NULL
;
167 tstate
->recursion_depth
= 0;
169 tstate
->use_tracing
= 0;
170 tstate
->tick_counter
= 0;
171 tstate
->gilstate_counter
= 0;
172 tstate
->async_exc
= NULL
;
174 tstate
->thread_id
= PyThread_get_thread_ident();
176 tstate
->thread_id
= 0;
181 tstate
->curexc_type
= NULL
;
182 tstate
->curexc_value
= NULL
;
183 tstate
->curexc_traceback
= NULL
;
185 tstate
->exc_type
= NULL
;
186 tstate
->exc_value
= NULL
;
187 tstate
->exc_traceback
= NULL
;
189 tstate
->c_profilefunc
= NULL
;
190 tstate
->c_tracefunc
= NULL
;
191 tstate
->c_profileobj
= NULL
;
192 tstate
->c_traceobj
= NULL
;
195 _PyGILState_NoteThreadState(tstate
);
199 tstate
->next
= interp
->tstate_head
;
200 interp
->tstate_head
= tstate
;
209 PyThreadState_Clear(PyThreadState
*tstate
)
211 if (Py_VerboseFlag
&& tstate
->frame
!= NULL
)
213 "PyThreadState_Clear: warning: thread still has a frame\n");
215 Py_CLEAR(tstate
->frame
);
217 Py_CLEAR(tstate
->dict
);
218 Py_CLEAR(tstate
->async_exc
);
220 Py_CLEAR(tstate
->curexc_type
);
221 Py_CLEAR(tstate
->curexc_value
);
222 Py_CLEAR(tstate
->curexc_traceback
);
224 Py_CLEAR(tstate
->exc_type
);
225 Py_CLEAR(tstate
->exc_value
);
226 Py_CLEAR(tstate
->exc_traceback
);
228 tstate
->c_profilefunc
= NULL
;
229 tstate
->c_tracefunc
= NULL
;
230 Py_CLEAR(tstate
->c_profileobj
);
231 Py_CLEAR(tstate
->c_traceobj
);
235 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
237 tstate_delete_common(PyThreadState
*tstate
)
239 PyInterpreterState
*interp
;
242 Py_FatalError("PyThreadState_Delete: NULL tstate");
243 interp
= tstate
->interp
;
245 Py_FatalError("PyThreadState_Delete: NULL interp");
247 for (p
= &interp
->tstate_head
; ; p
= &(*p
)->next
) {
250 "PyThreadState_Delete: invalid tstate");
261 PyThreadState_Delete(PyThreadState
*tstate
)
263 if (tstate
== _PyThreadState_Current
)
264 Py_FatalError("PyThreadState_Delete: tstate is still current");
265 tstate_delete_common(tstate
);
267 if (autoTLSkey
&& PyThread_get_key_value(autoTLSkey
) == tstate
)
268 PyThread_delete_key_value(autoTLSkey
);
269 #endif /* WITH_THREAD */
275 PyThreadState_DeleteCurrent()
277 PyThreadState
*tstate
= _PyThreadState_Current
;
280 "PyThreadState_DeleteCurrent: no current tstate");
281 _PyThreadState_Current
= NULL
;
282 tstate_delete_common(tstate
);
283 if (autoTLSkey
&& PyThread_get_key_value(autoTLSkey
) == tstate
)
284 PyThread_delete_key_value(autoTLSkey
);
285 PyEval_ReleaseLock();
287 #endif /* WITH_THREAD */
291 PyThreadState_Get(void)
293 if (_PyThreadState_Current
== NULL
)
294 Py_FatalError("PyThreadState_Get: no current thread");
296 return _PyThreadState_Current
;
301 PyThreadState_Swap(PyThreadState
*newts
)
303 PyThreadState
*oldts
= _PyThreadState_Current
;
305 _PyThreadState_Current
= newts
;
306 /* It should not be possible for more than one thread state
307 to be used for a thread. Check this the best we can in debug
310 #if defined(Py_DEBUG) && defined(WITH_THREAD)
312 PyThreadState
*check
= PyGILState_GetThisThreadState();
313 if (check
&& check
->interp
== newts
->interp
&& check
!= newts
)
314 Py_FatalError("Invalid thread state for this thread");
320 /* An extension mechanism to store arbitrary additional per-thread state.
321 PyThreadState_GetDict() returns a dictionary that can be used to hold such
322 state; the caller should pick a unique key and store its state there. If
323 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
324 and the caller should assume no per-thread state is available. */
327 PyThreadState_GetDict(void)
329 if (_PyThreadState_Current
== NULL
)
332 if (_PyThreadState_Current
->dict
== NULL
) {
334 _PyThreadState_Current
->dict
= d
= PyDict_New();
338 return _PyThreadState_Current
->dict
;
342 /* Asynchronously raise an exception in a thread.
343 Requested by Just van Rossum and Alex Martelli.
344 To prevent naive misuse, you must write your own extension
345 to call this. Must be called with the GIL held.
346 Returns the number of tstates modified; if it returns a number
347 greater than one, you're in trouble, and you should call it again
348 with exc=NULL to revert the effect. This raises no exceptions. */
351 PyThreadState_SetAsyncExc(long id
, PyObject
*exc
) {
352 PyThreadState
*tstate
= PyThreadState_GET();
353 PyInterpreterState
*interp
= tstate
->interp
;
357 for (p
= interp
->tstate_head
; p
!= NULL
; p
= p
->next
) {
358 if (p
->thread_id
!= id
)
360 Py_CLEAR(p
->async_exc
);
370 /* Routines for advanced debuggers, requested by David Beazley.
371 Don't use unless you know what you are doing! */
374 PyInterpreterState_Head(void)
380 PyInterpreterState_Next(PyInterpreterState
*interp
) {
385 PyInterpreterState_ThreadHead(PyInterpreterState
*interp
) {
386 return interp
->tstate_head
;
390 PyThreadState_Next(PyThreadState
*tstate
) {
394 /* The implementation of sys._current_frames(). This is intended to be
395 called with the GIL held, as it will be when called via
396 sys._current_frames(). It's possible it would work fine even without
397 the GIL held, but haven't thought enough about that.
400 _PyThread_CurrentFrames(void)
403 PyInterpreterState
*i
;
405 result
= PyDict_New();
409 /* for i in all interpreters:
410 * for t in all of i's thread states:
411 * if t's frame isn't NULL, map t's id to its frame
412 * Because these lists can mutute even when the GIL is held, we
413 * need to grab head_mutex for the duration.
416 for (i
= interp_head
; i
!= NULL
; i
= i
->next
) {
418 for (t
= i
->tstate_head
; t
!= NULL
; t
= t
->next
) {
421 struct _frame
*frame
= t
->frame
;
424 id
= PyInt_FromLong(t
->thread_id
);
427 stat
= PyDict_SetItem(result
, id
, (PyObject
*)frame
);
442 /* Python "auto thread state" API. */
445 /* Keep this as a static, as it is not reliable! It can only
446 ever be compared to the state for the *current* thread.
447 * If not equal, then it doesn't matter that the actual
448 value may change immediately after comparison, as it can't
449 possibly change to the current thread's state.
450 * If equal, then the current thread holds the lock, so the value can't
451 change until we yield the lock.
454 PyThreadState_IsCurrent(PyThreadState
*tstate
)
456 /* Must be the tstate for this thread */
457 assert(PyGILState_GetThisThreadState()==tstate
);
458 /* On Windows at least, simple reads and writes to 32 bit values
461 return tstate
== _PyThreadState_Current
;
464 /* Internal initialization/finalization functions called by
465 Py_Initialize/Py_Finalize
468 _PyGILState_Init(PyInterpreterState
*i
, PyThreadState
*t
)
470 assert(i
&& t
); /* must init with valid states */
471 autoTLSkey
= PyThread_create_key();
472 autoInterpreterState
= i
;
473 assert(PyThread_get_key_value(autoTLSkey
) == NULL
);
474 assert(t
->gilstate_counter
== 0);
476 _PyGILState_NoteThreadState(t
);
480 _PyGILState_Fini(void)
482 PyThread_delete_key(autoTLSkey
);
484 autoInterpreterState
= NULL
;;
487 /* When a thread state is created for a thread by some mechanism other than
488 PyGILState_Ensure, it's important that the GILState machinery knows about
489 it so it doesn't try to create another thread state for the thread (this is
490 a better fix for SF bug #1010677 than the first one attempted).
493 _PyGILState_NoteThreadState(PyThreadState
* tstate
)
495 /* If autoTLSkey is 0, this must be the very first threadstate created
496 in Py_Initialize(). Don't do anything for now (we'll be back here
497 when _PyGILState_Init is called). */
501 /* Stick the thread state for this thread in thread local storage.
503 The only situation where you can legitimately have more than one
504 thread state for an OS level thread is when there are multiple
507 a) You shouldn't really be using the PyGILState_ APIs anyway,
510 b) The slightly odd way PyThread_set_key_value works (see
511 comments by its implementation) means that the first thread
512 state created for that given OS level thread will "win",
513 which seems reasonable behaviour.
515 if (PyThread_set_key_value(autoTLSkey
, (void *)tstate
) < 0)
516 Py_FatalError("Couldn't create autoTLSkey mapping");
518 /* PyGILState_Release must not try to delete this thread state. */
519 tstate
->gilstate_counter
= 1;
522 /* The public functions */
524 PyGILState_GetThisThreadState(void)
526 if (autoInterpreterState
== NULL
|| autoTLSkey
== 0)
528 return (PyThreadState
*)PyThread_get_key_value(autoTLSkey
);
532 PyGILState_Ensure(void)
536 /* Note that we do not auto-init Python here - apart from
537 potential races with 2 threads auto-initializing, pep-311
538 spells out other issues. Embedders are expected to have
539 called Py_Initialize() and usually PyEval_InitThreads().
541 assert(autoInterpreterState
); /* Py_Initialize() hasn't been called! */
542 tcur
= (PyThreadState
*)PyThread_get_key_value(autoTLSkey
);
544 /* Create a new thread state for this thread */
545 tcur
= PyThreadState_New(autoInterpreterState
);
547 Py_FatalError("Couldn't create thread-state for new thread");
548 /* This is our thread state! We'll need to delete it in the
549 matching call to PyGILState_Release(). */
550 tcur
->gilstate_counter
= 0;
551 current
= 0; /* new thread state is never current */
554 current
= PyThreadState_IsCurrent(tcur
);
556 PyEval_RestoreThread(tcur
);
557 /* Update our counter in the thread-state - no need for locks:
558 - tcur will remain valid as we hold the GIL.
559 - the counter is safe as we are the only thread "allowed"
562 ++tcur
->gilstate_counter
;
563 return current
? PyGILState_LOCKED
: PyGILState_UNLOCKED
;
567 PyGILState_Release(PyGILState_STATE oldstate
)
569 PyThreadState
*tcur
= (PyThreadState
*)PyThread_get_key_value(
572 Py_FatalError("auto-releasing thread-state, "
573 "but no thread-state for this thread");
574 /* We must hold the GIL and have our thread state current */
575 /* XXX - remove the check - the assert should be fine,
576 but while this is very new (April 2003), the extra check
577 by release-only users can't hurt.
579 if (! PyThreadState_IsCurrent(tcur
))
580 Py_FatalError("This thread state must be current when releasing");
581 assert(PyThreadState_IsCurrent(tcur
));
582 --tcur
->gilstate_counter
;
583 assert(tcur
->gilstate_counter
>= 0); /* illegal counter value */
585 /* If we're going to destroy this thread-state, we must
586 * clear it while the GIL is held, as destructors may run.
588 if (tcur
->gilstate_counter
== 0) {
589 /* can't have been locked when we created it */
590 assert(oldstate
== PyGILState_UNLOCKED
);
591 PyThreadState_Clear(tcur
);
592 /* Delete the thread-state. Note this releases the GIL too!
593 * It's vital that the GIL be held here, to avoid shutdown
594 * races; see bugs 225673 and 1061968 (that nasty bug has a
595 * habit of coming back).
597 PyThreadState_DeleteCurrent();
599 /* Release the lock if necessary */
600 else if (oldstate
== PyGILState_UNLOCKED
)
608 #endif /* WITH_THREAD */