-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
gh-137514: Add a free-threading wrapper for mutexes #137515
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
Changes from all commits
8390e48
1eec109
ffdd0c6
fb77bb3
ef59495
513d011
c725383
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,14 +114,6 @@ NOTE: In the interpreter's initialization phase, some globals are currently | |
# define _PyUnicode_CHECK(op) PyUnicode_Check(op) | ||
#endif | ||
|
||
#ifdef Py_GIL_DISABLED | ||
# define LOCK_INTERNED(interp) PyMutex_Lock(&_Py_INTERP_CACHED_OBJECT(interp, interned_mutex)) | ||
# define UNLOCK_INTERNED(interp) PyMutex_Unlock(&_Py_INTERP_CACHED_OBJECT(interp, interned_mutex)) | ||
#else | ||
# define LOCK_INTERNED(interp) | ||
# define UNLOCK_INTERNED(interp) | ||
#endif | ||
|
||
static inline char* _PyUnicode_UTF8(PyObject *op) | ||
{ | ||
return FT_ATOMIC_LOAD_PTR_ACQUIRE(_PyCompactUnicodeObject_CAST(op)->utf8); | ||
|
@@ -15992,14 +15984,16 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */, | |
/* Do a setdefault on the per-interpreter cache. */ | ||
PyObject *interned = get_interned_dict(interp); | ||
assert(interned != NULL); | ||
|
||
LOCK_INTERNED(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. I believe it is worth to keep the old interface here. And implement LOCK_INTERNED via FT_MUTEX_LOCK. 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. Why? 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. It is shorter, easier to read and less error-prone on my sight. 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. Hm, instead of adding another locking macro, how about an 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. Looks like a good idea! |
||
#ifdef Py_GIL_DISABLED | ||
# define INTERN_MUTEX &_Py_INTERP_CACHED_OBJECT(interp, interned_mutex) | ||
#endif | ||
FT_MUTEX_LOCK(INTERN_MUTEX); | ||
PyObject *t; | ||
{ | ||
int res = PyDict_SetDefaultRef(interned, s, s, &t); | ||
if (res < 0) { | ||
PyErr_Clear(); | ||
UNLOCK_INTERNED(interp); | ||
FT_MUTEX_UNLOCK(INTERN_MUTEX); | ||
return s; | ||
} | ||
else if (res == 1) { | ||
|
@@ -16009,7 +16003,7 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */, | |
PyUnicode_CHECK_INTERNED(t) == SSTATE_INTERNED_MORTAL) { | ||
immortalize_interned(t); | ||
} | ||
UNLOCK_INTERNED(interp); | ||
FT_MUTEX_UNLOCK(INTERN_MUTEX); | ||
return t; | ||
} | ||
else { | ||
|
@@ -16042,7 +16036,7 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */, | |
immortalize_interned(s); | ||
} | ||
|
||
UNLOCK_INTERNED(interp); | ||
FT_MUTEX_UNLOCK(INTERN_MUTEX); | ||
return s; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,6 @@ typedef struct _PyLegacyEventHandler { | |
|
||
#define _PyLegacyEventHandler_CAST(op) ((_PyLegacyEventHandler *)(op)) | ||
|
||
#ifdef Py_GIL_DISABLED | ||
#define LOCK_SETUP() PyMutex_Lock(&_PyRuntime.ceval.sys_trace_profile_mutex); | ||
#define UNLOCK_SETUP() PyMutex_Unlock(&_PyRuntime.ceval.sys_trace_profile_mutex); | ||
#else | ||
#define LOCK_SETUP() | ||
#define UNLOCK_SETUP() | ||
#endif | ||
/* The Py_tracefunc function expects the following arguments: | ||
* obj: the trace object (PyObject *) | ||
* frame: the current frame (PyFrameObject *) | ||
|
@@ -509,9 +502,9 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) | |
|
||
// needs to be decref'd outside of the lock | ||
PyObject *old_profileobj; | ||
LOCK_SETUP(); | ||
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. Same here. I think it is better to keep the old one. 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 think this one is fine. We already used |
||
FT_MUTEX_LOCK(&_PyRuntime.ceval.sys_trace_profile_mutex); | ||
Py_ssize_t profiling_threads = setup_profile(tstate, func, arg, &old_profileobj); | ||
UNLOCK_SETUP(); | ||
FT_MUTEX_UNLOCK(&_PyRuntime.ceval.sys_trace_profile_mutex); | ||
Py_XDECREF(old_profileobj); | ||
|
||
uint32_t events = 0; | ||
|
@@ -605,10 +598,10 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) | |
} | ||
// needs to be decref'd outside of the lock | ||
PyObject *old_traceobj; | ||
LOCK_SETUP(); | ||
FT_MUTEX_LOCK(&_PyRuntime.ceval.sys_trace_profile_mutex); | ||
assert(tstate->interp->sys_tracing_threads >= 0); | ||
Py_ssize_t tracing_threads = setup_tracing(tstate, func, arg, &old_traceobj); | ||
UNLOCK_SETUP(); | ||
FT_MUTEX_UNLOCK(&_PyRuntime.ceval.sys_trace_profile_mutex); | ||
Py_XDECREF(old_traceobj); | ||
if (tracing_threads < 0) { | ||
return -1; | ||
|
Uh oh!
There was an error while loading. Please reload this page.