1 | n/a | |
---|
2 | n/a | /* Thread and interpreter state structures and their interfaces */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | |
---|
6 | n/a | #define GET_TSTATE() \ |
---|
7 | n/a | ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current)) |
---|
8 | n/a | #define SET_TSTATE(value) \ |
---|
9 | n/a | _Py_atomic_store_relaxed(&_PyThreadState_Current, (uintptr_t)(value)) |
---|
10 | n/a | #define GET_INTERP_STATE() \ |
---|
11 | n/a | (GET_TSTATE()->interp) |
---|
12 | n/a | |
---|
13 | n/a | |
---|
14 | n/a | /* -------------------------------------------------------------------------- |
---|
15 | n/a | CAUTION |
---|
16 | n/a | |
---|
17 | n/a | Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A |
---|
18 | n/a | number of these functions are advertised as safe to call when the GIL isn't |
---|
19 | n/a | held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's |
---|
20 | n/a | debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL |
---|
21 | n/a | to avoid the expense of doing their own locking). |
---|
22 | n/a | -------------------------------------------------------------------------- */ |
---|
23 | n/a | |
---|
24 | n/a | #ifdef HAVE_DLOPEN |
---|
25 | n/a | #ifdef HAVE_DLFCN_H |
---|
26 | n/a | #include <dlfcn.h> |
---|
27 | n/a | #endif |
---|
28 | n/a | #if !HAVE_DECL_RTLD_LAZY |
---|
29 | n/a | #define RTLD_LAZY 1 |
---|
30 | n/a | #endif |
---|
31 | n/a | #endif |
---|
32 | n/a | |
---|
33 | n/a | #ifdef __cplusplus |
---|
34 | n/a | extern "C" { |
---|
35 | n/a | #endif |
---|
36 | n/a | |
---|
37 | n/a | int _PyGILState_check_enabled = 1; |
---|
38 | n/a | |
---|
39 | n/a | #ifdef WITH_THREAD |
---|
40 | n/a | #include "pythread.h" |
---|
41 | n/a | static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ |
---|
42 | n/a | #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock())) |
---|
43 | n/a | #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) |
---|
44 | n/a | #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) |
---|
45 | n/a | |
---|
46 | n/a | /* The single PyInterpreterState used by this process' |
---|
47 | n/a | GILState implementation |
---|
48 | n/a | */ |
---|
49 | n/a | static PyInterpreterState *autoInterpreterState = NULL; |
---|
50 | n/a | static int autoTLSkey = -1; |
---|
51 | n/a | #else |
---|
52 | n/a | #define HEAD_INIT() /* Nothing */ |
---|
53 | n/a | #define HEAD_LOCK() /* Nothing */ |
---|
54 | n/a | #define HEAD_UNLOCK() /* Nothing */ |
---|
55 | n/a | #endif |
---|
56 | n/a | |
---|
57 | n/a | static PyInterpreterState *interp_head = NULL; |
---|
58 | n/a | |
---|
59 | n/a | /* Assuming the current thread holds the GIL, this is the |
---|
60 | n/a | PyThreadState for the current thread. */ |
---|
61 | n/a | _Py_atomic_address _PyThreadState_Current = {0}; |
---|
62 | n/a | PyThreadFrameGetter _PyThreadState_GetFrame = NULL; |
---|
63 | n/a | |
---|
64 | n/a | #ifdef WITH_THREAD |
---|
65 | n/a | static void _PyGILState_NoteThreadState(PyThreadState* tstate); |
---|
66 | n/a | #endif |
---|
67 | n/a | |
---|
68 | n/a | |
---|
69 | n/a | PyInterpreterState * |
---|
70 | n/a | PyInterpreterState_New(void) |
---|
71 | n/a | { |
---|
72 | n/a | PyInterpreterState *interp = (PyInterpreterState *) |
---|
73 | n/a | PyMem_RawMalloc(sizeof(PyInterpreterState)); |
---|
74 | n/a | |
---|
75 | n/a | if (interp != NULL) { |
---|
76 | n/a | HEAD_INIT(); |
---|
77 | n/a | #ifdef WITH_THREAD |
---|
78 | n/a | if (head_mutex == NULL) |
---|
79 | n/a | Py_FatalError("Can't initialize threads for interpreter"); |
---|
80 | n/a | #endif |
---|
81 | n/a | interp->modules = NULL; |
---|
82 | n/a | interp->modules_by_index = NULL; |
---|
83 | n/a | interp->sysdict = NULL; |
---|
84 | n/a | interp->builtins = NULL; |
---|
85 | n/a | interp->builtins_copy = NULL; |
---|
86 | n/a | interp->tstate_head = NULL; |
---|
87 | n/a | interp->codec_search_path = NULL; |
---|
88 | n/a | interp->codec_search_cache = NULL; |
---|
89 | n/a | interp->codec_error_registry = NULL; |
---|
90 | n/a | interp->codecs_initialized = 0; |
---|
91 | n/a | interp->fscodec_initialized = 0; |
---|
92 | n/a | interp->importlib = NULL; |
---|
93 | n/a | interp->import_func = NULL; |
---|
94 | n/a | interp->eval_frame = _PyEval_EvalFrameDefault; |
---|
95 | n/a | #ifdef HAVE_DLOPEN |
---|
96 | n/a | #if HAVE_DECL_RTLD_NOW |
---|
97 | n/a | interp->dlopenflags = RTLD_NOW; |
---|
98 | n/a | #else |
---|
99 | n/a | interp->dlopenflags = RTLD_LAZY; |
---|
100 | n/a | #endif |
---|
101 | n/a | #endif |
---|
102 | n/a | |
---|
103 | n/a | HEAD_LOCK(); |
---|
104 | n/a | interp->next = interp_head; |
---|
105 | n/a | interp_head = interp; |
---|
106 | n/a | HEAD_UNLOCK(); |
---|
107 | n/a | } |
---|
108 | n/a | |
---|
109 | n/a | return interp; |
---|
110 | n/a | } |
---|
111 | n/a | |
---|
112 | n/a | |
---|
113 | n/a | void |
---|
114 | n/a | PyInterpreterState_Clear(PyInterpreterState *interp) |
---|
115 | n/a | { |
---|
116 | n/a | PyThreadState *p; |
---|
117 | n/a | HEAD_LOCK(); |
---|
118 | n/a | for (p = interp->tstate_head; p != NULL; p = p->next) |
---|
119 | n/a | PyThreadState_Clear(p); |
---|
120 | n/a | HEAD_UNLOCK(); |
---|
121 | n/a | Py_CLEAR(interp->codec_search_path); |
---|
122 | n/a | Py_CLEAR(interp->codec_search_cache); |
---|
123 | n/a | Py_CLEAR(interp->codec_error_registry); |
---|
124 | n/a | Py_CLEAR(interp->modules); |
---|
125 | n/a | Py_CLEAR(interp->modules_by_index); |
---|
126 | n/a | Py_CLEAR(interp->sysdict); |
---|
127 | n/a | Py_CLEAR(interp->builtins); |
---|
128 | n/a | Py_CLEAR(interp->builtins_copy); |
---|
129 | n/a | Py_CLEAR(interp->importlib); |
---|
130 | n/a | Py_CLEAR(interp->import_func); |
---|
131 | n/a | } |
---|
132 | n/a | |
---|
133 | n/a | |
---|
134 | n/a | static void |
---|
135 | n/a | zapthreads(PyInterpreterState *interp) |
---|
136 | n/a | { |
---|
137 | n/a | PyThreadState *p; |
---|
138 | n/a | /* No need to lock the mutex here because this should only happen |
---|
139 | n/a | when the threads are all really dead (XXX famous last words). */ |
---|
140 | n/a | while ((p = interp->tstate_head) != NULL) { |
---|
141 | n/a | PyThreadState_Delete(p); |
---|
142 | n/a | } |
---|
143 | n/a | } |
---|
144 | n/a | |
---|
145 | n/a | |
---|
146 | n/a | void |
---|
147 | n/a | PyInterpreterState_Delete(PyInterpreterState *interp) |
---|
148 | n/a | { |
---|
149 | n/a | PyInterpreterState **p; |
---|
150 | n/a | zapthreads(interp); |
---|
151 | n/a | HEAD_LOCK(); |
---|
152 | n/a | for (p = &interp_head; ; p = &(*p)->next) { |
---|
153 | n/a | if (*p == NULL) |
---|
154 | n/a | Py_FatalError( |
---|
155 | n/a | "PyInterpreterState_Delete: invalid interp"); |
---|
156 | n/a | if (*p == interp) |
---|
157 | n/a | break; |
---|
158 | n/a | } |
---|
159 | n/a | if (interp->tstate_head != NULL) |
---|
160 | n/a | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
---|
161 | n/a | *p = interp->next; |
---|
162 | n/a | HEAD_UNLOCK(); |
---|
163 | n/a | PyMem_RawFree(interp); |
---|
164 | n/a | #ifdef WITH_THREAD |
---|
165 | n/a | if (interp_head == NULL && head_mutex != NULL) { |
---|
166 | n/a | PyThread_free_lock(head_mutex); |
---|
167 | n/a | head_mutex = NULL; |
---|
168 | n/a | } |
---|
169 | n/a | #endif |
---|
170 | n/a | } |
---|
171 | n/a | |
---|
172 | n/a | |
---|
173 | n/a | /* Default implementation for _PyThreadState_GetFrame */ |
---|
174 | n/a | static struct _frame * |
---|
175 | n/a | threadstate_getframe(PyThreadState *self) |
---|
176 | n/a | { |
---|
177 | n/a | return self->frame; |
---|
178 | n/a | } |
---|
179 | n/a | |
---|
180 | n/a | static PyThreadState * |
---|
181 | n/a | new_threadstate(PyInterpreterState *interp, int init) |
---|
182 | n/a | { |
---|
183 | n/a | PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState)); |
---|
184 | n/a | |
---|
185 | n/a | if (_PyThreadState_GetFrame == NULL) |
---|
186 | n/a | _PyThreadState_GetFrame = threadstate_getframe; |
---|
187 | n/a | |
---|
188 | n/a | if (tstate != NULL) { |
---|
189 | n/a | tstate->interp = interp; |
---|
190 | n/a | |
---|
191 | n/a | tstate->frame = NULL; |
---|
192 | n/a | tstate->recursion_depth = 0; |
---|
193 | n/a | tstate->overflowed = 0; |
---|
194 | n/a | tstate->recursion_critical = 0; |
---|
195 | n/a | tstate->tracing = 0; |
---|
196 | n/a | tstate->use_tracing = 0; |
---|
197 | n/a | tstate->gilstate_counter = 0; |
---|
198 | n/a | tstate->async_exc = NULL; |
---|
199 | n/a | #ifdef WITH_THREAD |
---|
200 | n/a | tstate->thread_id = PyThread_get_thread_ident(); |
---|
201 | n/a | #else |
---|
202 | n/a | tstate->thread_id = 0; |
---|
203 | n/a | #endif |
---|
204 | n/a | |
---|
205 | n/a | tstate->dict = NULL; |
---|
206 | n/a | |
---|
207 | n/a | tstate->curexc_type = NULL; |
---|
208 | n/a | tstate->curexc_value = NULL; |
---|
209 | n/a | tstate->curexc_traceback = NULL; |
---|
210 | n/a | |
---|
211 | n/a | tstate->exc_type = NULL; |
---|
212 | n/a | tstate->exc_value = NULL; |
---|
213 | n/a | tstate->exc_traceback = NULL; |
---|
214 | n/a | |
---|
215 | n/a | tstate->c_profilefunc = NULL; |
---|
216 | n/a | tstate->c_tracefunc = NULL; |
---|
217 | n/a | tstate->c_profileobj = NULL; |
---|
218 | n/a | tstate->c_traceobj = NULL; |
---|
219 | n/a | |
---|
220 | n/a | tstate->trash_delete_nesting = 0; |
---|
221 | n/a | tstate->trash_delete_later = NULL; |
---|
222 | n/a | tstate->on_delete = NULL; |
---|
223 | n/a | tstate->on_delete_data = NULL; |
---|
224 | n/a | |
---|
225 | n/a | tstate->coroutine_wrapper = NULL; |
---|
226 | n/a | tstate->in_coroutine_wrapper = 0; |
---|
227 | n/a | tstate->co_extra_user_count = 0; |
---|
228 | n/a | |
---|
229 | n/a | tstate->async_gen_firstiter = NULL; |
---|
230 | n/a | tstate->async_gen_finalizer = NULL; |
---|
231 | n/a | |
---|
232 | n/a | if (init) |
---|
233 | n/a | _PyThreadState_Init(tstate); |
---|
234 | n/a | |
---|
235 | n/a | HEAD_LOCK(); |
---|
236 | n/a | tstate->prev = NULL; |
---|
237 | n/a | tstate->next = interp->tstate_head; |
---|
238 | n/a | if (tstate->next) |
---|
239 | n/a | tstate->next->prev = tstate; |
---|
240 | n/a | interp->tstate_head = tstate; |
---|
241 | n/a | HEAD_UNLOCK(); |
---|
242 | n/a | } |
---|
243 | n/a | |
---|
244 | n/a | return tstate; |
---|
245 | n/a | } |
---|
246 | n/a | |
---|
247 | n/a | PyThreadState * |
---|
248 | n/a | PyThreadState_New(PyInterpreterState *interp) |
---|
249 | n/a | { |
---|
250 | n/a | return new_threadstate(interp, 1); |
---|
251 | n/a | } |
---|
252 | n/a | |
---|
253 | n/a | PyThreadState * |
---|
254 | n/a | _PyThreadState_Prealloc(PyInterpreterState *interp) |
---|
255 | n/a | { |
---|
256 | n/a | return new_threadstate(interp, 0); |
---|
257 | n/a | } |
---|
258 | n/a | |
---|
259 | n/a | void |
---|
260 | n/a | _PyThreadState_Init(PyThreadState *tstate) |
---|
261 | n/a | { |
---|
262 | n/a | #ifdef WITH_THREAD |
---|
263 | n/a | _PyGILState_NoteThreadState(tstate); |
---|
264 | n/a | #endif |
---|
265 | n/a | } |
---|
266 | n/a | |
---|
267 | n/a | PyObject* |
---|
268 | n/a | PyState_FindModule(struct PyModuleDef* module) |
---|
269 | n/a | { |
---|
270 | n/a | Py_ssize_t index = module->m_base.m_index; |
---|
271 | n/a | PyInterpreterState *state = GET_INTERP_STATE(); |
---|
272 | n/a | PyObject *res; |
---|
273 | n/a | if (module->m_slots) { |
---|
274 | n/a | return NULL; |
---|
275 | n/a | } |
---|
276 | n/a | if (index == 0) |
---|
277 | n/a | return NULL; |
---|
278 | n/a | if (state->modules_by_index == NULL) |
---|
279 | n/a | return NULL; |
---|
280 | n/a | if (index >= PyList_GET_SIZE(state->modules_by_index)) |
---|
281 | n/a | return NULL; |
---|
282 | n/a | res = PyList_GET_ITEM(state->modules_by_index, index); |
---|
283 | n/a | return res==Py_None ? NULL : res; |
---|
284 | n/a | } |
---|
285 | n/a | |
---|
286 | n/a | int |
---|
287 | n/a | _PyState_AddModule(PyObject* module, struct PyModuleDef* def) |
---|
288 | n/a | { |
---|
289 | n/a | PyInterpreterState *state; |
---|
290 | n/a | if (!def) { |
---|
291 | n/a | assert(PyErr_Occurred()); |
---|
292 | n/a | return -1; |
---|
293 | n/a | } |
---|
294 | n/a | if (def->m_slots) { |
---|
295 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
296 | n/a | "PyState_AddModule called on module with slots"); |
---|
297 | n/a | return -1; |
---|
298 | n/a | } |
---|
299 | n/a | state = GET_INTERP_STATE(); |
---|
300 | n/a | if (!state->modules_by_index) { |
---|
301 | n/a | state->modules_by_index = PyList_New(0); |
---|
302 | n/a | if (!state->modules_by_index) |
---|
303 | n/a | return -1; |
---|
304 | n/a | } |
---|
305 | n/a | while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index) |
---|
306 | n/a | if (PyList_Append(state->modules_by_index, Py_None) < 0) |
---|
307 | n/a | return -1; |
---|
308 | n/a | Py_INCREF(module); |
---|
309 | n/a | return PyList_SetItem(state->modules_by_index, |
---|
310 | n/a | def->m_base.m_index, module); |
---|
311 | n/a | } |
---|
312 | n/a | |
---|
313 | n/a | int |
---|
314 | n/a | PyState_AddModule(PyObject* module, struct PyModuleDef* def) |
---|
315 | n/a | { |
---|
316 | n/a | Py_ssize_t index; |
---|
317 | n/a | PyInterpreterState *state = GET_INTERP_STATE(); |
---|
318 | n/a | if (!def) { |
---|
319 | n/a | Py_FatalError("PyState_AddModule: Module Definition is NULL"); |
---|
320 | n/a | return -1; |
---|
321 | n/a | } |
---|
322 | n/a | index = def->m_base.m_index; |
---|
323 | n/a | if (state->modules_by_index) { |
---|
324 | n/a | if(PyList_GET_SIZE(state->modules_by_index) >= index) { |
---|
325 | n/a | if(module == PyList_GET_ITEM(state->modules_by_index, index)) { |
---|
326 | n/a | Py_FatalError("PyState_AddModule: Module already added!"); |
---|
327 | n/a | return -1; |
---|
328 | n/a | } |
---|
329 | n/a | } |
---|
330 | n/a | } |
---|
331 | n/a | return _PyState_AddModule(module, def); |
---|
332 | n/a | } |
---|
333 | n/a | |
---|
334 | n/a | int |
---|
335 | n/a | PyState_RemoveModule(struct PyModuleDef* def) |
---|
336 | n/a | { |
---|
337 | n/a | PyInterpreterState *state; |
---|
338 | n/a | Py_ssize_t index = def->m_base.m_index; |
---|
339 | n/a | if (def->m_slots) { |
---|
340 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
341 | n/a | "PyState_RemoveModule called on module with slots"); |
---|
342 | n/a | return -1; |
---|
343 | n/a | } |
---|
344 | n/a | state = GET_INTERP_STATE(); |
---|
345 | n/a | if (index == 0) { |
---|
346 | n/a | Py_FatalError("PyState_RemoveModule: Module index invalid."); |
---|
347 | n/a | return -1; |
---|
348 | n/a | } |
---|
349 | n/a | if (state->modules_by_index == NULL) { |
---|
350 | n/a | Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible."); |
---|
351 | n/a | return -1; |
---|
352 | n/a | } |
---|
353 | n/a | if (index > PyList_GET_SIZE(state->modules_by_index)) { |
---|
354 | n/a | Py_FatalError("PyState_RemoveModule: Module index out of bounds."); |
---|
355 | n/a | return -1; |
---|
356 | n/a | } |
---|
357 | n/a | return PyList_SetItem(state->modules_by_index, index, Py_None); |
---|
358 | n/a | } |
---|
359 | n/a | |
---|
360 | n/a | /* used by import.c:PyImport_Cleanup */ |
---|
361 | n/a | void |
---|
362 | n/a | _PyState_ClearModules(void) |
---|
363 | n/a | { |
---|
364 | n/a | PyInterpreterState *state = GET_INTERP_STATE(); |
---|
365 | n/a | if (state->modules_by_index) { |
---|
366 | n/a | Py_ssize_t i; |
---|
367 | n/a | for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) { |
---|
368 | n/a | PyObject *m = PyList_GET_ITEM(state->modules_by_index, i); |
---|
369 | n/a | if (PyModule_Check(m)) { |
---|
370 | n/a | /* cleanup the saved copy of module dicts */ |
---|
371 | n/a | PyModuleDef *md = PyModule_GetDef(m); |
---|
372 | n/a | if (md) |
---|
373 | n/a | Py_CLEAR(md->m_base.m_copy); |
---|
374 | n/a | } |
---|
375 | n/a | } |
---|
376 | n/a | /* Setting modules_by_index to NULL could be dangerous, so we |
---|
377 | n/a | clear the list instead. */ |
---|
378 | n/a | if (PyList_SetSlice(state->modules_by_index, |
---|
379 | n/a | 0, PyList_GET_SIZE(state->modules_by_index), |
---|
380 | n/a | NULL)) |
---|
381 | n/a | PyErr_WriteUnraisable(state->modules_by_index); |
---|
382 | n/a | } |
---|
383 | n/a | } |
---|
384 | n/a | |
---|
385 | n/a | void |
---|
386 | n/a | PyThreadState_Clear(PyThreadState *tstate) |
---|
387 | n/a | { |
---|
388 | n/a | if (Py_VerboseFlag && tstate->frame != NULL) |
---|
389 | n/a | fprintf(stderr, |
---|
390 | n/a | "PyThreadState_Clear: warning: thread still has a frame\n"); |
---|
391 | n/a | |
---|
392 | n/a | Py_CLEAR(tstate->frame); |
---|
393 | n/a | |
---|
394 | n/a | Py_CLEAR(tstate->dict); |
---|
395 | n/a | Py_CLEAR(tstate->async_exc); |
---|
396 | n/a | |
---|
397 | n/a | Py_CLEAR(tstate->curexc_type); |
---|
398 | n/a | Py_CLEAR(tstate->curexc_value); |
---|
399 | n/a | Py_CLEAR(tstate->curexc_traceback); |
---|
400 | n/a | |
---|
401 | n/a | Py_CLEAR(tstate->exc_type); |
---|
402 | n/a | Py_CLEAR(tstate->exc_value); |
---|
403 | n/a | Py_CLEAR(tstate->exc_traceback); |
---|
404 | n/a | |
---|
405 | n/a | tstate->c_profilefunc = NULL; |
---|
406 | n/a | tstate->c_tracefunc = NULL; |
---|
407 | n/a | Py_CLEAR(tstate->c_profileobj); |
---|
408 | n/a | Py_CLEAR(tstate->c_traceobj); |
---|
409 | n/a | |
---|
410 | n/a | Py_CLEAR(tstate->coroutine_wrapper); |
---|
411 | n/a | Py_CLEAR(tstate->async_gen_firstiter); |
---|
412 | n/a | Py_CLEAR(tstate->async_gen_finalizer); |
---|
413 | n/a | } |
---|
414 | n/a | |
---|
415 | n/a | |
---|
416 | n/a | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ |
---|
417 | n/a | static void |
---|
418 | n/a | tstate_delete_common(PyThreadState *tstate) |
---|
419 | n/a | { |
---|
420 | n/a | PyInterpreterState *interp; |
---|
421 | n/a | if (tstate == NULL) |
---|
422 | n/a | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
---|
423 | n/a | interp = tstate->interp; |
---|
424 | n/a | if (interp == NULL) |
---|
425 | n/a | Py_FatalError("PyThreadState_Delete: NULL interp"); |
---|
426 | n/a | HEAD_LOCK(); |
---|
427 | n/a | if (tstate->prev) |
---|
428 | n/a | tstate->prev->next = tstate->next; |
---|
429 | n/a | else |
---|
430 | n/a | interp->tstate_head = tstate->next; |
---|
431 | n/a | if (tstate->next) |
---|
432 | n/a | tstate->next->prev = tstate->prev; |
---|
433 | n/a | HEAD_UNLOCK(); |
---|
434 | n/a | if (tstate->on_delete != NULL) { |
---|
435 | n/a | tstate->on_delete(tstate->on_delete_data); |
---|
436 | n/a | } |
---|
437 | n/a | PyMem_RawFree(tstate); |
---|
438 | n/a | } |
---|
439 | n/a | |
---|
440 | n/a | |
---|
441 | n/a | void |
---|
442 | n/a | PyThreadState_Delete(PyThreadState *tstate) |
---|
443 | n/a | { |
---|
444 | n/a | if (tstate == GET_TSTATE()) |
---|
445 | n/a | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
---|
446 | n/a | #ifdef WITH_THREAD |
---|
447 | n/a | if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) |
---|
448 | n/a | PyThread_delete_key_value(autoTLSkey); |
---|
449 | n/a | #endif /* WITH_THREAD */ |
---|
450 | n/a | tstate_delete_common(tstate); |
---|
451 | n/a | } |
---|
452 | n/a | |
---|
453 | n/a | |
---|
454 | n/a | #ifdef WITH_THREAD |
---|
455 | n/a | void |
---|
456 | n/a | PyThreadState_DeleteCurrent() |
---|
457 | n/a | { |
---|
458 | n/a | PyThreadState *tstate = GET_TSTATE(); |
---|
459 | n/a | if (tstate == NULL) |
---|
460 | n/a | Py_FatalError( |
---|
461 | n/a | "PyThreadState_DeleteCurrent: no current tstate"); |
---|
462 | n/a | tstate_delete_common(tstate); |
---|
463 | n/a | if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) |
---|
464 | n/a | PyThread_delete_key_value(autoTLSkey); |
---|
465 | n/a | SET_TSTATE(NULL); |
---|
466 | n/a | PyEval_ReleaseLock(); |
---|
467 | n/a | } |
---|
468 | n/a | #endif /* WITH_THREAD */ |
---|
469 | n/a | |
---|
470 | n/a | |
---|
471 | n/a | /* |
---|
472 | n/a | * Delete all thread states except the one passed as argument. |
---|
473 | n/a | * Note that, if there is a current thread state, it *must* be the one |
---|
474 | n/a | * passed as argument. Also, this won't touch any other interpreters |
---|
475 | n/a | * than the current one, since we don't know which thread state should |
---|
476 | n/a | * be kept in those other interpreteres. |
---|
477 | n/a | */ |
---|
478 | n/a | void |
---|
479 | n/a | _PyThreadState_DeleteExcept(PyThreadState *tstate) |
---|
480 | n/a | { |
---|
481 | n/a | PyInterpreterState *interp = tstate->interp; |
---|
482 | n/a | PyThreadState *p, *next, *garbage; |
---|
483 | n/a | HEAD_LOCK(); |
---|
484 | n/a | /* Remove all thread states, except tstate, from the linked list of |
---|
485 | n/a | thread states. This will allow calling PyThreadState_Clear() |
---|
486 | n/a | without holding the lock. */ |
---|
487 | n/a | garbage = interp->tstate_head; |
---|
488 | n/a | if (garbage == tstate) |
---|
489 | n/a | garbage = tstate->next; |
---|
490 | n/a | if (tstate->prev) |
---|
491 | n/a | tstate->prev->next = tstate->next; |
---|
492 | n/a | if (tstate->next) |
---|
493 | n/a | tstate->next->prev = tstate->prev; |
---|
494 | n/a | tstate->prev = tstate->next = NULL; |
---|
495 | n/a | interp->tstate_head = tstate; |
---|
496 | n/a | HEAD_UNLOCK(); |
---|
497 | n/a | /* Clear and deallocate all stale thread states. Even if this |
---|
498 | n/a | executes Python code, we should be safe since it executes |
---|
499 | n/a | in the current thread, not one of the stale threads. */ |
---|
500 | n/a | for (p = garbage; p; p = next) { |
---|
501 | n/a | next = p->next; |
---|
502 | n/a | PyThreadState_Clear(p); |
---|
503 | n/a | PyMem_RawFree(p); |
---|
504 | n/a | } |
---|
505 | n/a | } |
---|
506 | n/a | |
---|
507 | n/a | |
---|
508 | n/a | PyThreadState * |
---|
509 | n/a | _PyThreadState_UncheckedGet(void) |
---|
510 | n/a | { |
---|
511 | n/a | return GET_TSTATE(); |
---|
512 | n/a | } |
---|
513 | n/a | |
---|
514 | n/a | |
---|
515 | n/a | PyThreadState * |
---|
516 | n/a | PyThreadState_Get(void) |
---|
517 | n/a | { |
---|
518 | n/a | PyThreadState *tstate = GET_TSTATE(); |
---|
519 | n/a | if (tstate == NULL) |
---|
520 | n/a | Py_FatalError("PyThreadState_Get: no current thread"); |
---|
521 | n/a | |
---|
522 | n/a | return tstate; |
---|
523 | n/a | } |
---|
524 | n/a | |
---|
525 | n/a | |
---|
526 | n/a | PyThreadState * |
---|
527 | n/a | PyThreadState_Swap(PyThreadState *newts) |
---|
528 | n/a | { |
---|
529 | n/a | PyThreadState *oldts = GET_TSTATE(); |
---|
530 | n/a | |
---|
531 | n/a | SET_TSTATE(newts); |
---|
532 | n/a | /* It should not be possible for more than one thread state |
---|
533 | n/a | to be used for a thread. Check this the best we can in debug |
---|
534 | n/a | builds. |
---|
535 | n/a | */ |
---|
536 | n/a | #if defined(Py_DEBUG) && defined(WITH_THREAD) |
---|
537 | n/a | if (newts) { |
---|
538 | n/a | /* This can be called from PyEval_RestoreThread(). Similar |
---|
539 | n/a | to it, we need to ensure errno doesn't change. |
---|
540 | n/a | */ |
---|
541 | n/a | int err = errno; |
---|
542 | n/a | PyThreadState *check = PyGILState_GetThisThreadState(); |
---|
543 | n/a | if (check && check->interp == newts->interp && check != newts) |
---|
544 | n/a | Py_FatalError("Invalid thread state for this thread"); |
---|
545 | n/a | errno = err; |
---|
546 | n/a | } |
---|
547 | n/a | #endif |
---|
548 | n/a | return oldts; |
---|
549 | n/a | } |
---|
550 | n/a | |
---|
551 | n/a | /* An extension mechanism to store arbitrary additional per-thread state. |
---|
552 | n/a | PyThreadState_GetDict() returns a dictionary that can be used to hold such |
---|
553 | n/a | state; the caller should pick a unique key and store its state there. If |
---|
554 | n/a | PyThreadState_GetDict() returns NULL, an exception has *not* been raised |
---|
555 | n/a | and the caller should assume no per-thread state is available. */ |
---|
556 | n/a | |
---|
557 | n/a | PyObject * |
---|
558 | n/a | PyThreadState_GetDict(void) |
---|
559 | n/a | { |
---|
560 | n/a | PyThreadState *tstate = GET_TSTATE(); |
---|
561 | n/a | if (tstate == NULL) |
---|
562 | n/a | return NULL; |
---|
563 | n/a | |
---|
564 | n/a | if (tstate->dict == NULL) { |
---|
565 | n/a | PyObject *d; |
---|
566 | n/a | tstate->dict = d = PyDict_New(); |
---|
567 | n/a | if (d == NULL) |
---|
568 | n/a | PyErr_Clear(); |
---|
569 | n/a | } |
---|
570 | n/a | return tstate->dict; |
---|
571 | n/a | } |
---|
572 | n/a | |
---|
573 | n/a | |
---|
574 | n/a | /* Asynchronously raise an exception in a thread. |
---|
575 | n/a | Requested by Just van Rossum and Alex Martelli. |
---|
576 | n/a | To prevent naive misuse, you must write your own extension |
---|
577 | n/a | to call this, or use ctypes. Must be called with the GIL held. |
---|
578 | n/a | Returns the number of tstates modified (normally 1, but 0 if `id` didn't |
---|
579 | n/a | match any known thread id). Can be called with exc=NULL to clear an |
---|
580 | n/a | existing async exception. This raises no exceptions. */ |
---|
581 | n/a | |
---|
582 | n/a | int |
---|
583 | n/a | PyThreadState_SetAsyncExc(long id, PyObject *exc) { |
---|
584 | n/a | PyInterpreterState *interp = GET_INTERP_STATE(); |
---|
585 | n/a | PyThreadState *p; |
---|
586 | n/a | |
---|
587 | n/a | /* Although the GIL is held, a few C API functions can be called |
---|
588 | n/a | * without the GIL held, and in particular some that create and |
---|
589 | n/a | * destroy thread and interpreter states. Those can mutate the |
---|
590 | n/a | * list of thread states we're traversing, so to prevent that we lock |
---|
591 | n/a | * head_mutex for the duration. |
---|
592 | n/a | */ |
---|
593 | n/a | HEAD_LOCK(); |
---|
594 | n/a | for (p = interp->tstate_head; p != NULL; p = p->next) { |
---|
595 | n/a | if (p->thread_id == id) { |
---|
596 | n/a | /* Tricky: we need to decref the current value |
---|
597 | n/a | * (if any) in p->async_exc, but that can in turn |
---|
598 | n/a | * allow arbitrary Python code to run, including |
---|
599 | n/a | * perhaps calls to this function. To prevent |
---|
600 | n/a | * deadlock, we need to release head_mutex before |
---|
601 | n/a | * the decref. |
---|
602 | n/a | */ |
---|
603 | n/a | PyObject *old_exc = p->async_exc; |
---|
604 | n/a | Py_XINCREF(exc); |
---|
605 | n/a | p->async_exc = exc; |
---|
606 | n/a | HEAD_UNLOCK(); |
---|
607 | n/a | Py_XDECREF(old_exc); |
---|
608 | n/a | _PyEval_SignalAsyncExc(); |
---|
609 | n/a | return 1; |
---|
610 | n/a | } |
---|
611 | n/a | } |
---|
612 | n/a | HEAD_UNLOCK(); |
---|
613 | n/a | return 0; |
---|
614 | n/a | } |
---|
615 | n/a | |
---|
616 | n/a | |
---|
617 | n/a | /* Routines for advanced debuggers, requested by David Beazley. |
---|
618 | n/a | Don't use unless you know what you are doing! */ |
---|
619 | n/a | |
---|
620 | n/a | PyInterpreterState * |
---|
621 | n/a | PyInterpreterState_Head(void) |
---|
622 | n/a | { |
---|
623 | n/a | return interp_head; |
---|
624 | n/a | } |
---|
625 | n/a | |
---|
626 | n/a | PyInterpreterState * |
---|
627 | n/a | PyInterpreterState_Next(PyInterpreterState *interp) { |
---|
628 | n/a | return interp->next; |
---|
629 | n/a | } |
---|
630 | n/a | |
---|
631 | n/a | PyThreadState * |
---|
632 | n/a | PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
---|
633 | n/a | return interp->tstate_head; |
---|
634 | n/a | } |
---|
635 | n/a | |
---|
636 | n/a | PyThreadState * |
---|
637 | n/a | PyThreadState_Next(PyThreadState *tstate) { |
---|
638 | n/a | return tstate->next; |
---|
639 | n/a | } |
---|
640 | n/a | |
---|
641 | n/a | /* The implementation of sys._current_frames(). This is intended to be |
---|
642 | n/a | called with the GIL held, as it will be when called via |
---|
643 | n/a | sys._current_frames(). It's possible it would work fine even without |
---|
644 | n/a | the GIL held, but haven't thought enough about that. |
---|
645 | n/a | */ |
---|
646 | n/a | PyObject * |
---|
647 | n/a | _PyThread_CurrentFrames(void) |
---|
648 | n/a | { |
---|
649 | n/a | PyObject *result; |
---|
650 | n/a | PyInterpreterState *i; |
---|
651 | n/a | |
---|
652 | n/a | result = PyDict_New(); |
---|
653 | n/a | if (result == NULL) |
---|
654 | n/a | return NULL; |
---|
655 | n/a | |
---|
656 | n/a | /* for i in all interpreters: |
---|
657 | n/a | * for t in all of i's thread states: |
---|
658 | n/a | * if t's frame isn't NULL, map t's id to its frame |
---|
659 | n/a | * Because these lists can mutate even when the GIL is held, we |
---|
660 | n/a | * need to grab head_mutex for the duration. |
---|
661 | n/a | */ |
---|
662 | n/a | HEAD_LOCK(); |
---|
663 | n/a | for (i = interp_head; i != NULL; i = i->next) { |
---|
664 | n/a | PyThreadState *t; |
---|
665 | n/a | for (t = i->tstate_head; t != NULL; t = t->next) { |
---|
666 | n/a | PyObject *id; |
---|
667 | n/a | int stat; |
---|
668 | n/a | struct _frame *frame = t->frame; |
---|
669 | n/a | if (frame == NULL) |
---|
670 | n/a | continue; |
---|
671 | n/a | id = PyLong_FromLong(t->thread_id); |
---|
672 | n/a | if (id == NULL) |
---|
673 | n/a | goto Fail; |
---|
674 | n/a | stat = PyDict_SetItem(result, id, (PyObject *)frame); |
---|
675 | n/a | Py_DECREF(id); |
---|
676 | n/a | if (stat < 0) |
---|
677 | n/a | goto Fail; |
---|
678 | n/a | } |
---|
679 | n/a | } |
---|
680 | n/a | HEAD_UNLOCK(); |
---|
681 | n/a | return result; |
---|
682 | n/a | |
---|
683 | n/a | Fail: |
---|
684 | n/a | HEAD_UNLOCK(); |
---|
685 | n/a | Py_DECREF(result); |
---|
686 | n/a | return NULL; |
---|
687 | n/a | } |
---|
688 | n/a | |
---|
689 | n/a | /* Python "auto thread state" API. */ |
---|
690 | n/a | #ifdef WITH_THREAD |
---|
691 | n/a | |
---|
692 | n/a | /* Keep this as a static, as it is not reliable! It can only |
---|
693 | n/a | ever be compared to the state for the *current* thread. |
---|
694 | n/a | * If not equal, then it doesn't matter that the actual |
---|
695 | n/a | value may change immediately after comparison, as it can't |
---|
696 | n/a | possibly change to the current thread's state. |
---|
697 | n/a | * If equal, then the current thread holds the lock, so the value can't |
---|
698 | n/a | change until we yield the lock. |
---|
699 | n/a | */ |
---|
700 | n/a | static int |
---|
701 | n/a | PyThreadState_IsCurrent(PyThreadState *tstate) |
---|
702 | n/a | { |
---|
703 | n/a | /* Must be the tstate for this thread */ |
---|
704 | n/a | assert(PyGILState_GetThisThreadState()==tstate); |
---|
705 | n/a | return tstate == GET_TSTATE(); |
---|
706 | n/a | } |
---|
707 | n/a | |
---|
708 | n/a | /* Internal initialization/finalization functions called by |
---|
709 | n/a | Py_Initialize/Py_FinalizeEx |
---|
710 | n/a | */ |
---|
711 | n/a | void |
---|
712 | n/a | _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) |
---|
713 | n/a | { |
---|
714 | n/a | assert(i && t); /* must init with valid states */ |
---|
715 | n/a | autoTLSkey = PyThread_create_key(); |
---|
716 | n/a | if (autoTLSkey == -1) |
---|
717 | n/a | Py_FatalError("Could not allocate TLS entry"); |
---|
718 | n/a | autoInterpreterState = i; |
---|
719 | n/a | assert(PyThread_get_key_value(autoTLSkey) == NULL); |
---|
720 | n/a | assert(t->gilstate_counter == 0); |
---|
721 | n/a | |
---|
722 | n/a | _PyGILState_NoteThreadState(t); |
---|
723 | n/a | } |
---|
724 | n/a | |
---|
725 | n/a | PyInterpreterState * |
---|
726 | n/a | _PyGILState_GetInterpreterStateUnsafe(void) |
---|
727 | n/a | { |
---|
728 | n/a | return autoInterpreterState; |
---|
729 | n/a | } |
---|
730 | n/a | |
---|
731 | n/a | void |
---|
732 | n/a | _PyGILState_Fini(void) |
---|
733 | n/a | { |
---|
734 | n/a | PyThread_delete_key(autoTLSkey); |
---|
735 | n/a | autoTLSkey = -1; |
---|
736 | n/a | autoInterpreterState = NULL; |
---|
737 | n/a | } |
---|
738 | n/a | |
---|
739 | n/a | /* Reset the TLS key - called by PyOS_AfterFork(). |
---|
740 | n/a | * This should not be necessary, but some - buggy - pthread implementations |
---|
741 | n/a | * don't reset TLS upon fork(), see issue #10517. |
---|
742 | n/a | */ |
---|
743 | n/a | void |
---|
744 | n/a | _PyGILState_Reinit(void) |
---|
745 | n/a | { |
---|
746 | n/a | PyThreadState *tstate = PyGILState_GetThisThreadState(); |
---|
747 | n/a | PyThread_delete_key(autoTLSkey); |
---|
748 | n/a | if ((autoTLSkey = PyThread_create_key()) == -1) |
---|
749 | n/a | Py_FatalError("Could not allocate TLS entry"); |
---|
750 | n/a | |
---|
751 | n/a | /* If the thread had an associated auto thread state, reassociate it with |
---|
752 | n/a | * the new key. */ |
---|
753 | n/a | if (tstate && PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) |
---|
754 | n/a | Py_FatalError("Couldn't create autoTLSkey mapping"); |
---|
755 | n/a | } |
---|
756 | n/a | |
---|
757 | n/a | /* When a thread state is created for a thread by some mechanism other than |
---|
758 | n/a | PyGILState_Ensure, it's important that the GILState machinery knows about |
---|
759 | n/a | it so it doesn't try to create another thread state for the thread (this is |
---|
760 | n/a | a better fix for SF bug #1010677 than the first one attempted). |
---|
761 | n/a | */ |
---|
762 | n/a | static void |
---|
763 | n/a | _PyGILState_NoteThreadState(PyThreadState* tstate) |
---|
764 | n/a | { |
---|
765 | n/a | /* If autoTLSkey isn't initialized, this must be the very first |
---|
766 | n/a | threadstate created in Py_Initialize(). Don't do anything for now |
---|
767 | n/a | (we'll be back here when _PyGILState_Init is called). */ |
---|
768 | n/a | if (!autoInterpreterState) |
---|
769 | n/a | return; |
---|
770 | n/a | |
---|
771 | n/a | /* Stick the thread state for this thread in thread local storage. |
---|
772 | n/a | |
---|
773 | n/a | The only situation where you can legitimately have more than one |
---|
774 | n/a | thread state for an OS level thread is when there are multiple |
---|
775 | n/a | interpreters. |
---|
776 | n/a | |
---|
777 | n/a | You shouldn't really be using the PyGILState_ APIs anyway (see issues |
---|
778 | n/a | #10915 and #15751). |
---|
779 | n/a | |
---|
780 | n/a | The first thread state created for that given OS level thread will |
---|
781 | n/a | "win", which seems reasonable behaviour. |
---|
782 | n/a | */ |
---|
783 | n/a | if (PyThread_get_key_value(autoTLSkey) == NULL) { |
---|
784 | n/a | if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) |
---|
785 | n/a | Py_FatalError("Couldn't create autoTLSkey mapping"); |
---|
786 | n/a | } |
---|
787 | n/a | |
---|
788 | n/a | /* PyGILState_Release must not try to delete this thread state. */ |
---|
789 | n/a | tstate->gilstate_counter = 1; |
---|
790 | n/a | } |
---|
791 | n/a | |
---|
792 | n/a | /* The public functions */ |
---|
793 | n/a | PyThreadState * |
---|
794 | n/a | PyGILState_GetThisThreadState(void) |
---|
795 | n/a | { |
---|
796 | n/a | if (autoInterpreterState == NULL) |
---|
797 | n/a | return NULL; |
---|
798 | n/a | return (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
---|
799 | n/a | } |
---|
800 | n/a | |
---|
801 | n/a | int |
---|
802 | n/a | PyGILState_Check(void) |
---|
803 | n/a | { |
---|
804 | n/a | PyThreadState *tstate; |
---|
805 | n/a | |
---|
806 | n/a | if (!_PyGILState_check_enabled) |
---|
807 | n/a | return 1; |
---|
808 | n/a | |
---|
809 | n/a | if (autoTLSkey == -1) |
---|
810 | n/a | return 1; |
---|
811 | n/a | |
---|
812 | n/a | tstate = GET_TSTATE(); |
---|
813 | n/a | if (tstate == NULL) |
---|
814 | n/a | return 0; |
---|
815 | n/a | |
---|
816 | n/a | return (tstate == PyGILState_GetThisThreadState()); |
---|
817 | n/a | } |
---|
818 | n/a | |
---|
819 | n/a | PyGILState_STATE |
---|
820 | n/a | PyGILState_Ensure(void) |
---|
821 | n/a | { |
---|
822 | n/a | int current; |
---|
823 | n/a | PyThreadState *tcur; |
---|
824 | n/a | /* Note that we do not auto-init Python here - apart from |
---|
825 | n/a | potential races with 2 threads auto-initializing, pep-311 |
---|
826 | n/a | spells out other issues. Embedders are expected to have |
---|
827 | n/a | called Py_Initialize() and usually PyEval_InitThreads(). |
---|
828 | n/a | */ |
---|
829 | n/a | assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ |
---|
830 | n/a | tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
---|
831 | n/a | if (tcur == NULL) { |
---|
832 | n/a | /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is |
---|
833 | n/a | called from a new thread for the first time, we need the create the |
---|
834 | n/a | GIL. */ |
---|
835 | n/a | PyEval_InitThreads(); |
---|
836 | n/a | |
---|
837 | n/a | /* Create a new thread state for this thread */ |
---|
838 | n/a | tcur = PyThreadState_New(autoInterpreterState); |
---|
839 | n/a | if (tcur == NULL) |
---|
840 | n/a | Py_FatalError("Couldn't create thread-state for new thread"); |
---|
841 | n/a | /* This is our thread state! We'll need to delete it in the |
---|
842 | n/a | matching call to PyGILState_Release(). */ |
---|
843 | n/a | tcur->gilstate_counter = 0; |
---|
844 | n/a | current = 0; /* new thread state is never current */ |
---|
845 | n/a | } |
---|
846 | n/a | else |
---|
847 | n/a | current = PyThreadState_IsCurrent(tcur); |
---|
848 | n/a | if (current == 0) |
---|
849 | n/a | PyEval_RestoreThread(tcur); |
---|
850 | n/a | /* Update our counter in the thread-state - no need for locks: |
---|
851 | n/a | - tcur will remain valid as we hold the GIL. |
---|
852 | n/a | - the counter is safe as we are the only thread "allowed" |
---|
853 | n/a | to modify this value |
---|
854 | n/a | */ |
---|
855 | n/a | ++tcur->gilstate_counter; |
---|
856 | n/a | return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; |
---|
857 | n/a | } |
---|
858 | n/a | |
---|
859 | n/a | void |
---|
860 | n/a | PyGILState_Release(PyGILState_STATE oldstate) |
---|
861 | n/a | { |
---|
862 | n/a | PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value( |
---|
863 | n/a | autoTLSkey); |
---|
864 | n/a | if (tcur == NULL) |
---|
865 | n/a | Py_FatalError("auto-releasing thread-state, " |
---|
866 | n/a | "but no thread-state for this thread"); |
---|
867 | n/a | /* We must hold the GIL and have our thread state current */ |
---|
868 | n/a | /* XXX - remove the check - the assert should be fine, |
---|
869 | n/a | but while this is very new (April 2003), the extra check |
---|
870 | n/a | by release-only users can't hurt. |
---|
871 | n/a | */ |
---|
872 | n/a | if (! PyThreadState_IsCurrent(tcur)) |
---|
873 | n/a | Py_FatalError("This thread state must be current when releasing"); |
---|
874 | n/a | assert(PyThreadState_IsCurrent(tcur)); |
---|
875 | n/a | --tcur->gilstate_counter; |
---|
876 | n/a | assert(tcur->gilstate_counter >= 0); /* illegal counter value */ |
---|
877 | n/a | |
---|
878 | n/a | /* If we're going to destroy this thread-state, we must |
---|
879 | n/a | * clear it while the GIL is held, as destructors may run. |
---|
880 | n/a | */ |
---|
881 | n/a | if (tcur->gilstate_counter == 0) { |
---|
882 | n/a | /* can't have been locked when we created it */ |
---|
883 | n/a | assert(oldstate == PyGILState_UNLOCKED); |
---|
884 | n/a | PyThreadState_Clear(tcur); |
---|
885 | n/a | /* Delete the thread-state. Note this releases the GIL too! |
---|
886 | n/a | * It's vital that the GIL be held here, to avoid shutdown |
---|
887 | n/a | * races; see bugs 225673 and 1061968 (that nasty bug has a |
---|
888 | n/a | * habit of coming back). |
---|
889 | n/a | */ |
---|
890 | n/a | PyThreadState_DeleteCurrent(); |
---|
891 | n/a | } |
---|
892 | n/a | /* Release the lock if necessary */ |
---|
893 | n/a | else if (oldstate == PyGILState_UNLOCKED) |
---|
894 | n/a | PyEval_SaveThread(); |
---|
895 | n/a | } |
---|
896 | n/a | |
---|
897 | n/a | #endif /* WITH_THREAD */ |
---|
898 | n/a | |
---|
899 | n/a | #ifdef __cplusplus |
---|
900 | n/a | } |
---|
901 | n/a | #endif |
---|
902 | n/a | |
---|
903 | n/a | |
---|