Skip to content

gh-117657: Move static variables in fileutils.c to thread state #120559

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Move static variables in fileutils.c to thread state
  • Loading branch information
Fidget-Spinner committed Jun 15, 2024
commit 990b0504cc9650826dfc54011833d2cacd1739f9
6 changes: 6 additions & 0 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ struct _ts {
PyObject *previous_executor;

uint64_t dict_global_version;

// For Python/fileutils.c
int fileutils_ioctl_works;
int fileutils_skiproot_initialized;
int fileutils_combineex_initialized;
int fileutils__Py_open_cloexec_works;
};

#ifdef Py_DEBUG
Expand Down
43 changes: 20 additions & 23 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ extern int winerror_to_errno(int);
# include <fcntl.h> // fcntl(F_GETFD)
#endif

#ifdef O_CLOEXEC
/* Does open() support the O_CLOEXEC flag? Possible values:

-1: unknown
0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23
1: open() supports O_CLOEXEC flag, close-on-exec is set

The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO
and os.open(). */
int _Py_open_cloexec_works = -1;
#endif

// The value must be the same in unicodeobject.c.
#define MAX_UNICODE 0x10ffff

Expand Down Expand Up @@ -1455,7 +1443,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
DWORD flags;
#else
#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
static int ioctl_works = -1;
PyThreadState *tstate = PyThreadState_Get();
int request;
int err;
#endif
Expand Down Expand Up @@ -1502,7 +1490,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
#else

#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
if (ioctl_works != 0 && raise != 0) {
if (tstate->fileutils_ioctl_works != 0 && raise != 0) {
/* fast-path: ioctl() only requires one syscall */
/* caveat: raise=0 is an indicator that we must be async-signal-safe
* thus avoid using ioctl() so we skip the fast-path. */
Expand All @@ -1512,7 +1500,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
request = FIOCLEX;
err = ioctl(fd, request, NULL);
if (!err) {
ioctl_works = 1;
tstate->fileutils_ioctl_works = 1;
return 0;
}

Expand All @@ -1539,7 +1527,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
with EACCES. While FIOCLEX is safe operation it may be
unavailable because ioctl was denied altogether.
This can be the case on Android. */
ioctl_works = 0;
tstate->fileutils_ioctl_works = 0;
}
/* fallback to fcntl() if ioctl() does not work */
}
Expand Down Expand Up @@ -1626,7 +1614,16 @@ _Py_open_impl(const char *pathname, int flags, int gil_held)
#ifdef MS_WINDOWS
flags |= O_NOINHERIT;
#elif defined(O_CLOEXEC)
atomic_flag_works = &_Py_open_cloexec_works;
PyThreadState *tstate = PyThreadState_Get();
/* Does open() support the O_CLOEXEC flag? Possible values:

-1: unknown
0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23
1: open() supports O_CLOEXEC flag, close-on-exec is set

The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO
and os.open(). */
atomic_flag_works = &tstate->fileutils__Py_open_cloexec_works;
flags |= O_CLOEXEC;
#else
atomic_flag_works = NULL;
Expand Down Expand Up @@ -2236,12 +2233,12 @@ _Py_abspath(const wchar_t *path, wchar_t **abspath_p)
HRESULT
PathCchSkipRoot(const wchar_t *path, const wchar_t **rootEnd)
{
static int initialized = 0;
PyThreadState *tstate = PyThreadState_Get();
typedef HRESULT(__stdcall *PPathCchSkipRoot) (PCWSTR pszPath,
PCWSTR *ppszRootEnd);
static PPathCchSkipRoot _PathCchSkipRoot;

if (initialized == 0) {
if (tstate->fileutils_skiproot_initialized == 0) {
HMODULE pathapi = LoadLibraryExW(L"api-ms-win-core-path-l1-1-0.dll", NULL,
LOAD_LIBRARY_SEARCH_SYSTEM32);
if (pathapi) {
Expand All @@ -2251,7 +2248,7 @@ PathCchSkipRoot(const wchar_t *path, const wchar_t **rootEnd)
else {
_PathCchSkipRoot = NULL;
}
initialized = 1;
tstate->fileutils_skiproot_initialized = 1;
}

if (!_PathCchSkipRoot) {
Expand All @@ -2265,15 +2262,15 @@ static HRESULT
PathCchCombineEx(wchar_t *buffer, size_t bufsize, const wchar_t *dirname,
const wchar_t *relfile, unsigned long flags)
{
static int initialized = 0;
PyThreadState *tstate = PyThreadState_Get();
typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut,
size_t cchPathOut,
PCWSTR pszPathIn,
PCWSTR pszMore,
unsigned long dwFlags);
static PPathCchCombineEx _PathCchCombineEx;

if (initialized == 0) {
if (tstate->fileutils_combineex_initialized == 0) {
HMODULE pathapi = LoadLibraryExW(L"api-ms-win-core-path-l1-1-0.dll", NULL,
LOAD_LIBRARY_SEARCH_SYSTEM32);
if (pathapi) {
Expand All @@ -2283,7 +2280,7 @@ PathCchCombineEx(wchar_t *buffer, size_t bufsize, const wchar_t *dirname,
else {
_PathCchCombineEx = NULL;
}
initialized = 1;
tstate->fileutils_combineex_initialized = 1;
}

if (!_PathCchCombineEx) {
Expand Down
4 changes: 4 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,10 @@ init_threadstate(_PyThreadStateImpl *_tstate,
tstate->what_event = -1;
tstate->previous_executor = NULL;
tstate->dict_global_version = 0;
tstate->fileutils_ioctl_works = -1;
tstate->fileutils_skiproot_initialized = 0;
tstate->fileutils_combineex_initialized = 0;
tstate->fileutils__Py_open_cloexec_works = -1;

tstate->delete_later = NULL;

Expand Down
4 changes: 0 additions & 4 deletions Tools/c-analyzer/TODO
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ Objects/bytesobject.c:_Py_onel_strings Py_ssize_t _Py_
Objects/dictobject.c:empty_keys_struct static PyDictKeysObject empty_keys_struct


# "initialized"
Python/fileutils.c:_Py_open_cloexec_works int _Py_open_cloexec_works


# other non-object (40)
Modules/_tracemalloc.c:allocators static struct { PyMemAllocatorEx mem; PyMemAllocatorEx raw; PyMemAllocatorEx obj; } allocators
Modules/_tracemalloc.c:tables_lock static PyThread_type_lock tables_lock
Expand Down
1 change: 0 additions & 1 deletion Tools/tsan/suppressions_free_threading.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ race_top:new_reference
race_top:set_contains_key
# https://gist.github.com/colesbury/d13d033f413b4ad07929d044bed86c35
race_top:set_discard_entry
race_top:set_inheritable
race_top:_PyDict_CheckConsistency
race_top:_Py_dict_lookup_threadsafe
race_top:_multiprocessing_SemLock_acquire_impl
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy