Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
546b934
Factor out add_threadstate().
ericsnowcurrently Mar 21, 2023
0bcd136
Add _PyThreadState_InitDetached().
ericsnowcurrently Mar 21, 2023
35d5310
Add _PyThreadState_ClearDetached().
ericsnowcurrently Mar 21, 2023
cba9e34
Add _PyRuntime.cached_objects.main_tstate.
ericsnowcurrently Mar 21, 2023
3b1bb8b
Add _Py_AcquireGlobalObjectsState() and _Py_ReleaseGlobalObjectsState().
ericsnowcurrently Mar 22, 2023
eb42aa1
Add _Py_AddToGlobalDict().
ericsnowcurrently Mar 22, 2023
4e9da2d
Drop _Py_AcquireGlobalObjectsState() and _Py_ReleaseGlobalObjectsStat…
ericsnowcurrently Mar 22, 2023
6216207
Add acquire_global_objects_lock() and release_global_objects_lock().
ericsnowcurrently Mar 22, 2023
3c007c0
Add some TODO comments.
ericsnowcurrently Mar 13, 2023
7d95514
Factor out store_interned().
ericsnowcurrently Mar 20, 2023
5c20b84
Store a thread state to use just for interned strings.
ericsnowcurrently Mar 20, 2023
a3ae02a
Always use the main interpreter when possibly resizing the interned d…
ericsnowcurrently Mar 20, 2023
d5fbc37
Use _PyRuntime.cached_objects.main_tstate instead.
ericsnowcurrently Mar 21, 2023
459325f
Add _PyThreadState_IsBound() and _PyThreadState_Unbind().
ericsnowcurrently Mar 21, 2023
4f25244
Make sure the one-off tstate is bound before using it.
ericsnowcurrently Mar 21, 2023
e68535a
Use _Py_AcquireGlobalObjectsState() in store_interned().
ericsnowcurrently Mar 22, 2023
22753b3
Use _Py_AddToGlobalDict().
ericsnowcurrently Mar 22, 2023
5c7bfd7
Move the extensions dict to _PyRuntime.cached_objects.
ericsnowcurrently Mar 22, 2023
f1a33ce
Add _Py_GetFromGlobalDict().
ericsnowcurrently Mar 22, 2023
10e6d69
Add _Py_PopFromGlobalDict().
ericsnowcurrently Mar 22, 2023
7dbae68
Adjust _Py_AddToGlobalDict().
ericsnowcurrently Mar 22, 2023
3e08c1f
Use _Py_PopFromGlobalDict() in unicode_dealloc().
ericsnowcurrently Mar 22, 2023
1039a60
Use the global dict API in import.c.
ericsnowcurrently Mar 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Drop _Py_AcquireGlobalObjectsState() and _Py_ReleaseGlobalObjectsStat…
…e().
  • Loading branch information
ericsnowcurrently committed Mar 22, 2023
commit 4e9da2da68655c6fdb106ef098bdf9510484ca97
3 changes: 0 additions & 3 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
extern void _PyThreadState_InitDetached(PyThreadState *, PyInterpreterState *);
extern void _PyThreadState_ClearDetached(PyThreadState *);

extern PyThreadState * _Py_AcquireGlobalObjectsState(PyInterpreterState *);
extern void _Py_ReleaseGlobalObjectsState(PyThreadState *);

extern PyObject * _Py_AddToGlobalDict(PyObject *, PyObject *, PyObject *);


Expand Down
66 changes: 32 additions & 34 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,9 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
// global objects
//---------------

/* The global objects thread state is meant to be used in a very limited
way and should not be used to actually run any Python code. */

static PyThreadState *
bind_global_objects_state(_PyRuntimeState *runtime)
{
Expand Down Expand Up @@ -603,53 +606,42 @@ unbind_global_objects_state(_PyRuntimeState *runtime)
#endif
}

PyThreadState *
_Py_AcquireGlobalObjectsState(PyInterpreterState *interp)
PyObject *
_Py_AddToGlobalDict(PyObject *dict, PyObject *key, PyObject *value)
{
assert(dict != NULL);
assert(PyDict_CheckExact(dict));

/* All global objects are stored in _PyRuntime
and owned by the main interpreter. */
_PyRuntimeState *runtime = &_PyRuntime;
assert(interp != NULL);
assert(interp->runtime == runtime);
PyThreadState *curts = current_fast_get(runtime);
PyInterpreterState *interp = curts->interp;
assert(interp != NULL); // The GIL must be held.

/* Due to interpreter isolation we must hold a global lock,
starting at this point and ending before we return.
Note that the operations in this function are very fucused
and we should not expect any reentrancy. */
// For now we rely on the GIL.

/* Swap to the main interpreter, if necessary. */
PyThreadState *oldts = NULL;
/* All global objects are owned by the main interpreter. */
if (!_Py_IsMainInterpreter(interp)) {
PyThreadState *main_tstate = bind_global_objects_state(runtime);

oldts = _PyThreadState_Swap(runtime, main_tstate);
assert(oldts != NULL);

unbind_global_objects_state(runtime);
}

return oldts;
}

void
_Py_ReleaseGlobalObjectsState(PyThreadState *oldts)
{
if (oldts != NULL) {
/* The thread state was swapped in _Py_AcquireGlobalObjectsState(),
because the main interpreter wasn't running in the OS thread.. */
_PyRuntimeState *runtime = &_PyRuntime;
assert(oldts->interp->runtime == runtime);
assert(!_Py_IsMainInterpreter(oldts->interp));

// The returned tstate should be _PyRuntime.cached_objects.main_tstate.
_PyThreadState_Swap(runtime, oldts);
/* The limitations of the global objects thread state apply
from this point to the point we swap back to oldts. */
}
}

PyObject *
_Py_AddToGlobalDict(PyObject *dict, PyObject *key, PyObject *value)
{
assert(dict != NULL);

/* Swap to the main interpreter, if necessary. */
PyInterpreterState *interp = _PyInterpreterState_GET();
PyThreadState *oldts = _Py_AcquireGlobalObjectsState(interp);

/* This might trigger a resize, which is why we must "acquire"
the global object state. */
the global object state. Also note that PyDict_SetDefault()
must be compatible with our reentrancy and global objects state
constraints. */
PyObject *actual = PyDict_SetDefault(dict, key, value);
if (actual == NULL) {
/* Raising an exception from one interpreter in another
Expand All @@ -661,9 +653,15 @@ _Py_AddToGlobalDict(PyObject *dict, PyObject *key, PyObject *value)

/* Swap back, it it wasn't in the main interpreter already. */
if (oldts != NULL) {
_Py_ReleaseGlobalObjectsState(oldts);
// The returned tstate should be _PyRuntime.cached_objects.main_tstate.
_PyThreadState_Swap(runtime, oldts);

unbind_global_objects_state(runtime);
}

// This is where we would release the global lock,
// if we weren't relying on the GIL.

// XXX Immortalize the key and value.

return actual;
Expand Down