2 /* Thread and interpreter state structures and their interfaces */
17 PyObject *tmp = (PyObject *)(x); \
25 static PyThread_type_lock head_mutex
= NULL
; /* Protects interp->tstate_head */
26 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
27 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
28 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
30 #define HEAD_INIT() /* Nothing */
31 #define HEAD_LOCK() /* Nothing */
32 #define HEAD_UNLOCK() /* Nothing */
35 static PyInterpreterState
*interp_head
= NULL
;
37 PyThreadState
*_PyThreadState_Current
= NULL
;
38 PyThreadFrameGetter _PyThreadState_GetFrame
= NULL
;
42 PyInterpreterState_New(void)
44 PyInterpreterState
*interp
= PyMem_NEW(PyInterpreterState
, 1);
48 interp
->modules
= NULL
;
49 interp
->sysdict
= NULL
;
50 interp
->builtins
= NULL
;
51 interp
->tstate_head
= NULL
;
52 interp
->codec_search_path
= NULL
;
53 interp
->codec_search_cache
= NULL
;
54 interp
->codec_error_registry
= NULL
;
57 interp
->dlopenflags
= RTLD_NOW
;
59 interp
->dlopenflags
= RTLD_LAZY
;
64 interp
->next
= interp_head
;
74 PyInterpreterState_Clear(PyInterpreterState
*interp
)
78 for (p
= interp
->tstate_head
; p
!= NULL
; p
= p
->next
)
79 PyThreadState_Clear(p
);
81 ZAP(interp
->codec_search_path
);
82 ZAP(interp
->codec_search_cache
);
83 ZAP(interp
->codec_error_registry
);
86 ZAP(interp
->builtins
);
91 zapthreads(PyInterpreterState
*interp
)
94 /* No need to lock the mutex here because this should only happen
95 when the threads are all really dead (XXX famous last words). */
96 while ((p
= interp
->tstate_head
) != NULL
) {
97 PyThreadState_Delete(p
);
103 PyInterpreterState_Delete(PyInterpreterState
*interp
)
105 PyInterpreterState
**p
;
108 for (p
= &interp_head
; ; p
= &(*p
)->next
) {
111 "PyInterpreterState_Delete: invalid interp");
115 if (interp
->tstate_head
!= NULL
)
116 Py_FatalError("PyInterpreterState_Delete: remaining threads");
123 /* Default implementation for _PyThreadState_GetFrame */
124 static struct _frame
*
125 threadstate_getframe(PyThreadState
*self
)
131 PyThreadState_New(PyInterpreterState
*interp
)
133 PyThreadState
*tstate
= PyMem_NEW(PyThreadState
, 1);
134 if (_PyThreadState_GetFrame
== NULL
)
135 _PyThreadState_GetFrame
= threadstate_getframe
;
137 if (tstate
!= NULL
) {
138 tstate
->interp
= interp
;
140 tstate
->frame
= NULL
;
141 tstate
->recursion_depth
= 0;
143 tstate
->use_tracing
= 0;
144 tstate
->tick_counter
= 0;
145 tstate
->gilstate_counter
= 0;
149 tstate
->curexc_type
= NULL
;
150 tstate
->curexc_value
= NULL
;
151 tstate
->curexc_traceback
= NULL
;
153 tstate
->exc_type
= NULL
;
154 tstate
->exc_value
= NULL
;
155 tstate
->exc_traceback
= NULL
;
157 tstate
->c_profilefunc
= NULL
;
158 tstate
->c_tracefunc
= NULL
;
159 tstate
->c_profileobj
= NULL
;
160 tstate
->c_traceobj
= NULL
;
163 tstate
->next
= interp
->tstate_head
;
164 interp
->tstate_head
= tstate
;
173 PyThreadState_Clear(PyThreadState
*tstate
)
175 if (Py_VerboseFlag
&& tstate
->frame
!= NULL
)
177 "PyThreadState_Clear: warning: thread still has a frame\n");
183 ZAP(tstate
->curexc_type
);
184 ZAP(tstate
->curexc_value
);
185 ZAP(tstate
->curexc_traceback
);
187 ZAP(tstate
->exc_type
);
188 ZAP(tstate
->exc_value
);
189 ZAP(tstate
->exc_traceback
);
191 tstate
->c_profilefunc
= NULL
;
192 tstate
->c_tracefunc
= NULL
;
193 ZAP(tstate
->c_profileobj
);
194 ZAP(tstate
->c_traceobj
);
198 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
200 tstate_delete_common(PyThreadState
*tstate
)
202 PyInterpreterState
*interp
;
205 Py_FatalError("PyThreadState_Delete: NULL tstate");
206 interp
= tstate
->interp
;
208 Py_FatalError("PyThreadState_Delete: NULL interp");
210 for (p
= &interp
->tstate_head
; ; p
= &(*p
)->next
) {
213 "PyThreadState_Delete: invalid tstate");
224 PyThreadState_Delete(PyThreadState
*tstate
)
226 if (tstate
== _PyThreadState_Current
)
227 Py_FatalError("PyThreadState_Delete: tstate is still current");
228 tstate_delete_common(tstate
);
234 PyThreadState_DeleteCurrent()
236 PyThreadState
*tstate
= _PyThreadState_Current
;
239 "PyThreadState_DeleteCurrent: no current tstate");
240 _PyThreadState_Current
= NULL
;
241 tstate_delete_common(tstate
);
242 PyEval_ReleaseLock();
244 #endif /* WITH_THREAD */
248 PyThreadState_Get(void)
250 if (_PyThreadState_Current
== NULL
)
251 Py_FatalError("PyThreadState_Get: no current thread");
253 return _PyThreadState_Current
;
258 PyThreadState_Swap(PyThreadState
*new)
260 PyThreadState
*old
= _PyThreadState_Current
;
262 _PyThreadState_Current
= new;
263 /* It should not be possible for more than one thread state
264 to be used for a thread. Check this the best we can in debug
267 #if defined(Py_DEBUG) && defined(WITH_THREAD)
269 PyThreadState
*check
= PyGILState_GetThisThreadState();
270 if (check
&& check
!= new)
271 Py_FatalError("Invalid thread state for this thread");
277 /* An extension mechanism to store arbitrary additional per-thread state.
278 PyThreadState_GetDict() returns a dictionary that can be used to hold such
279 state; the caller should pick a unique key and store its state there. If
280 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
281 and the caller should assume no per-thread state is available. */
284 PyThreadState_GetDict(void)
286 if (_PyThreadState_Current
== NULL
)
289 if (_PyThreadState_Current
->dict
== NULL
) {
291 _PyThreadState_Current
->dict
= d
= PyDict_New();
295 return _PyThreadState_Current
->dict
;
299 /* Routines for advanced debuggers, requested by David Beazley.
300 Don't use unless you know what you are doing! */
303 PyInterpreterState_Head(void)
309 PyInterpreterState_Next(PyInterpreterState
*interp
) {
314 PyInterpreterState_ThreadHead(PyInterpreterState
*interp
) {
315 return interp
->tstate_head
;
319 PyThreadState_Next(PyThreadState
*tstate
) {
323 /* Python "auto thread state" API. */
326 /* Keep this as a static, as it is not reliable! It can only
327 ever be compared to the state for the *current* thread.
328 * If not equal, then it doesn't matter that the actual
329 value may change immediately after comparison, as it can't
330 possibly change to the current thread's state.
331 * If equal, then the current thread holds the lock, so the value can't
332 change until we yield the lock.
335 PyThreadState_IsCurrent(PyThreadState
*tstate
)
337 /* Must be the tstate for this thread */
338 assert(PyGILState_GetThisThreadState()==tstate
);
339 /* On Windows at least, simple reads and writes to 32 bit values
342 return tstate
== _PyThreadState_Current
;
345 /* The single PyInterpreterState used by this process'
346 GILState implementation
348 static PyInterpreterState
*autoInterpreterState
= NULL
;
349 static int autoTLSkey
= 0;
351 /* Internal initialization/finalization functions called by
352 Py_Initialize/Py_Finalize
354 void _PyGILState_Init(PyInterpreterState
*i
, PyThreadState
*t
)
356 assert(i
&& t
); /* must init with a valid states */
357 autoTLSkey
= PyThread_create_key();
358 autoInterpreterState
= i
;
359 /* Now stash the thread state for this thread in TLS */
360 PyThread_set_key_value(autoTLSkey
, (void *)t
);
361 assert(t
->gilstate_counter
==0); /* must be a new thread state */
362 t
->gilstate_counter
= 1;
365 void _PyGILState_Fini(void)
367 PyThread_delete_key(autoTLSkey
);
369 autoInterpreterState
= NULL
;;
372 /* The public functions */
373 PyThreadState
*PyGILState_GetThisThreadState(void)
375 if (autoInterpreterState
==NULL
|| autoTLSkey
==0)
377 return (PyThreadState
*) PyThread_get_key_value(autoTLSkey
);
380 PyGILState_STATE
PyGILState_Ensure(void)
384 /* Note that we do not auto-init Python here - apart from
385 potential races with 2 threads auto-initializing, pep-311
386 spells out other issues. Embedders are expected to have
387 called Py_Initialize() and usually PyEval_InitThreads().
389 assert(autoInterpreterState
); /* Py_Initialize() hasn't been called! */
390 tcur
= PyThread_get_key_value(autoTLSkey
);
392 /* Create a new thread state for this thread */
393 tcur
= PyThreadState_New(autoInterpreterState
);
395 Py_FatalError("Couldn't create thread-state for new thread");
396 PyThread_set_key_value(autoTLSkey
, (void *)tcur
);
397 current
= 0; /* new thread state is never current */
399 current
= PyThreadState_IsCurrent(tcur
);
401 PyEval_RestoreThread(tcur
);
402 /* Update our counter in the thread-state - no need for locks:
403 - tcur will remain valid as we hold the GIL.
404 - the counter is safe as we are the only thread "allowed"
407 tcur
->gilstate_counter
++;
408 return current
? PyGILState_LOCKED
: PyGILState_UNLOCKED
;
411 void PyGILState_Release(PyGILState_STATE oldstate
)
413 PyThreadState
*tcur
= PyThread_get_key_value(autoTLSkey
);
415 Py_FatalError("auto-releasing thread-state, "
416 "but no thread-state for this thread");
417 /* We must hold the GIL and have our thread state current */
418 /* XXX - remove the check - the assert should be fine,
419 but while this is very new (April 2003), the extra check
420 by release-only users can't hurt.
422 if (!PyThreadState_IsCurrent(tcur
))
423 Py_FatalError("This thread state must be current when releasing");
424 assert (PyThreadState_IsCurrent(tcur
));
425 tcur
->gilstate_counter
-= 1;
426 assert (tcur
->gilstate_counter
>= 0); /* illegal counter value */
428 /* If we are about to destroy this thread-state, we must
429 clear it while the lock is held, as destructors may run
431 if (tcur
->gilstate_counter
==0) {
432 /* can't have been locked when we created it */
433 assert(oldstate
==PyGILState_UNLOCKED
);
434 PyThreadState_Clear(tcur
);
437 /* Release the lock if necessary */
438 if (oldstate
==PyGILState_UNLOCKED
)
439 PyEval_ReleaseThread(tcur
);
441 /* Now complete destruction of the thread if necessary */
442 if (tcur
->gilstate_counter
==0) {
443 /* Delete this thread from our TLS */
444 PyThread_delete_key_value(autoTLSkey
);
445 /* Delete the thread-state */
446 PyThreadState_Delete(tcur
);
449 #endif /* WITH_THREAD */