Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(29488)

Side by Side Diff: Python/pystate.c

Issue 10915: Make the PyGILState API compatible with multiple interpreters
Patch Set: Created 8 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Modules/_threadmodule.c ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* Thread and interpreter state structures and their interfaces */ 2 /* Thread and interpreter state structures and their interfaces */
3 3
4 #include "Python.h" 4 #include "Python.h"
5 5
6 /* -------------------------------------------------------------------------- 6 /* --------------------------------------------------------------------------
7 CAUTION 7 CAUTION
8 8
9 Always use malloc() and free() directly in this file. A number of these 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 10 functions are advertised as safe to call when the GIL isn't held, and in
(...skipping 16 matching lines...) Expand all
27 #include "pythread.h" 27 #include "pythread.h"
28 static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ 28 static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
29 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()) ) 29 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()) )
30 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) 30 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
31 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) 31 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
32 32
33 #ifdef __cplusplus 33 #ifdef __cplusplus
34 extern "C" { 34 extern "C" {
35 #endif 35 #endif
36 36
37 /* The single PyInterpreterState used by this process' 37 /* The single PyInterpreterState used by the legacy PyGILState_* API
38 GILState implementation 38 (non-Ex variants)
39 */ 39 */
40 static PyInterpreterState *autoInterpreterState = NULL; 40 static PyInterpreterState *main_interp = NULL;
41 static int autoTLSkey = 0;
42 #else 41 #else
43 #define HEAD_INIT() /* Nothing */ 42 #define HEAD_INIT() /* Nothing */
44 #define HEAD_LOCK() /* Nothing */ 43 #define HEAD_LOCK() /* Nothing */
45 #define HEAD_UNLOCK() /* Nothing */ 44 #define HEAD_UNLOCK() /* Nothing */
46 #endif 45 #endif
47 46
48 static PyInterpreterState *interp_head = NULL; 47 static PyInterpreterState *interp_head = NULL;
49 48
50 /* Assuming the current thread holds the GIL, this is the 49 /* Assuming the current thread holds the GIL, this is the
51 PyThreadState for the current thread. */ 50 PyThreadState for the current thread. */
52 _Py_atomic_address _PyThreadState_Current = {NULL}; 51 _Py_atomic_address _PyThreadState_Current = {NULL};
53 PyThreadFrameGetter _PyThreadState_GetFrame = NULL; 52 PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
54 53
55 #ifdef WITH_THREAD 54 #ifdef WITH_THREAD
56 static void _PyGILState_NoteThreadState(PyThreadState* tstate); 55 static void _PyGILState_NoteThreadState(PyInterpreterState *, PyThreadState *);
57 #endif 56 #endif
58 57
59 58
60 PyInterpreterState * 59 PyInterpreterState *
61 PyInterpreterState_New(void) 60 PyInterpreterState_New(void)
62 { 61 {
63 PyInterpreterState *interp = (PyInterpreterState *) 62 PyInterpreterState *interp = (PyInterpreterState *)
64 malloc(sizeof(PyInterpreterState)); 63 malloc(sizeof(PyInterpreterState));
65 64
66 if (interp != NULL) { 65 if (interp != NULL) {
67 HEAD_INIT(); 66 HEAD_INIT();
68 #ifdef WITH_THREAD 67 #ifdef WITH_THREAD
69 if (head_mutex == NULL) 68 if (head_mutex == NULL)
70 Py_FatalError("Can't initialize threads for interpreter"); 69 Py_FatalError("Can't initialize threads for interpreter");
70 interp->auto_tls_key = PyThread_create_key();
71 if (interp->auto_tls_key == -1)
72 Py_FatalError("Could not allocate TLS entry");
73 assert(PyThread_get_key_value(interp->auto_tls_key) == NULL);
71 #endif 74 #endif
72 interp->modules = NULL; 75 interp->modules = NULL;
73 interp->modules_reloading = NULL; 76 interp->modules_reloading = NULL;
74 interp->modules_by_index = NULL; 77 interp->modules_by_index = NULL;
75 interp->sysdict = NULL; 78 interp->sysdict = NULL;
76 interp->builtins = NULL; 79 interp->builtins = NULL;
77 interp->tstate_head = NULL; 80 interp->tstate_head = NULL;
78 interp->codec_search_path = NULL; 81 interp->codec_search_path = NULL;
79 interp->codec_search_cache = NULL; 82 interp->codec_search_cache = NULL;
80 interp->codec_error_registry = NULL; 83 interp->codec_error_registry = NULL;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 for (p = &interp_head; ; p = &(*p)->next) { 143 for (p = &interp_head; ; p = &(*p)->next) {
141 if (*p == NULL) 144 if (*p == NULL)
142 Py_FatalError( 145 Py_FatalError(
143 "PyInterpreterState_Delete: invalid interp"); 146 "PyInterpreterState_Delete: invalid interp");
144 if (*p == interp) 147 if (*p == interp)
145 break; 148 break;
146 } 149 }
147 if (interp->tstate_head != NULL) 150 if (interp->tstate_head != NULL)
148 Py_FatalError("PyInterpreterState_Delete: remaining threads"); 151 Py_FatalError("PyInterpreterState_Delete: remaining threads");
149 *p = interp->next; 152 *p = interp->next;
153 #ifdef WITH_THREAD
154 PyThread_delete_key(interp->auto_tls_key);
155 #endif
150 HEAD_UNLOCK(); 156 HEAD_UNLOCK();
151 free(interp); 157 free(interp);
152 } 158 }
153 159
154 160
155 /* Default implementation for _PyThreadState_GetFrame */ 161 /* Default implementation for _PyThreadState_GetFrame */
156 static struct _frame * 162 static struct _frame *
157 threadstate_getframe(PyThreadState *self) 163 threadstate_getframe(PyThreadState *self)
158 { 164 {
159 return self->frame; 165 return self->frame;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 tstate->exc_type = NULL; 200 tstate->exc_type = NULL;
195 tstate->exc_value = NULL; 201 tstate->exc_value = NULL;
196 tstate->exc_traceback = NULL; 202 tstate->exc_traceback = NULL;
197 203
198 tstate->c_profilefunc = NULL; 204 tstate->c_profilefunc = NULL;
199 tstate->c_tracefunc = NULL; 205 tstate->c_tracefunc = NULL;
200 tstate->c_profileobj = NULL; 206 tstate->c_profileobj = NULL;
201 tstate->c_traceobj = NULL; 207 tstate->c_traceobj = NULL;
202 208
203 if (init) 209 if (init)
204 _PyThreadState_Init(tstate); 210 _PyThreadState_Init(interp, tstate);
205 211
206 HEAD_LOCK(); 212 HEAD_LOCK();
207 tstate->next = interp->tstate_head; 213 tstate->next = interp->tstate_head;
208 interp->tstate_head = tstate; 214 interp->tstate_head = tstate;
209 HEAD_UNLOCK(); 215 HEAD_UNLOCK();
210 } 216 }
211 217
212 return tstate; 218 return tstate;
213 } 219 }
214 220
215 PyThreadState * 221 PyThreadState *
216 PyThreadState_New(PyInterpreterState *interp) 222 PyThreadState_New(PyInterpreterState *interp)
217 { 223 {
218 return new_threadstate(interp, 1); 224 return new_threadstate(interp, 1);
219 } 225 }
220 226
221 PyThreadState * 227 PyThreadState *
222 _PyThreadState_Prealloc(PyInterpreterState *interp) 228 _PyThreadState_Prealloc(PyInterpreterState *interp)
223 { 229 {
224 return new_threadstate(interp, 0); 230 return new_threadstate(interp, 0);
225 } 231 }
226 232
227 void 233 void
228 _PyThreadState_Init(PyThreadState *tstate) 234 _PyThreadState_Init(PyInterpreterState *interp, PyThreadState *tstate)
229 { 235 {
230 #ifdef WITH_THREAD 236 #ifdef WITH_THREAD
231 _PyGILState_NoteThreadState(tstate); 237 _PyGILState_NoteThreadState(interp, tstate);
232 #endif 238 #endif
233 } 239 }
234 240
235 PyObject* 241 PyObject*
236 PyState_FindModule(struct PyModuleDef* m) 242 PyState_FindModule(struct PyModuleDef* m)
237 { 243 {
238 Py_ssize_t index = m->m_base.m_index; 244 Py_ssize_t index = m->m_base.m_index;
239 PyInterpreterState *state = PyThreadState_GET()->interp; 245 PyInterpreterState *state = PyThreadState_GET()->interp;
240 PyObject *res; 246 PyObject *res;
241 if (index == 0) 247 if (index == 0)
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 } 335 }
330 *p = tstate->next; 336 *p = tstate->next;
331 HEAD_UNLOCK(); 337 HEAD_UNLOCK();
332 free(tstate); 338 free(tstate);
333 } 339 }
334 340
335 341
336 void 342 void
337 PyThreadState_Delete(PyThreadState *tstate) 343 PyThreadState_Delete(PyThreadState *tstate)
338 { 344 {
345 PyInterpreterState *interp = tstate->interp;
339 if (tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current)) 346 if (tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current))
340 Py_FatalError("PyThreadState_Delete: tstate is still current"); 347 Py_FatalError("PyThreadState_Delete: tstate is still current");
341 tstate_delete_common(tstate); 348 tstate_delete_common(tstate);
342 #ifdef WITH_THREAD 349 #ifdef WITH_THREAD
343 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) 350 if (PyThread_get_key_value(interp->auto_tls_key) == tstate)
344 PyThread_delete_key_value(autoTLSkey); 351 PyThread_delete_key_value(interp->auto_tls_key);
345 #endif /* WITH_THREAD */ 352 #endif /* WITH_THREAD */
346 } 353 }
347 354
348 355
349 #ifdef WITH_THREAD 356 #ifdef WITH_THREAD
350 void 357 void
351 PyThreadState_DeleteCurrent() 358 PyThreadState_DeleteCurrent()
352 { 359 {
353 PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed( 360 PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
354 &_PyThreadState_Current); 361 &_PyThreadState_Current);
362 PyInterpreterState *interp = tstate->interp;
355 if (tstate == NULL) 363 if (tstate == NULL)
356 Py_FatalError( 364 Py_FatalError(
357 "PyThreadState_DeleteCurrent: no current tstate"); 365 "PyThreadState_DeleteCurrent: no current tstate");
358 _Py_atomic_store_relaxed(&_PyThreadState_Current, NULL); 366 _Py_atomic_store_relaxed(&_PyThreadState_Current, NULL);
359 tstate_delete_common(tstate); 367 tstate_delete_common(tstate);
360 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) 368 if (interp && PyThread_get_key_value(interp->auto_tls_key) == tstate)
361 PyThread_delete_key_value(autoTLSkey); 369 PyThread_delete_key_value(interp->auto_tls_key);
362 PyEval_ReleaseLock(); 370 PyEval_ReleaseLock();
363 } 371 }
364 #endif /* WITH_THREAD */ 372 #endif /* WITH_THREAD */
365 373
366 374
367 PyThreadState * 375 PyThreadState *
368 PyThreadState_Get(void) 376 PyThreadState_Get(void)
369 { 377 {
370 PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed( 378 PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
371 &_PyThreadState_Current); 379 &_PyThreadState_Current);
(...skipping 14 matching lines...) Expand all
386 /* It should not be possible for more than one thread state 394 /* It should not be possible for more than one thread state
387 to be used for a thread. Check this the best we can in debug 395 to be used for a thread. Check this the best we can in debug
388 builds. 396 builds.
389 */ 397 */
390 #if defined(Py_DEBUG) && defined(WITH_THREAD) 398 #if defined(Py_DEBUG) && defined(WITH_THREAD)
391 if (newts) { 399 if (newts) {
392 /* This can be called from PyEval_RestoreThread(). Similar 400 /* This can be called from PyEval_RestoreThread(). Similar
393 to it, we need to ensure errno doesn't change. 401 to it, we need to ensure errno doesn't change.
394 */ 402 */
395 int err = errno; 403 int err = errno;
404 /* XXX why this and not oldts instead? */
396 PyThreadState *check = PyGILState_GetThisThreadState(); 405 PyThreadState *check = PyGILState_GetThisThreadState();
397 if (check && check->interp == newts->interp && check != newts) 406 if (check && check->interp == newts->interp && check != newts)
398 Py_FatalError("Invalid thread state for this thread"); 407 Py_FatalError("Invalid thread state for this thread");
399 errno = err; 408 errno = err;
400 } 409 }
401 #endif 410 #endif
402 return oldts; 411 return oldts;
403 } 412 }
404 413
405 /* An extension mechanism to store arbitrary additional per-thread state. 414 /* An extension mechanism to store arbitrary additional per-thread state.
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 * If not equal, then it doesn't matter that the actual 559 * If not equal, then it doesn't matter that the actual
551 value may change immediately after comparison, as it can't 560 value may change immediately after comparison, as it can't
552 possibly change to the current thread's state. 561 possibly change to the current thread's state.
553 * If equal, then the current thread holds the lock, so the value can't 562 * If equal, then the current thread holds the lock, so the value can't
554 change until we yield the lock. 563 change until we yield the lock.
555 */ 564 */
556 static int 565 static int
557 PyThreadState_IsCurrent(PyThreadState *tstate) 566 PyThreadState_IsCurrent(PyThreadState *tstate)
558 { 567 {
559 /* Must be the tstate for this thread */ 568 /* Must be the tstate for this thread */
560 assert(PyGILState_GetThisThreadState()==tstate);
561 return tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current); 569 return tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current);
562 } 570 }
563 571
564 /* Internal initialization/finalization functions called by 572 /* Internal initialization/finalization functions called by
565 Py_Initialize/Py_Finalize 573 Py_Initialize/Py_Finalize
566 */ 574 */
567 void 575 void
568 _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) 576 _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
569 { 577 {
570 assert(i && t); /* must init with valid states */ 578 assert(i && t); /* must init with valid states */
571 autoTLSkey = PyThread_create_key(); 579 main_interp = i;
572 if (autoTLSkey == -1) 580 assert(t->gilstate_counter == 1);
573 Py_FatalError("Could not allocate TLS entry");
574 autoInterpreterState = i;
575 assert(PyThread_get_key_value(autoTLSkey) == NULL);
576 assert(t->gilstate_counter == 0);
577 581
578 _PyGILState_NoteThreadState(t); 582 _PyGILState_NoteThreadState(i, t);
579 } 583 }
580 584
581 void 585 void
582 _PyGILState_Fini(void) 586 _PyGILState_Fini(void)
583 { 587 {
584 PyThread_delete_key(autoTLSkey); 588 main_interp = NULL;
585 autoInterpreterState = NULL;
586 } 589 }
587 590
588 /* When a thread state is created for a thread by some mechanism other than 591 /* When a thread state is created for a thread by some mechanism other than
589 PyGILState_Ensure, it's important that the GILState machinery knows about 592 PyGILState_Ensure, it's important that the GILState machinery knows about
590 it so it doesn't try to create another thread state for the thread (this is 593 it so it doesn't try to create another thread state for the thread (this is
591 a better fix for SF bug #1010677 than the first one attempted). 594 a better fix for SF bug #1010677 than the first one attempted).
592 */ 595 */
593 static void 596 static void
594 _PyGILState_NoteThreadState(PyThreadState* tstate) 597 _PyGILState_NoteThreadState(PyInterpreterState *interp, PyThreadState* tstate)
595 { 598 {
596 /* If autoTLSkey isn't initialized, this must be the very first
597 threadstate created in Py_Initialize(). Don't do anything for now
598 (we'll be back here when _PyGILState_Init is called). */
599 if (!autoInterpreterState)
600 return;
601
602 /* Stick the thread state for this thread in thread local storage. 599 /* Stick the thread state for this thread in thread local storage.
603 600
604 The only situation where you can legitimately have more than one 601 The only situation where you can legitimately have more than one
605 thread state for an OS level thread is when there are multiple 602 thread state for an OS level thread is when there are multiple
606 interpreters, when: 603 interpreters, when:
607 604
608 a) You shouldn't really be using the PyGILState_ APIs anyway, 605 a) You shouldn't really be using the PyGILState_ APIs anyway,
609 and: 606 and:
610 607
611 b) The slightly odd way PyThread_set_key_value works (see 608 b) The slightly odd way PyThread_set_key_value works (see
612 comments by its implementation) means that the first thread 609 comments by its implementation) means that the first thread
613 state created for that given OS level thread will "win", 610 state created for that given OS level thread will "win",
614 which seems reasonable behaviour. 611 which seems reasonable behaviour.
615 */ 612 */
616 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) 613 if (PyThread_set_key_value(interp->auto_tls_key, (void *)tstate) < 0)
617 Py_FatalError("Couldn't create autoTLSkey mapping"); 614 Py_FatalError("Couldn't create auto_tls_key mapping");
618 615
619 /* PyGILState_Release must not try to delete this thread state. */ 616 /* PyGILState_Release must not try to delete this thread state. */
620 tstate->gilstate_counter = 1; 617 tstate->gilstate_counter = 1;
621 } 618 }
622 619
623 /* The public functions */ 620 /* The public functions */
624 PyThreadState * 621 PyThreadState *
622 PyGILState_GetThisThreadStateEx(PyInterpreterState *interp)
623 {
624 return (PyThreadState *)PyThread_get_key_value(interp->auto_tls_key);
625 }
626
627 PyThreadState *
625 PyGILState_GetThisThreadState(void) 628 PyGILState_GetThisThreadState(void)
626 { 629 {
627 if (autoInterpreterState == NULL) 630 if (main_interp == NULL)
628 return NULL; 631 return NULL;
629 return (PyThreadState *)PyThread_get_key_value(autoTLSkey); 632 return (PyThreadState *)PyThread_get_key_value(main_interp->auto_tls_key);
630 } 633 }
631 634
632 PyGILState_STATE 635 PyGILState_STATE
633 PyGILState_Ensure(void) 636 PyGILState_EnsureEx(PyInterpreterState *interp)
634 { 637 {
635 int current; 638 int current;
636 PyThreadState *tcur; 639 PyThreadState *tcur;
637 /* Note that we do not auto-init Python here - apart from 640 /* Note that we do not auto-init Python here - apart from
638 potential races with 2 threads auto-initializing, pep-311 641 potential races with 2 threads auto-initializing, pep-311
639 spells out other issues. Embedders are expected to have 642 spells out other issues. Embedders are expected to have
640 called Py_Initialize() and usually PyEval_InitThreads(). 643 called Py_Initialize() and usually PyEval_InitThreads().
641 */ 644 */
642 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ 645 assert(interp);
643 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey); 646 tcur = (PyThreadState *)PyThread_get_key_value(interp->auto_tls_key);
644 if (tcur == NULL) { 647 if (tcur == NULL) {
645 /* Create a new thread state for this thread */ 648 /* Create a new thread state for this thread */
646 tcur = PyThreadState_New(autoInterpreterState); 649 tcur = PyThreadState_New(interp);
647 if (tcur == NULL) 650 if (tcur == NULL)
648 Py_FatalError("Couldn't create thread-state for new thread"); 651 Py_FatalError("Couldn't create thread-state for new thread");
649 /* This is our thread state! We'll need to delete it in the 652 /* This is our thread state! We'll need to delete it in the
650 matching call to PyGILState_Release(). */ 653 matching call to PyGILState_Release(). */
651 tcur->gilstate_counter = 0; 654 tcur->gilstate_counter = 0;
652 current = 0; /* new thread state is never current */ 655 current = 0; /* new thread state is never current */
653 } 656 }
654 else 657 else
655 current = PyThreadState_IsCurrent(tcur); 658 current = PyThreadState_IsCurrent(tcur);
656 if (current == 0) 659 if (current == 0)
657 PyEval_RestoreThread(tcur); 660 PyEval_RestoreThread(tcur);
658 /* Update our counter in the thread-state - no need for locks: 661 /* Update our counter in the thread-state - no need for locks:
659 - tcur will remain valid as we hold the GIL. 662 - tcur will remain valid as we hold the GIL.
660 - the counter is safe as we are the only thread "allowed" 663 - the counter is safe as we are the only thread "allowed"
661 to modify this value 664 to modify this value
662 */ 665 */
663 ++tcur->gilstate_counter; 666 ++tcur->gilstate_counter;
664 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; 667 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
665 } 668 }
666 669
670 PyGILState_STATE
671 PyGILState_Ensure()
672 {
673 return PyGILState_EnsureEx(main_interp);
674 }
675
667 void 676 void
668 PyGILState_Release(PyGILState_STATE oldstate) 677 PyGILState_ReleaseEx(PyInterpreterState *interp, PyGILState_STATE oldstate)
669 { 678 {
670 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value( 679 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
671 autoTLSkey); 680 interp->auto_tls_key);
672 if (tcur == NULL) 681 if (tcur == NULL)
673 Py_FatalError("auto-releasing thread-state, " 682 Py_FatalError("auto-releasing thread-state, "
674 "but no thread-state for this thread"); 683 "but no thread-state for this thread");
675 /* We must hold the GIL and have our thread state current */ 684 /* We must hold the GIL and have our thread state current */
676 /* XXX - remove the check - the assert should be fine, 685 /* XXX - remove the check - the assert should be fine,
677 but while this is very new (April 2003), the extra check 686 but while this is very new (April 2003), the extra check
678 by release-only users can't hurt. 687 by release-only users can't hurt.
679 */ 688 */
680 if (! PyThreadState_IsCurrent(tcur)) 689 if (! PyThreadState_IsCurrent(tcur))
681 Py_FatalError("This thread state must be current when releasing"); 690 Py_FatalError("This thread state must be current when releasing");
682 assert(PyThreadState_IsCurrent(tcur));
683 --tcur->gilstate_counter; 691 --tcur->gilstate_counter;
684 assert(tcur->gilstate_counter >= 0); /* illegal counter value */ 692 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
685 693
686 /* If we're going to destroy this thread-state, we must 694 /* If we're going to destroy this thread-state, we must
687 * clear it while the GIL is held, as destructors may run. 695 * clear it while the GIL is held, as destructors may run.
688 */ 696 */
689 if (tcur->gilstate_counter == 0) { 697 if (tcur->gilstate_counter == 0) {
690 /* can't have been locked when we created it */ 698 /* can't have been locked when we created it */
691 assert(oldstate == PyGILState_UNLOCKED); 699 assert(oldstate == PyGILState_UNLOCKED);
692 PyThreadState_Clear(tcur); 700 PyThreadState_Clear(tcur);
693 /* Delete the thread-state. Note this releases the GIL too! 701 /* Delete the thread-state. Note this releases the GIL too!
694 * It's vital that the GIL be held here, to avoid shutdown 702 * It's vital that the GIL be held here, to avoid shutdown
695 * races; see bugs 225673 and 1061968 (that nasty bug has a 703 * races; see bugs 225673 and 1061968 (that nasty bug has a
696 * habit of coming back). 704 * habit of coming back).
697 */ 705 */
698 PyThreadState_DeleteCurrent(); 706 PyThreadState_DeleteCurrent();
699 } 707 }
700 /* Release the lock if necessary */ 708 /* Release the lock if necessary */
701 else if (oldstate == PyGILState_UNLOCKED) 709 else if (oldstate == PyGILState_UNLOCKED)
702 PyEval_SaveThread(); 710 PyEval_SaveThread();
711 }
712
713 void
714 PyGILState_Release(PyGILState_STATE oldstate)
715 {
716 PyGILState_ReleaseEx(main_interp, oldstate);
703 } 717 }
704 718
705 #ifdef __cplusplus 719 #ifdef __cplusplus
706 } 720 }
707 #endif 721 #endif
708 722
709 #endif /* WITH_THREAD */ 723 #endif /* WITH_THREAD */
710 724
711 725
OLDNEW
« no previous file with comments | « Modules/_threadmodule.c ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+