-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Implement PEP 788 #133110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Implement PEP 788 #133110
Changes from 1 commit
1828b9a
d0895f9
5c3ee8d
7b9ac59
0868c15
e9ea644
0ebdca4
40989de
d501f35
c5ec89c
4e1f599
3127a3f
82b5b9f
fda9886
f7723c0
62e9549
bc60630
9d8d526
54b0ce0
5955de6
92cf906
b0d0673
0a15beb
16d79de
c2bffcd
911c6b5
b84fa90
9ccf6bd
481caf5
71e1aec
d661578
4249c5d
71f2fd7
03ccb38
bca65fb
510ade1
3971408
6c4c52b
64920a8
05436f3
02bc2d7
03fa2af
b955d85
5236700
a277130
dac0c1a
ab9e3b5
79a1852
47957b8
771d7ed
0c3c1c7
531928e
08a8af6
02f93bc
d6c82bd
082fd69
61f70ae
fa961e9
ea1da77
6f19384
b702da2
40e7e68
96efc81
af6f6fd
2c52cdc
3ffe9d0
588364c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,32 +270,39 @@ PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc( | |
|
||
typedef uintptr_t PyInterpreterRef; | ||
|
||
PyAPI_FUNC(PyInterpreterRef) PyInterpreterRef_Get(void); | ||
PyAPI_FUNC(int) PyInterpreterRef_Get(PyInterpreterRef *ptr); | ||
PyAPI_FUNC(PyInterpreterRef) PyInterpreterRef_Dup(PyInterpreterRef ref); | ||
PyAPI_FUNC(int) PyInterpreterState_AsStrong(PyInterpreterState *interp, PyInterpreterRef *strong_ptr); | ||
PyAPI_FUNC(int) PyInterpreterRef_Main(PyInterpreterRef *strong_ptr); | ||
PyAPI_FUNC(void) PyInterpreterRef_Close(PyInterpreterRef ref); | ||
PyAPI_FUNC(PyInterpreterState *) PyInterpreterRef_AsInterpreter(PyInterpreterRef ref); | ||
|
||
#define PyInterpreterRef_Close(ref) do { \ | ||
PyInterpreterRef_Close(ref); \ | ||
ref = 0; \ | ||
} while (0); \ | ||
} while (0); | ||
|
||
/* Weak interpreter references */ | ||
|
||
typedef struct _interpreter_weakref { | ||
ZeroIntensity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int64_t id; | ||
Py_ssize_t refcount; | ||
} PyInterpreterWeakRef; | ||
} _PyInterpreterWeakRef; | ||
|
||
PyAPI_FUNC(PyInterpreterWeakRef) PyInterpreterWeakRef_Get(void); | ||
typedef _PyInterpreterWeakRef *PyInterpreterWeakRef; | ||
|
||
PyAPI_FUNC(int) PyInterpreterWeakRef_Get(PyInterpreterWeakRef *ptr); | ||
PyAPI_FUNC(PyInterpreterWeakRef) PyInterpreterWeakRef_Dup(PyInterpreterWeakRef wref); | ||
PyAPI_FUNC(int) PyInterpreterWeakRef_AsStrong(PyInterpreterWeakRef wref, PyInterpreterRef *strong_ptr); | ||
PyAPI_FUNC(void) PyInterpreterWeakRef_Close(PyInterpreterWeakRef wref); | ||
|
||
#define PyInterpreterWeakRef_Close(ref) do { \ | ||
PyInterpreterWeakRef_Close(ref); \ | ||
ref = 0; \ | ||
} while (0); | ||
ZeroIntensity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Exports for '_testcapi' shared extension | ||
PyAPI_FUNC(Py_ssize_t) _PyInterpreterState_Refcount(PyInterpreterState *interp); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move it to the internal C API? (Include/internal/pycore_pystate.h). Same remark for _PyInterpreterState_Incref(). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move tests to _testinternalcapi in this case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But we're not testing the internal C API, we're testing the public C API. We just use the internal C API to write those tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't add private functions (ex: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I split up the tests, so now the internal stuff is in |
||
PyAPI_FUNC(void) _PyInterpreterState_Incref(PyInterpreterState *interp); | ||
PyAPI_FUNC(int) _PyInterpreterState_Incref(PyInterpreterState *interp); | ||
|
||
PyAPI_FUNC(int) PyThreadState_Ensure(PyInterpreterRef interp_ref); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3145,12 +3145,22 @@ _PyInterpreterState_Refcount(PyInterpreterState *interp) | |
return refcount; | ||
} | ||
|
||
void | ||
int | ||
_PyInterpreterState_Incref(PyInterpreterState *interp) | ||
{ | ||
assert(interp != NULL); | ||
assert(_Py_atomic_load_ssize_relaxed(&interp->threads.finalizing.countdown) >= 0); | ||
struct _Py_finalizing_threads *finalizing = &interp->threads.finalizing; | ||
assert(_Py_atomic_load_ssize_relaxed(&finalizing->countdown) >= 0); | ||
PyMutex *mutex = &finalizing->mutex; | ||
PyMutex_Lock(mutex); | ||
if (_PyEvent_IsSet(&finalizing->finished)) { | ||
PyMutex_Unlock(mutex); | ||
return -1; | ||
} | ||
|
||
_Py_atomic_add_ssize(&interp->threads.finalizing.countdown, 1); | ||
PyMutex_Unlock(mutex); | ||
return 0; | ||
} | ||
|
||
static PyInterpreterState * | ||
|
@@ -3164,19 +3174,29 @@ ref_as_interp(PyInterpreterRef ref) | |
return interp; | ||
} | ||
|
||
PyInterpreterRef | ||
PyInterpreterRef_Get(void) | ||
int | ||
PyInterpreterRef_Get(PyInterpreterRef *ref_ptr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may rename the parameter to just "ref". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
{ | ||
assert(ref_ptr != NULL); | ||
PyInterpreterState *interp = PyInterpreterState_Get(); | ||
_PyInterpreterState_Incref(interp); | ||
return (PyInterpreterRef)interp; | ||
if (_PyInterpreterState_Incref(interp) < 0) { | ||
PyErr_SetString(PyExc_PythonFinalizationError, | ||
"Cannot acquire strong interpreter references anymore"); | ||
return -1; | ||
} | ||
*ref_ptr = (PyInterpreterRef)interp; | ||
return 0; | ||
} | ||
|
||
PyInterpreterRef | ||
PyInterpreterRef_Dup(PyInterpreterRef ref) | ||
{ | ||
PyInterpreterState *interp = ref_as_interp(ref); | ||
_PyInterpreterState_Incref(interp); | ||
int res = _PyInterpreterState_Incref(interp); | ||
(void)res; | ||
// We already hold a strong reference, so it shouldn't be possible | ||
// for the interpreter to be at a point where references don't work anymore | ||
assert(res == 0); | ||
return (PyInterpreterRef)interp; | ||
} | ||
|
||
|
@@ -3195,24 +3215,48 @@ PyInterpreterRef_AsInterpreter(PyInterpreterRef ref) | |
return interp; | ||
} | ||
|
||
PyInterpreterWeakRef | ||
PyInterpreterWeakRef_Get(void) | ||
int | ||
PyInterpreterWeakRef_Get(PyInterpreterWeakRef *wref_ptr) | ||
{ | ||
PyInterpreterState *interp = PyInterpreterState_Get(); | ||
PyInterpreterWeakRef wref = { interp->id }; | ||
_PyInterpreterWeakRef *wref = PyMem_RawMalloc(sizeof(_PyInterpreterWeakRef)); | ||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (wref == NULL) { | ||
PyErr_NoMemory(); | ||
return -1; | ||
} | ||
wref->refcount = 1; | ||
wref->id = interp->id; | ||
*wref_ptr = (PyInterpreterWeakRef)wref; | ||
return 0; | ||
} | ||
|
||
static _PyInterpreterWeakRef * | ||
wref_handle_as_ptr(PyInterpreterWeakRef wref_handle) | ||
{ | ||
_PyInterpreterWeakRef *wref = (_PyInterpreterWeakRef *)wref_handle; | ||
if (wref == NULL) { | ||
Py_FatalError("Got a null weak interpreter reference, likely due to use after close."); | ||
ZeroIntensity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return wref; | ||
} | ||
|
||
PyInterpreterWeakRef | ||
PyInterpreterWeakRef_Dup(PyInterpreterWeakRef wref) | ||
PyInterpreterWeakRef_Dup(PyInterpreterWeakRef wref_handle) | ||
{ | ||
_PyInterpreterWeakRef *wref = wref_handle_as_ptr(wref_handle); | ||
++wref->refcount; | ||
return wref; | ||
} | ||
|
||
#undef PyInterpreterWeakRef_Close | ||
void | ||
PyInterpreterWeakRef_Close(PyInterpreterWeakRef wref) | ||
PyInterpreterWeakRef_Close(PyInterpreterWeakRef wref_handle) | ||
{ | ||
return; | ||
_PyInterpreterWeakRef *wref = wref_handle_as_ptr(wref_handle); | ||
if (--wref->refcount == 0) { | ||
PyMem_RawFree(wref); | ||
} | ||
} | ||
|
||
static int | ||
|
@@ -3234,10 +3278,11 @@ try_acquire_strong_ref(PyInterpreterState *interp, PyInterpreterRef *strong_ptr) | |
} | ||
|
||
int | ||
PyInterpreterWeakRef_AsStrong(PyInterpreterWeakRef wref, PyInterpreterRef *strong_ptr) | ||
PyInterpreterWeakRef_AsStrong(PyInterpreterWeakRef wref_handle, PyInterpreterRef *strong_ptr) | ||
{ | ||
assert(strong_ptr != NULL); | ||
int64_t interp_id = wref.id; | ||
_PyInterpreterWeakRef *wref = wref_handle_as_ptr(wref_handle); | ||
int64_t interp_id = wref->id; | ||
/* Interpreters cannot be deleted while we hold the runtime lock. */ | ||
_PyRuntimeState *runtime = &_PyRuntime; | ||
HEAD_LOCK(runtime); | ||
|
@@ -3254,13 +3299,12 @@ PyInterpreterWeakRef_AsStrong(PyInterpreterWeakRef wref, PyInterpreterRef *stron | |
} | ||
|
||
int | ||
PyInterpreterState_AsStrong(PyInterpreterState *interp, PyInterpreterRef *strong_ptr) | ||
PyInterpreterRef_Main(PyInterpreterRef *strong_ptr) | ||
{ | ||
assert(interp != NULL); | ||
assert(strong_ptr != NULL); | ||
_PyRuntimeState *runtime = &_PyRuntime; | ||
HEAD_LOCK(runtime); | ||
int res = try_acquire_strong_ref(interp, strong_ptr); | ||
int res = try_acquire_strong_ref(&runtime->_main_interpreter, strong_ptr); | ||
HEAD_UNLOCK(runtime); | ||
|
||
return res; | ||
|
Uh oh!
There was an error while loading. Please reload this page.