2424#include "pycore_long.h" // _PyLong_IsNegative()
2525#include "pycore_moduleobject.h" // _PyModule_GetState()
2626#include "pycore_object.h" // _PyObject_LookupSpecial()
27+ #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_INT_RELAXED()
2728#include "pycore_pylifecycle.h" // _PyOS_URandom()
2829#include "pycore_pystate.h" // _PyInterpreterState_GET()
29- #include "pycore_pyatomic_ft_wrappers.h" // FT_MUTEX_LOCK()
3030#include "pycore_signal.h" // Py_NSIG
3131#include "pycore_time.h" // _PyLong_FromTime_t()
3232#include "pycore_tuple.h" // _PyTuple_FromPairSteal
@@ -16941,11 +16941,6 @@ DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
1694116941
1694216942typedef struct {
1694316943 PyObject_HEAD
16944- #ifdef Py_GIL_DISABLED
16945- // Protects scandir iterator state when a os.scandir() iterator is used
16946- // from multiple threads.
16947- PyMutex mutex;
16948- #endif
1694916944 path_t path;
1695016945#ifdef MS_WINDOWS
1695116946 HANDLE handle;
@@ -16957,6 +16952,9 @@ typedef struct {
1695716952#ifdef HAVE_FDOPENDIR
1695816953 int fd;
1695916954#endif
16955+ // Protects the iterator state when an os.scandir() iterator is used from
16956+ // multiple threads.
16957+ PyMutex mutex;
1696016958} ScandirIterator;
1696116959
1696216960#define ScandirIterator_CAST(op) ((ScandirIterator *)(op))
@@ -16966,19 +16964,19 @@ typedef struct {
1696616964static int
1696716965ScandirIterator_is_closed(ScandirIterator *iterator)
1696816966{
16969- FT_MUTEX_LOCK (&iterator->mutex);
16967+ PyMutex_Lock (&iterator->mutex);
1697016968 int closed = iterator->handle == INVALID_HANDLE_VALUE;
16971- FT_MUTEX_UNLOCK (&iterator->mutex);
16969+ PyMutex_Unlock (&iterator->mutex);
1697216970 return closed;
1697316971}
1697416972
1697516973static void
1697616974ScandirIterator_closedir(ScandirIterator *iterator)
1697716975{
16978- FT_MUTEX_LOCK (&iterator->mutex);
16976+ PyMutex_Lock (&iterator->mutex);
1697916977 HANDLE handle = iterator->handle;
1698016978 iterator->handle = INVALID_HANDLE_VALUE;
16981- FT_MUTEX_UNLOCK (&iterator->mutex);
16979+ PyMutex_Unlock (&iterator->mutex);
1698216980
1698316981 if (handle != INVALID_HANDLE_VALUE) {
1698416982 Py_BEGIN_ALLOW_THREADS
@@ -16996,7 +16994,7 @@ ScandirIterator_iternext(PyObject *op)
1699616994 DWORD error = ERROR_SUCCESS;
1699716995 int found = 0;
1699816996
16999- FT_MUTEX_LOCK (&iterator->mutex);
16997+ PyMutex_Lock (&iterator->mutex);
1700016998 /* Happens if the iterator is iterated twice, or closed explicitly */
1700116999 while (iterator->handle != INVALID_HANDLE_VALUE) {
1700217000 if (!iterator->first_time) {
@@ -17023,7 +17021,7 @@ ScandirIterator_iternext(PyObject *op)
1702317021
1702417022 /* Loop till we get a non-dot directory or finish iterating */
1702517023 }
17026- FT_MUTEX_UNLOCK (&iterator->mutex);
17024+ PyMutex_Unlock (&iterator->mutex);
1702717025
1702817026 if (found) {
1702917027 PyObject *module = PyType_GetModule(Py_TYPE(iterator));
@@ -17047,19 +17045,19 @@ ScandirIterator_iternext(PyObject *op)
1704717045static int
1704817046ScandirIterator_is_closed(ScandirIterator *iterator)
1704917047{
17050- FT_MUTEX_LOCK (&iterator->mutex);
17048+ PyMutex_Lock (&iterator->mutex);
1705117049 int closed = iterator->dirp == NULL;
17052- FT_MUTEX_UNLOCK (&iterator->mutex);
17050+ PyMutex_Unlock (&iterator->mutex);
1705317051 return closed;
1705417052}
1705517053
1705617054static void
1705717055ScandirIterator_closedir(ScandirIterator *iterator)
1705817056{
17059- FT_MUTEX_LOCK (&iterator->mutex);
17057+ PyMutex_Lock (&iterator->mutex);
1706017058 DIR *dirp = iterator->dirp;
1706117059 iterator->dirp = NULL;
17062- FT_MUTEX_UNLOCK (&iterator->mutex);
17060+ PyMutex_Unlock (&iterator->mutex);
1706317061
1706417062 if (dirp != NULL) {
1706517063 Py_BEGIN_ALLOW_THREADS
@@ -17089,7 +17087,7 @@ ScandirIterator_iternext(PyObject *op)
1708917087 unsigned char d_type = 0;
1709017088#endif
1709117089
17092- FT_MUTEX_LOCK (&iterator->mutex);
17090+ PyMutex_Lock (&iterator->mutex);
1709317091 /* Happens if the iterator is iterated twice, or closed explicitly */
1709417092 while (iterator->dirp != NULL) {
1709517093 Py_BEGIN_ALLOW_THREADS
@@ -17126,7 +17124,7 @@ ScandirIterator_iternext(PyObject *op)
1712617124
1712717125 /* Loop till we get a non-dot directory or finish iterating */
1712817126 }
17129- FT_MUTEX_UNLOCK (&iterator->mutex);
17127+ PyMutex_Unlock (&iterator->mutex);
1713017128
1713117129 if (found) {
1713217130 PyObject *module = PyType_GetModule(Py_TYPE(iterator));
@@ -17286,9 +17284,7 @@ os_scandir_impl(PyObject *module, path_t *path)
1728617284 if (!iterator)
1728717285 return NULL;
1728817286
17289- #ifdef Py_GIL_DISABLED
1729017287 iterator->mutex = (PyMutex){0};
17291- #endif
1729217288#ifdef MS_WINDOWS
1729317289 iterator->handle = INVALID_HANDLE_VALUE;
1729417290#else
0 commit comments