Skip to content

Commit d30ace8

Browse files
committed
review comments
1 parent 4406050 commit d30ace8

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Lib/test/test_free_threading/test_threading_iter_locked.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
import unittest
23
from threading import Thread, Barrier, iter_locked
34
from test.support import threading_helper
@@ -15,6 +16,7 @@ def __iter__(self):
1516

1617
def __next__(self):
1718
a = next(self.it)
19+
time.sleep(0)
1820
b = next(self.it)
1921
return a, b
2022

Modules/_threadmodule.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ static PyType_Spec ThreadHandle_Type_spec = {
747747
typedef struct {
748748
PyObject_HEAD
749749
PyObject *it;
750+
PyMutex lock;
750751
} iter_locked_object;
751752

752753
#define iter_locked_object_CAST(op) ((iter_locked_object *)(op))
@@ -774,6 +775,7 @@ _thread_iter_locked_impl(PyTypeObject *type, PyObject *iterable)
774775
return NULL;
775776
}
776777
il->it = it;
778+
il->lock = (PyMutex){0};
777779

778780
return (PyObject *)il;
779781
}
@@ -804,14 +806,17 @@ iter_locked_next(PyObject *op)
804806
iter_locked_object *lz = iter_locked_object_CAST(op);
805807
PyObject *result = NULL;
806808

807-
Py_BEGIN_CRITICAL_SECTION(op); // lock on op or lz->it?
809+
// we cannot use Py_BEGIN_CRITICAL_SECTION as it is not available in the normal build
810+
PyMutex_Lock(&(lz->lock));
811+
808812
PyObject *it = lz->it;
809813
result = PyIter_Next(it);
810814
if (result == NULL) {
811815
/* Note: StopIteration is already cleared by PyIter_Next() */
812816
/* If PyErr_Occurred() we will also return NULL*/
813817
}
814-
Py_END_CRITICAL_SECTION();
818+
PyMutex_Unlock(&(lz->lock));
819+
815820
return result;
816821
}
817822

0 commit comments

Comments
 (0)