Skip to content

Commit dc43fc4

Browse files
committed
move functions and add inline for functions used once
1 parent 407b1e9 commit dc43fc4

1 file changed

Lines changed: 51 additions & 50 deletions

File tree

Modules/_threadmodule.c

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -75,56 +75,6 @@ get_thread_state_by_cls(PyTypeObject *cls)
7575
return get_thread_state(module);
7676
}
7777

78-
/* Helper to set the thread name using platform-specific APIs */
79-
static int
80-
set_native_thread_name(const char *name)
81-
{
82-
int rc = 0;
83-
#ifdef __APPLE__
84-
rc = pthread_setname_np(name);
85-
#elif defined(__NetBSD__)
86-
pthread_t thread = pthread_self();
87-
rc = pthread_setname_np(thread, "%s", (void *)name);
88-
#elif defined(HAVE_PTHREAD_SETNAME_NP)
89-
pthread_t thread = pthread_self();
90-
rc = pthread_setname_np(thread, name);
91-
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
92-
pthread_t thread = pthread_self();
93-
pthread_set_name_np(thread, name);
94-
rc = 0; /* pthread_set_name_np() returns void */
95-
#endif
96-
return rc;
97-
}
98-
99-
/* Helper to encode and truncate thread name; returns new reference or NULL */
100-
static PyObject *
101-
encode_thread_name(PyObject *name_obj, const char *encoding)
102-
{
103-
#ifdef __sun
104-
/* Solaris always uses UTF-8 */
105-
encoding = "utf-8";
106-
#endif
107-
108-
PyObject *name_encoded = PyUnicode_AsEncodedString(name_obj, encoding, "replace");
109-
if (name_encoded == NULL) {
110-
return NULL;
111-
}
112-
113-
#ifdef _PYTHREAD_NAME_MAXLEN
114-
if (PyBytes_GET_SIZE(name_encoded) > _PYTHREAD_NAME_MAXLEN) {
115-
PyObject *truncated = PyBytes_FromStringAndSize(PyBytes_AS_STRING(name_encoded),
116-
_PYTHREAD_NAME_MAXLEN);
117-
Py_DECREF(name_encoded);
118-
if (truncated == NULL) {
119-
return NULL;
120-
}
121-
return truncated;
122-
}
123-
#endif
124-
125-
return name_encoded;
126-
}
127-
12878
#ifdef MS_WINDOWS
12979
typedef HRESULT (WINAPI *PF_GET_THREAD_DESCRIPTION)(HANDLE, PCWSTR*);
13080
typedef HRESULT (WINAPI *PF_SET_THREAD_DESCRIPTION)(HANDLE, PCWSTR);
@@ -2570,6 +2520,56 @@ of the main interpreter.");
25702520
# include <pthread_np.h>
25712521
#endif
25722522

2523+
/* Helper to set the thread name using platform-specific APIs */
2524+
static int
2525+
set_native_thread_name(const char *name)
2526+
{
2527+
int rc = 0;
2528+
#ifdef __APPLE__
2529+
rc = pthread_setname_np(name);
2530+
#elif defined(__NetBSD__)
2531+
pthread_t thread = pthread_self();
2532+
rc = pthread_setname_np(thread, "%s", (void *)name);
2533+
#elif defined(HAVE_PTHREAD_SETNAME_NP)
2534+
pthread_t thread = pthread_self();
2535+
rc = pthread_setname_np(thread, name);
2536+
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
2537+
pthread_t thread = pthread_self();
2538+
pthread_set_name_np(thread, name);
2539+
rc = 0; /* pthread_set_name_np() returns void */
2540+
#endif
2541+
return rc;
2542+
}
2543+
2544+
/* Helper to encode and truncate thread name; returns new reference or NULL */
2545+
static PyObject *
2546+
encode_thread_name(PyObject *name_obj, const char *encoding)
2547+
{
2548+
#ifdef __sun
2549+
/* Solaris always uses UTF-8 */
2550+
encoding = "utf-8";
2551+
#endif
2552+
2553+
PyObject *name_encoded = PyUnicode_AsEncodedString(name_obj, encoding, "replace");
2554+
if (name_encoded == NULL) {
2555+
return NULL;
2556+
}
2557+
2558+
#ifdef _PYTHREAD_NAME_MAXLEN
2559+
if (PyBytes_GET_SIZE(name_encoded) > _PYTHREAD_NAME_MAXLEN) {
2560+
PyObject *truncated = PyBytes_FromStringAndSize(PyBytes_AS_STRING(name_encoded),
2561+
_PYTHREAD_NAME_MAXLEN);
2562+
Py_DECREF(name_encoded);
2563+
if (truncated == NULL) {
2564+
return NULL;
2565+
}
2566+
return truncated;
2567+
}
2568+
#endif
2569+
2570+
return name_encoded;
2571+
}
2572+
25732573
#if defined(HAVE_PTHREAD_GETNAME_NP) || defined(HAVE_PTHREAD_GET_NAME_NP) || defined(MS_WINDOWS)
25742574
/*[clinic input]
25752575
_thread._get_name
@@ -2635,6 +2635,7 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
26352635
{
26362636
#ifndef MS_WINDOWS
26372637
#ifdef __sun
2638+
// Solaris always uses UTF-8
26382639
const char *encoding = "utf-8";
26392640
#else
26402641
PyInterpreterState *interp = _PyInterpreterState_GET();

0 commit comments

Comments
 (0)