Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-120321: Make gi_yieldfrom thread-safe in free-threading build#144292

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
colesbury wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromcolesbury:gh-120321-yield-from
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
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
9 changes: 5 additions & 4 deletionsInclude/internal/pycore_frame.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,15 +44,16 @@ extern PyFrameObject* _PyFrame_New_NoTrack(PyCodeObject *code);
/* other API */

typedefenum_framestate {
FRAME_CREATED=-3,
FRAME_SUSPENDED=-2,
FRAME_SUSPENDED_YIELD_FROM=-1,
FRAME_CREATED=-4,
FRAME_SUSPENDED=-3,
FRAME_SUSPENDED_YIELD_FROM=-2,
FRAME_SUSPENDED_YIELD_FROM_LOCKED=-1,
FRAME_EXECUTING=0,
FRAME_COMPLETED=1,
FRAME_CLEARED=4
}PyFrameState;

#defineFRAME_STATE_SUSPENDED(S) ((S)== FRAME_SUSPENDED|| (S)== FRAME_SUSPENDED_YIELD_FROM)
#defineFRAME_STATE_SUSPENDED(S) ((S)>= FRAME_SUSPENDED&& (S)<= FRAME_SUSPENDED_YIELD_FROM_LOCKED)
#defineFRAME_STATE_FINISHED(S) ((S) >= FRAME_COMPLETED)

#ifdef__cplusplus
Expand Down
3 changes: 3 additions & 0 deletionsInclude/internal/pycore_lock.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,6 +70,9 @@ PyMutex_LockFlags(PyMutex *m, _PyLockFlags flags)
// error messages) otherwise returns 0.
externint_PyMutex_TryUnlock(PyMutex *m);

// Yield the processor to other threads (e.g., sched_yield).
externvoid_Py_yield(void);


// PyEvent is a one-time event notification
typedefstruct {
Expand Down
23 changes: 17 additions & 6 deletionsLib/test/support/threading_helper.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -250,21 +250,32 @@ def requires_working_threading(*, module=False):
returnunittest.skipUnless(can_start_thread,msg)


defrun_concurrently(worker_func,nthreads,args=(),kwargs={}):
defrun_concurrently(worker_func,nthreads=None,args=(),kwargs={}):
"""
Run the worker function concurrently in multiple threads.
Run the worker function(s) concurrently in multiple threads.
If `worker_func` is a single callable, it is used for all threads.
If it is a list of callables, each callable is used for one thread.
"""
fromcollections.abcimportIterable

ifnthreadsisNone:
nthreads=len(worker_func)
ifnotisinstance(worker_func,Iterable):
worker_func= [worker_func]*nthreads
assertlen(worker_func)==nthreads

barrier=threading.Barrier(nthreads)

defwrapper_func(*args,**kwargs):
defwrapper_func(func,*args,**kwargs):
# Wait for all threads to reach this point before proceeding.
barrier.wait()
worker_func(*args,**kwargs)
func(*args,**kwargs)

withcatch_threading_exception()ascm:
workers= [
threading.Thread(target=wrapper_func,args=args,kwargs=kwargs)
for_inrange(nthreads)
threading.Thread(target=wrapper_func,args=(func,*args),kwargs=kwargs)
forfuncinworker_func
]
withstart_threads(workers):
pass
Expand Down
37 changes: 37 additions & 0 deletionsLib/test/test_free_threading/test_generators.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
importconcurrent.futures
importitertools
importthreading
importunittest
fromthreadingimportBarrier
fromunittestimportTestCase
Expand DownExpand Up@@ -120,3 +122,38 @@ def drive_generator(g):

g=gen()
threading_helper.run_concurrently(drive_generator,self.NUM_THREADS,args=(g,))

deftest_concurrent_gi_yieldfrom(self):
defgen_yield_from():
yieldfromitertools.count()

g=gen_yield_from()
next(g)# Put in FRAME_SUSPENDED_YIELD_FROM state

defread_yieldfrom(gen):
for_inrange(10000):
self.assertIsNotNone(gen.gi_yieldfrom)

threading_helper.run_concurrently(read_yieldfrom,self.NUM_THREADS,args=(g,))

deftest_gi_yieldfrom_close_race(self):
defgen_yield_from():
yieldfromitertools.count()

g=gen_yield_from()
next(g)

done=threading.Event()

defreader():
whilenotdone.is_set():
g.gi_yieldfrom

defcloser():
try:
g.close()
exceptValueError:
pass
done.set()

threading_helper.run_concurrently([reader,closer])
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Made ``gi_yieldfrom`` thread-safe in the free-threading build
by using a lightweight lock on the frame state.
36 changes: 34 additions & 2 deletionsObjects/genobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@
#include "pycore_gc.h" // _PyGC_CLEAR_FINALIZED()
#include "pycore_genobject.h" // _PyGen_SetStopIterationValue()
#include "pycore_interpframe.h" // _PyFrame_GetCode()
#include "pycore_lock.h" // _Py_yield()
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_opcode_utils.h" // RESUME_AFTER_YIELD_FROM
Expand DownExpand Up@@ -44,6 +45,18 @@ static PyObject* async_gen_athrow_new(PyAsyncGenObject *, PyObject *);
((gen)->gi_frame_state = (state), true)
#endif

// Wait for any in-progress gi_yieldfrom read to complete.
static inline void
gen_yield_from_lock_wait(PyGenObject *gen, int8_t *frame_state)
{
#ifdef Py_GIL_DISABLED
while (*frame_state == FRAME_SUSPENDED_YIELD_FROM_LOCKED) {
_Py_yield();
*frame_state = _Py_atomic_load_int8_relaxed(&gen->gi_frame_state);
}
#endif
}


static const char *NON_INIT_CORO_MSG = "can't send non-None value to a "
"just-started coroutine";
Expand DownExpand Up@@ -318,6 +331,8 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, PyObject **presult)
*presult = NULL;
int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
do {
gen_yield_from_lock_wait(gen, &frame_state);

if (frame_state == FRAME_CREATED && arg && arg != Py_None) {
const char *msg = "can't send non-None value to a "
"just-started generator";
Expand DownExpand Up@@ -452,6 +467,8 @@ gen_close(PyObject *self, PyObject *args)

int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
do {
gen_yield_from_lock_wait(gen, &frame_state);

if (frame_state == FRAME_CREATED) {
// && (1) to avoid -Wunreachable-code warning on Clang
if (!_Py_GEN_TRY_SET_FRAME_STATE(gen, frame_state, FRAME_CLEARED) && (1)) {
Expand DownExpand Up@@ -614,6 +631,8 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
{
int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
do {
gen_yield_from_lock_wait(gen, &frame_state);

if (frame_state == FRAME_EXECUTING) {
gen_raise_already_executing_error(gen);
return NULL;
Expand DownExpand Up@@ -876,12 +895,25 @@ static PyObject *
gen_getyieldfrom(PyObject *self, void *Py_UNUSED(ignored))
{
PyGenObject *gen = _PyGen_CAST(self);
int8_t frame_state = FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state);
#ifdef Py_GIL_DISABLED
int8_t frame_state = _Py_atomic_load_int8_relaxed(&gen->gi_frame_state);
do {
gen_yield_from_lock_wait(gen, &frame_state);
if (frame_state != FRAME_SUSPENDED_YIELD_FROM) {
Py_RETURN_NONE;
}
} while (!_Py_GEN_TRY_SET_FRAME_STATE(gen, frame_state, FRAME_SUSPENDED_YIELD_FROM_LOCKED));

PyObject *result = PyStackRef_AsPyObjectNew(_PyFrame_StackPeek(&gen->gi_iframe));
_Py_atomic_store_int8_release(&gen->gi_frame_state, FRAME_SUSPENDED_YIELD_FROM);
return result;
#else
int8_t frame_state = gen->gi_frame_state;
if (frame_state != FRAME_SUSPENDED_YIELD_FROM) {
Py_RETURN_NONE;
}
// TODO: still not thread-safe with free threading
return PyStackRef_AsPyObjectNew(_PyFrame_StackPeek(&gen->gi_iframe));
#endif
}


Expand Down
4 changes: 3 additions & 1 deletionPython/ceval.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3391,7 +3391,9 @@ _PyEval_GetAwaitable(PyObject *iterable, int oparg)
elseif (PyCoro_CheckExact(iter)) {
PyCoroObject*coro= (PyCoroObject*)iter;
int8_tframe_state=FT_ATOMIC_LOAD_INT8_RELAXED(coro->cr_frame_state);
if (frame_state==FRAME_SUSPENDED_YIELD_FROM) {
if (frame_state==FRAME_SUSPENDED_YIELD_FROM||
frame_state==FRAME_SUSPENDED_YIELD_FROM_LOCKED)
{
/* `iter` is a coroutine object that is being awaited. */
Py_CLEAR(iter);
_PyErr_SetString(PyThreadState_GET(),PyExc_RuntimeError,
Expand Down
5 changes: 4 additions & 1 deletionPython/ceval_macros.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -522,19 +522,22 @@ gen_try_set_executing(PyGenObject *gen)
#ifdefPy_GIL_DISABLED
if (!_PyObject_IsUniquelyReferenced((PyObject*)gen)) {
int8_tframe_state=_Py_atomic_load_int8_relaxed(&gen->gi_frame_state);
while (frame_state<FRAME_EXECUTING) {
while (frame_state<FRAME_SUSPENDED_YIELD_FROM_LOCKED) {
if (_Py_atomic_compare_exchange_int8(&gen->gi_frame_state,
&frame_state,
FRAME_EXECUTING)) {
return true;
}
}
// NB: We return false for FRAME_SUSPENDED_YIELD_FROM_LOCKED as well.
// That case is rare enough that we can just handle it in the deopt.
return false;
}
#endif
// Use faster non-atomic modifications in the GIL-enabled build and when
// the object is uniquely referenced in the free-threaded build.
if (gen->gi_frame_state<FRAME_EXECUTING) {
assert(gen->gi_frame_state!=FRAME_SUSPENDED_YIELD_FROM_LOCKED);
gen->gi_frame_state=FRAME_EXECUTING;
return true;
}
Expand Down
2 changes: 1 addition & 1 deletionPython/lock.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,7 +40,7 @@ struct mutex_entry {
int handed_off;
};

staticvoid
void
_Py_yield(void)
{
#ifdef MS_WINDOWS
Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp