| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include "Python.h" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifdef HAVE_DLOPEN |
| 17 |
#ifdef HAVE_DLFCN_H |
| 18 |
#include <dlfcn.h> |
| 19 |
#endif |
| 20 |
#ifndef RTLD_LAZY |
| 21 |
#define RTLD_LAZY 1 |
| 22 |
#endif |
| 23 |
#endif |
| 24 |
|
| 25 |
#ifdef __cplusplus |
| 26 |
extern "C" { |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifdef WITH_THREAD |
| 30 |
#include "pythread.h" |
| 31 |
static PyThread_type_lock head_mutex = NULL; |
| 32 |
#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock())) |
| 33 |
#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) |
| 34 |
#define HEAD_UNLOCK() PyThread_release_lock(head_mutex) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
static PyInterpreterState *autoInterpreterState = NULL; |
| 40 |
static int autoTLSkey = 0; |
| 41 |
#else |
| 42 |
#define HEAD_INIT() |
| 43 |
#define HEAD_LOCK() |
| 44 |
#define HEAD_UNLOCK() |
| 45 |
#endif |
| 46 |
|
| 47 |
static PyInterpreterState *interp_head = NULL; |
| 48 |
|
| 49 |
PyThreadState *_PyThreadState_Current = NULL; |
| 50 |
PyThreadFrameGetter _PyThreadState_GetFrame = NULL; |
| 51 |
|
| 52 |
#ifdef WITH_THREAD |
| 53 |
static void _PyGILState_NoteThreadState(PyThreadState* tstate); |
| 54 |
#endif |
| 55 |
|
| 56 |
|
| 57 |
PyInterpreterState * |
| 58 |
PyInterpreterState_New(void) |
| 59 |
{ |
| 60 |
PyInterpreterState *interp = (PyInterpreterState *) |
| 61 |
malloc(sizeof(PyInterpreterState)); |
| 62 |
|
| 63 |
if (interp != NULL) { |
| 64 |
HEAD_INIT(); |
| 65 |
#ifdef WITH_THREAD |
| 66 |
if (head_mutex == NULL) |
| 67 |
Py_FatalError("Can't initialize threads for interpreter"); |
| 68 |
#endif |
| 69 |
interp->modules = NULL; |
| 70 |
interp->modules_reloading = 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; |
| 77 |
#ifdef HAVE_DLOPEN |
| 78 |
#ifdef RTLD_NOW |
| 79 |
interp->dlopenflags = RTLD_NOW; |
| 80 |
#else |
| 81 |
interp->dlopenflags = RTLD_LAZY; |
| 82 |
#endif |
| 83 |
#endif |
| 84 |
#ifdef WITH_TSC |
| 85 |
interp->tscdump = 0; |
| 86 |
#endif |
| 87 |
|
| 88 |
HEAD_LOCK(); |
| 89 |
interp->next = interp_head; |
| 90 |
interp_head = interp; |
| 91 |
HEAD_UNLOCK(); |
| 92 |
} |
| 93 |
|
| 94 |
return interp; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
void |
| 99 |
PyInterpreterState_Clear(PyInterpreterState *interp) |
| 100 |
{ |
| 101 |
PyThreadState *p; |
| 102 |
HEAD_LOCK(); |
| 103 |
for (p = interp->tstate_head; p != NULL; p = p->next) |
| 104 |
PyThreadState_Clear(p); |
| 105 |
HEAD_UNLOCK(); |
| 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->modules_reloading); |
| 111 |
Py_CLEAR(interp->sysdict); |
| 112 |
Py_CLEAR(interp->builtins); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
static void |
| 117 |
zapthreads(PyInterpreterState *interp) |
| 118 |
{ |
| 119 |
PyThreadState *p; |
| 120 |
|
| 121 |
|
| 122 |
while ((p = interp->tstate_head) != NULL) { |
| 123 |
PyThreadState_Delete(p); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
void |
| 129 |
PyInterpreterState_Delete(PyInterpreterState *interp) |
| 130 |
{ |
| 131 |
PyInterpreterState **p; |
| 132 |
zapthreads(interp); |
| 133 |
HEAD_LOCK(); |
| 134 |
for (p = &interp_head; ; p = &(*p)->next) { |
| 135 |
if (*p == NULL) |
| 136 |
Py_FatalError( |
| 137 |
"PyInterpreterState_Delete: invalid interp"); |
| 138 |
if (*p == interp) |
| 139 |
break; |
| 140 |
} |
| 141 |
if (interp->tstate_head != NULL) |
| 142 |
Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 143 |
*p = interp->next; |
| 144 |
HEAD_UNLOCK(); |
| 145 |
free(interp); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static struct _frame * |
| 151 |
threadstate_getframe(PyThreadState *self) |
| 152 |
{ |
| 153 |
return self->frame; |
| 154 |
} |
| 155 |
|
| 156 |
static PyThreadState * |
| 157 |
new_threadstate(PyInterpreterState *interp, int init) |
| 158 |
{ |
| 159 |
PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState)); |
| 160 |
|
| 161 |
if (_PyThreadState_GetFrame == NULL) |
| 162 |
_PyThreadState_GetFrame = threadstate_getframe; |
| 163 |
|
| 164 |
if (tstate != NULL) { |
| 165 |
tstate->interp = interp; |
| 166 |
|
| 167 |
tstate->frame = NULL; |
| 168 |
tstate->recursion_depth = 0; |
| 169 |
tstate->tracing = 0; |
| 170 |
tstate->use_tracing = 0; |
| 171 |
tstate->tick_counter = 0; |
| 172 |
tstate->gilstate_counter = 0; |
| 173 |
tstate->async_exc = NULL; |
| 174 |
#ifdef WITH_THREAD |
| 175 |
tstate->thread_id = PyThread_get_thread_ident(); |
| 176 |
#else |
| 177 |
tstate->thread_id = 0; |
| 178 |
#endif |
| 179 |
|
| 180 |
tstate->dict = NULL; |
| 181 |
|
| 182 |
tstate->curexc_type = NULL; |
| 183 |
tstate->curexc_value = NULL; |
| 184 |
tstate->curexc_traceback = NULL; |
| 185 |
|
| 186 |
tstate->exc_type = NULL; |
| 187 |
tstate->exc_value = NULL; |
| 188 |
tstate->exc_traceback = NULL; |
| 189 |
|
| 190 |
tstate->c_profilefunc = NULL; |
| 191 |
tstate->c_tracefunc = NULL; |
| 192 |
tstate->c_profileobj = NULL; |
| 193 |
tstate->c_traceobj = NULL; |
| 194 |
|
| 195 |
tstate->trash_delete_nesting = 0; |
| 196 |
tstate->trash_delete_later = NULL; |
| 197 |
|
| 198 |
if (init) |
| 199 |
_PyThreadState_Init(tstate); |
| 200 |
|
| 201 |
HEAD_LOCK(); |
| 202 |
tstate->next = interp->tstate_head; |
| 203 |
interp->tstate_head = tstate; |
| 204 |
HEAD_UNLOCK(); |
| 205 |
} |
| 206 |
|
| 207 |
return tstate; |
| 208 |
} |
| 209 |
|
| 210 |
PyThreadState * |
| 211 |
PyThreadState_New(PyInterpreterState *interp) |
| 212 |
{ |
| 213 |
return new_threadstate(interp, 1); |
| 214 |
} |
| 215 |
|
| 216 |
PyThreadState * |
| 217 |
_PyThreadState_Prealloc(PyInterpreterState *interp) |
| 218 |
{ |
| 219 |
return new_threadstate(interp, 0); |
| 220 |
} |
| 221 |
|
| 222 |
void |
| 223 |
_PyThreadState_Init(PyThreadState *tstate) |
| 224 |
{ |
| 225 |
#ifdef WITH_THREAD |
| 226 |
_PyGILState_NoteThreadState(tstate); |
| 227 |
#endif |
| 228 |
} |
| 229 |
|
| 230 |
void |
| 231 |
PyThreadState_Clear(PyThreadState *tstate) |
| 232 |
{ |
| 233 |
if (Py_VerboseFlag && tstate->frame != NULL) |
| 234 |
fprintf(stderr, |
| 235 |
"PyThreadState_Clear: warning: thread still has a frame\n"); |
| 236 |
|
| 237 |
Py_CLEAR(tstate->frame); |
| 238 |
|
| 239 |
Py_CLEAR(tstate->dict); |
| 240 |
Py_CLEAR(tstate->async_exc); |
| 241 |
|
| 242 |
Py_CLEAR(tstate->curexc_type); |
| 243 |
Py_CLEAR(tstate->curexc_value); |
| 244 |
Py_CLEAR(tstate->curexc_traceback); |
| 245 |
|
| 246 |
Py_CLEAR(tstate->exc_type); |
| 247 |
Py_CLEAR(tstate->exc_value); |
| 248 |
Py_CLEAR(tstate->exc_traceback); |
| 249 |
|
| 250 |
tstate->c_profilefunc = NULL; |
| 251 |
tstate->c_tracefunc = NULL; |
| 252 |
Py_CLEAR(tstate->c_profileobj); |
| 253 |
Py_CLEAR(tstate->c_traceobj); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
static void |
| 259 |
tstate_delete_common(PyThreadState *tstate) |
| 260 |
{ |
| 261 |
PyInterpreterState *interp; |
| 262 |
PyThreadState **p; |
| 263 |
PyThreadState *prev_p = NULL; |
| 264 |
if (tstate == NULL) |
| 265 |
Py_FatalError("PyThreadState_Delete: NULL tstate"); |
| 266 |
interp = tstate->interp; |
| 267 |
if (interp == NULL) |
| 268 |
Py_FatalError("PyThreadState_Delete: NULL interp"); |
| 269 |
HEAD_LOCK(); |
| 270 |
for (p = &interp->tstate_head; ; p = &(*p)->next) { |
| 271 |
if (*p == NULL) |
| 272 |
Py_FatalError( |
| 273 |
"PyThreadState_Delete: invalid tstate"); |
| 274 |
if (*p == tstate) |
| 275 |
break; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
if (*p == prev_p) |
| 281 |
Py_FatalError( |
| 282 |
"PyThreadState_Delete: small circular list(!)" |
| 283 |
" and tstate not found."); |
| 284 |
prev_p = *p; |
| 285 |
if ((*p)->next == interp->tstate_head) |
| 286 |
Py_FatalError( |
| 287 |
"PyThreadState_Delete: circular list(!) and" |
| 288 |
" tstate not found."); |
| 289 |
} |
| 290 |
*p = tstate->next; |
| 291 |
HEAD_UNLOCK(); |
| 292 |
free(tstate); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
void |
| 297 |
PyThreadState_Delete(PyThreadState *tstate) |
| 298 |
{ |
| 299 |
if (tstate == _PyThreadState_Current) |
| 300 |
Py_FatalError("PyThreadState_Delete: tstate is still current"); |
| 301 |
tstate_delete_common(tstate); |
| 302 |
#ifdef WITH_THREAD |
| 303 |
if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) |
| 304 |
PyThread_delete_key_value(autoTLSkey); |
| 305 |
#endif |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
#ifdef WITH_THREAD |
| 310 |
void |
| 311 |
PyThreadState_DeleteCurrent() |
| 312 |
{ |
| 313 |
PyThreadState *tstate = _PyThreadState_Current; |
| 314 |
if (tstate == NULL) |
| 315 |
Py_FatalError( |
| 316 |
"PyThreadState_DeleteCurrent: no current tstate"); |
| 317 |
_PyThreadState_Current = NULL; |
| 318 |
if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) |
| 319 |
PyThread_delete_key_value(autoTLSkey); |
| 320 |
tstate_delete_common(tstate); |
| 321 |
PyEval_ReleaseLock(); |
| 322 |
} |
| 323 |
#endif |
| 324 |
|
| 325 |
|
| 326 |
PyThreadState * |
| 327 |
PyThreadState_Get(void) |
| 328 |
{ |
| 329 |
if (_PyThreadState_Current == NULL) |
| 330 |
Py_FatalError("PyThreadState_Get: no current thread"); |
| 331 |
|
| 332 |
return _PyThreadState_Current; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
PyThreadState * |
| 337 |
PyThreadState_Swap(PyThreadState *newts) |
| 338 |
{ |
| 339 |
PyThreadState *oldts = _PyThreadState_Current; |
| 340 |
|
| 341 |
_PyThreadState_Current = newts; |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
#if defined(Py_DEBUG) && defined(WITH_THREAD) |
| 347 |
if (newts) { |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
int err = errno; |
| 352 |
PyThreadState *check = PyGILState_GetThisThreadState(); |
| 353 |
if (check && check->interp == newts->interp && check != newts) |
| 354 |
Py_FatalError("Invalid thread state for this thread"); |
| 355 |
errno = err; |
| 356 |
} |
| 357 |
#endif |
| 358 |
return oldts; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
PyObject * |
| 368 |
PyThreadState_GetDict(void) |
| 369 |
{ |
| 370 |
if (_PyThreadState_Current == NULL) |
| 371 |
return NULL; |
| 372 |
|
| 373 |
if (_PyThreadState_Current->dict == NULL) { |
| 374 |
PyObject *d; |
| 375 |
_PyThreadState_Current->dict = d = PyDict_New(); |
| 376 |
if (d == NULL) |
| 377 |
PyErr_Clear(); |
| 378 |
} |
| 379 |
return _PyThreadState_Current->dict; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
int |
| 392 |
PyThreadState_SetAsyncExc(long id, PyObject *exc) { |
| 393 |
PyThreadState *tstate = PyThreadState_GET(); |
| 394 |
PyInterpreterState *interp = tstate->interp; |
| 395 |
PyThreadState *p; |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
HEAD_LOCK(); |
| 404 |
for (p = interp->tstate_head; p != NULL; p = p->next) { |
| 405 |
if (p->thread_id == id) { |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
PyObject *old_exc = p->async_exc; |
| 414 |
Py_XINCREF(exc); |
| 415 |
p->async_exc = exc; |
| 416 |
HEAD_UNLOCK(); |
| 417 |
Py_XDECREF(old_exc); |
| 418 |
return 1; |
| 419 |
} |
| 420 |
} |
| 421 |
HEAD_UNLOCK(); |
| 422 |
return 0; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
PyInterpreterState * |
| 430 |
PyInterpreterState_Head(void) |
| 431 |
{ |
| 432 |
return interp_head; |
| 433 |
} |
| 434 |
|
| 435 |
PyInterpreterState * |
| 436 |
PyInterpreterState_Next(PyInterpreterState *interp) { |
| 437 |
return interp->next; |
| 438 |
} |
| 439 |
|
| 440 |
PyThreadState * |
| 441 |
PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
| 442 |
return interp->tstate_head; |
| 443 |
} |
| 444 |
|
| 445 |
PyThreadState * |
| 446 |
PyThreadState_Next(PyThreadState *tstate) { |
| 447 |
return tstate->next; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
PyObject * |
| 456 |
_PyThread_CurrentFrames(void) |
| 457 |
{ |
| 458 |
PyObject *result; |
| 459 |
PyInterpreterState *i; |
| 460 |
|
| 461 |
result = PyDict_New(); |
| 462 |
if (result == NULL) |
| 463 |
return NULL; |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
HEAD_LOCK(); |
| 472 |
for (i = interp_head; i != NULL; i = i->next) { |
| 473 |
PyThreadState *t; |
| 474 |
for (t = i->tstate_head; t != NULL; t = t->next) { |
| 475 |
PyObject *id; |
| 476 |
int stat; |
| 477 |
struct _frame *frame = t->frame; |
| 478 |
if (frame == NULL) |
| 479 |
continue; |
| 480 |
id = PyInt_FromLong(t->thread_id); |
| 481 |
if (id == NULL) |
| 482 |
goto Fail; |
| 483 |
stat = PyDict_SetItem(result, id, (PyObject *)frame); |
| 484 |
Py_DECREF(id); |
| 485 |
if (stat < 0) |
| 486 |
goto Fail; |
| 487 |
} |
| 488 |
} |
| 489 |
HEAD_UNLOCK(); |
| 490 |
return result; |
| 491 |
|
| 492 |
Fail: |
| 493 |
HEAD_UNLOCK(); |
| 494 |
Py_DECREF(result); |
| 495 |
return NULL; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
#ifdef WITH_THREAD |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
static int |
| 510 |
PyThreadState_IsCurrent(PyThreadState *tstate) |
| 511 |
{ |
| 512 |
|
| 513 |
assert(PyGILState_GetThisThreadState()==tstate); |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
return tstate == _PyThreadState_Current; |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
void |
| 524 |
_PyGILState_Init(PyInterpreterState *i, PyThreadState *t) |
| 525 |
{ |
| 526 |
assert(i && t); |
| 527 |
autoTLSkey = PyThread_create_key(); |
| 528 |
autoInterpreterState = i; |
| 529 |
assert(PyThread_get_key_value(autoTLSkey) == NULL); |
| 530 |
assert(t->gilstate_counter == 0); |
| 531 |
|
| 532 |
_PyGILState_NoteThreadState(t); |
| 533 |
} |
| 534 |
|
| 535 |
void |
| 536 |
_PyGILState_Fini(void) |
| 537 |
{ |
| 538 |
PyThread_delete_key(autoTLSkey); |
| 539 |
autoInterpreterState = NULL; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
static void |
| 548 |
_PyGILState_NoteThreadState(PyThreadState* tstate) |
| 549 |
{ |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
if (!autoInterpreterState) |
| 554 |
return; |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) |
| 571 |
Py_FatalError("Couldn't create autoTLSkey mapping"); |
| 572 |
|
| 573 |
|
| 574 |
tstate->gilstate_counter = 1; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
PyThreadState * |
| 579 |
PyGILState_GetThisThreadState(void) |
| 580 |
{ |
| 581 |
if (autoInterpreterState == NULL) |
| 582 |
return NULL; |
| 583 |
return (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
| 584 |
} |
| 585 |
|
| 586 |
PyGILState_STATE |
| 587 |
PyGILState_Ensure(void) |
| 588 |
{ |
| 589 |
int current; |
| 590 |
PyThreadState *tcur; |
| 591 |
int need_init_threads = 0; |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
assert(autoInterpreterState); |
| 599 |
tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
| 600 |
if (tcur == NULL) { |
| 601 |
need_init_threads = 1; |
| 602 |
|
| 603 |
|
| 604 |
tcur = PyThreadState_New(autoInterpreterState); |
| 605 |
if (tcur == NULL) |
| 606 |
Py_FatalError("Couldn't create thread-state for new thread"); |
| 607 |
|
| 608 |
|
| 609 |
tcur->gilstate_counter = 0; |
| 610 |
current = 0; |
| 611 |
} |
| 612 |
else { |
| 613 |
current = PyThreadState_IsCurrent(tcur); |
| 614 |
} |
| 615 |
|
| 616 |
if (current == 0) { |
| 617 |
PyEval_RestoreThread(tcur); |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
++tcur->gilstate_counter; |
| 626 |
|
| 627 |
if (need_init_threads) { |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
PyEval_InitThreads(); |
| 632 |
} |
| 633 |
|
| 634 |
return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; |
| 635 |
} |
| 636 |
|
| 637 |
void |
| 638 |
PyGILState_Release(PyGILState_STATE oldstate) |
| 639 |
{ |
| 640 |
PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value( |
| 641 |
autoTLSkey); |
| 642 |
if (tcur == NULL) |
| 643 |
Py_FatalError("auto-releasing thread-state, " |
| 644 |
"but no thread-state for this thread"); |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
if (! PyThreadState_IsCurrent(tcur)) |
| 651 |
Py_FatalError("This thread state must be current when releasing"); |
| 652 |
assert(PyThreadState_IsCurrent(tcur)); |
| 653 |
--tcur->gilstate_counter; |
| 654 |
assert(tcur->gilstate_counter >= 0); |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
if (tcur->gilstate_counter == 0) { |
| 660 |
|
| 661 |
assert(oldstate == PyGILState_UNLOCKED); |
| 662 |
PyThreadState_Clear(tcur); |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
PyThreadState_DeleteCurrent(); |
| 669 |
} |
| 670 |
|
| 671 |
else if (oldstate == PyGILState_UNLOCKED) |
| 672 |
PyEval_SaveThread(); |
| 673 |
} |
| 674 |
|
| 675 |
#endif |
| 676 |
|
| 677 |
#ifdef __cplusplus |
| 678 |
} |
| 679 |
#endif |
| 680 |
|
| 681 |
|