Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5890bc0

Browse files
Drop the module's global state.
1 parent 412c59c commit 5890bc0

1 file changed

Lines changed: 0 additions & 179 deletions

File tree

Modules/_xxsubinterpretersmodule.c

Lines changed: 0 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -489,173 +489,6 @@ _run_in_interpreter(PyInterpreterState *interp,
489489
}
490490

491491

492-
/* "owned" interpreters *****************************************************/
493-
494-
struct owned_id {
495-
int64_t id;
496-
struct owned_id *next;
497-
};
498-
499-
typedef struct {
500-
PyMutex mutex;
501-
struct owned_id *head;
502-
Py_ssize_t count;
503-
} owned_ids;
504-
505-
static int
506-
init_owned(owned_ids *owned)
507-
{
508-
return 0;
509-
}
510-
511-
static void
512-
clear_owned(owned_ids *owned)
513-
{
514-
PyMutex_Lock(&owned->mutex);
515-
struct owned_id *cur = owned->head;
516-
Py_ssize_t count = 0;
517-
while (cur != NULL) {
518-
struct owned_id *next = cur->next;
519-
PyMem_RawFree(cur);
520-
cur = next;
521-
count += 1;
522-
}
523-
assert(count == owned->count);
524-
owned->head = NULL;
525-
owned->count = 0;
526-
PyMutex_Unlock(&owned->mutex);
527-
}
528-
529-
static struct owned_id *
530-
_find_owned(owned_ids *owned, int64_t interpid, struct owned_id **p_prev)
531-
{
532-
// The caller must manage the lock.
533-
if (owned->head == NULL) {
534-
return NULL;
535-
}
536-
537-
struct owned_id *prev = NULL;
538-
struct owned_id *cur = owned->head;
539-
while (cur != NULL) {
540-
if (cur->id == interpid) {
541-
break;
542-
}
543-
prev = cur;
544-
cur = cur->next;
545-
}
546-
if (cur == NULL) {
547-
return NULL;
548-
}
549-
550-
if (p_prev != NULL) {
551-
*p_prev = prev;
552-
}
553-
return cur;
554-
}
555-
556-
static void
557-
add_owned(owned_ids *owned, PyInterpreterState *interp)
558-
{
559-
PyMutex_Lock(&owned->mutex);
560-
int64_t id = PyInterpreterState_GetID(interp);
561-
struct owned_id *new = PyMem_RawMalloc(sizeof(struct owned_id));
562-
assert(new != NULL);
563-
new->id = id;
564-
new->next = owned->head;
565-
owned->head = new;
566-
owned->count += 1;
567-
PyMutex_Unlock(&owned->mutex);
568-
}
569-
570-
static void
571-
drop_owned(owned_ids *owned, int64_t interpid)
572-
{
573-
PyMutex_Lock(&owned->mutex);
574-
575-
struct owned_id *prev;
576-
struct owned_id *found = _find_owned(owned, interpid, &prev);
577-
if (found != NULL) {
578-
if (prev == NULL) {
579-
assert(found == owned->head);
580-
owned->head = found->next;
581-
}
582-
else {
583-
assert(found != owned->head);
584-
prev->next = found->next;
585-
}
586-
PyMem_RawFree(found);
587-
owned->count -= 1;
588-
}
589-
590-
PyMutex_Unlock(&owned->mutex);
591-
}
592-
593-
static int
594-
is_owned(owned_ids *owned, PyInterpreterState *interp)
595-
{
596-
int64_t interpid = PyInterpreterState_GetID(interp);
597-
PyMutex_Lock(&owned->mutex);
598-
599-
struct owned_id *found = _find_owned(owned, interpid, NULL);
600-
int res = found != NULL;
601-
602-
PyMutex_Unlock(&owned->mutex);
603-
return res;
604-
}
605-
606-
607-
/* global state *************************************************************/
608-
609-
/* globals is the process-global state for the module. It holds all
610-
the data that we need to share between interpreters, so it cannot
611-
hold PyObject values. */
612-
static struct globals {
613-
PyMutex mutex;
614-
int module_count;
615-
owned_ids owned;
616-
} _globals = {0};
617-
618-
static int
619-
_globals_init(void)
620-
{
621-
int res = -1;
622-
PyMutex_Lock(&_globals.mutex);
623-
624-
_globals.module_count++;
625-
if (_globals.module_count > 1) {
626-
// Already initialized.
627-
res = 0;
628-
goto finally;
629-
}
630-
631-
if (init_owned(&_globals.owned) < 0) {
632-
goto finally;
633-
}
634-
635-
res = 0;
636-
637-
finally:
638-
PyMutex_Unlock(&_globals.mutex);
639-
return res;
640-
}
641-
642-
static void
643-
_globals_fini(void)
644-
{
645-
PyMutex_Lock(&_globals.mutex);
646-
647-
_globals.module_count--;
648-
if (_globals.module_count > 0) {
649-
goto finally;
650-
}
651-
652-
clear_owned(&_globals.owned);
653-
654-
finally:
655-
PyMutex_Unlock(&_globals.mutex);
656-
}
657-
658-
659492
/* module level code ********************************************************/
660493

661494
static long
@@ -793,8 +626,6 @@ interp_create(PyObject *self, PyObject *args, PyObject *kwds)
793626
return NULL;
794627
}
795628

796-
add_owned(&_globals.owned, interp);
797-
798629
if (reqrefs) {
799630
// Decref to 0 will destroy the interpreter.
800631
_PyInterpreterState_RequireIDRef(interp, 1);
@@ -837,7 +668,6 @@ interp_destroy(PyObject *self, PyObject *args, PyObject *kwds)
837668
if (interp == NULL) {
838669
return NULL;
839670
}
840-
int64_t interpid = PyInterpreterState_GetID(interp);
841671

842672
// Ensure we don't try to destroy the current interpreter.
843673
PyInterpreterState *current = _get_current_interp();
@@ -861,8 +691,6 @@ interp_destroy(PyObject *self, PyObject *args, PyObject *kwds)
861691
// Destroy the interpreter.
862692
_PyXI_EndInterpreter(interp, NULL, NULL);
863693

864-
drop_owned(&_globals.owned, interpid);
865-
866694
Py_RETURN_NONE;
867695
}
868696

@@ -1612,10 +1440,6 @@ module_exec(PyObject *mod)
16121440
PyInterpreterState *interp = PyInterpreterState_Get();
16131441
module_state *state = get_module_state(mod);
16141442

1615-
if (_globals_init() != 0) {
1616-
return -1;
1617-
}
1618-
16191443
#define ADD_WHENCE(NAME) \
16201444
if (PyModule_AddIntConstant(mod, "WHENCE_" #NAME, \
16211445
_PyInterpreterState_WHENCE_##NAME) < 0) \
@@ -1650,7 +1474,6 @@ module_exec(PyObject *mod)
16501474
return 0;
16511475

16521476
error:
1653-
_globals_fini();
16541477
return -1;
16551478
}
16561479

@@ -1684,8 +1507,6 @@ module_free(void *mod)
16841507
module_state *state = get_module_state(mod);
16851508
assert(state != NULL);
16861509
clear_module_state(state);
1687-
1688-
_globals_fini();
16891510
}
16901511

16911512
static struct PyModuleDef moduledef = {

0 commit comments

Comments
 (0)