Skip to content

Commit 246f880

Browse files
committed
fix linker error
1 parent 3689044 commit 246f880

File tree

1 file changed

+49
-53
lines changed

1 file changed

+49
-53
lines changed

Modules/_threadmodule.c

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,55 @@ get_thread_state_by_cls(PyTypeObject *cls)
7575
return get_thread_state(module);
7676
}
7777

78-
// Declarations for thread name handling
79-
static int set_native_thread_name(const char *name);
80-
static PyObject *encode_thread_name(PyObject *name_obj, const char *encoding);
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+
}
81127

82128
#ifdef MS_WINDOWS
83129
typedef HRESULT (WINAPI *PF_GET_THREAD_DESCRIPTION)(HANDLE, PCWSTR*);
@@ -2583,56 +2629,6 @@ _thread.set_name
25832629
Set the name of the current thread.
25842630
[clinic start generated code]*/
25852631

2586-
/* Helper to set the thread name using platform-specific APIs */
2587-
static int
2588-
set_native_thread_name(const char *name)
2589-
{
2590-
int rc = 0;
2591-
#ifdef __APPLE__
2592-
rc = pthread_setname_np(name);
2593-
#elif defined(__NetBSD__)
2594-
pthread_t thread = pthread_self();
2595-
rc = pthread_setname_np(thread, "%s", (void *)name);
2596-
#elif defined(HAVE_PTHREAD_SETNAME_NP)
2597-
pthread_t thread = pthread_self();
2598-
rc = pthread_setname_np(thread, name);
2599-
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
2600-
pthread_t thread = pthread_self();
2601-
pthread_set_name_np(thread, name);
2602-
rc = 0; /* pthread_set_name_np() returns void */
2603-
#endif
2604-
return rc;
2605-
}
2606-
2607-
/* Helper to encode and truncate thread name; returns new reference or NULL */
2608-
static PyObject *
2609-
encode_thread_name(PyObject *name_obj, const char *encoding)
2610-
{
2611-
#ifdef __sun
2612-
/* Solaris always uses UTF-8 */
2613-
encoding = "utf-8";
2614-
#endif
2615-
2616-
PyObject *name_encoded = PyUnicode_AsEncodedString(name_obj, encoding, "replace");
2617-
if (name_encoded == NULL) {
2618-
return NULL;
2619-
}
2620-
2621-
#ifdef _PYTHREAD_NAME_MAXLEN
2622-
if (PyBytes_GET_SIZE(name_encoded) > _PYTHREAD_NAME_MAXLEN) {
2623-
PyObject *truncated = PyBytes_FromStringAndSize(PyBytes_AS_STRING(name_encoded),
2624-
_PYTHREAD_NAME_MAXLEN);
2625-
Py_DECREF(name_encoded);
2626-
if (truncated == NULL) {
2627-
return NULL;
2628-
}
2629-
return truncated;
2630-
}
2631-
#endif
2632-
2633-
return name_encoded;
2634-
}
2635-
26362632
/* Implementation of _thread.set_name */
26372633
static PyObject *
26382634
_thread_set_name_impl(PyObject *module, PyObject *name_obj)

0 commit comments

Comments
 (0)