Skip to content

Commit 2546653

Browse files
committed
Introduce PyThread_ident_t, PyThread_handle_t
1 parent 1882e0a commit 2546653

File tree

4 files changed

+45
-40
lines changed

4 files changed

+45
-40
lines changed

Include/internal/pycore_pythread.h

+12-7
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ PyAPI_FUNC(PyLockStatus) PyThread_acquire_lock_timed_with_retries(
106106
PyThread_type_lock,
107107
PY_TIMEOUT_T microseconds);
108108

109-
PyAPI_FUNC(unsigned long long) PyThread_get_thread_ident_ex(void);
109+
typedef unsigned long long PyThread_ident_t;
110+
typedef Py_uintptr_t PyThread_handle_t;
111+
112+
#define PY_FORMAT_THREAD_IDENT_T "llu"
113+
114+
PyAPI_FUNC(PyThread_ident_t) PyThread_get_thread_ident_ex(void);
110115

111116
/* Thread joining APIs.
112117
*
@@ -121,27 +126,27 @@ PyAPI_FUNC(unsigned long long) PyThread_get_thread_ident_ex(void);
121126
*/
122127
PyAPI_FUNC(int) PyThread_start_joinable_thread(void (*func)(void *),
123128
void *arg,
124-
unsigned long long* ident,
125-
Py_uintptr_t* handle);
129+
PyThread_ident_t* ident,
130+
PyThread_handle_t* handle);
126131
/*
127132
* Join a thread started with `PyThread_start_joinable_thread`.
128133
* This function cannot be interrupted. It returns 0 on success,
129134
* a non-zero value on failure.
130135
*/
131-
PyAPI_FUNC(int) PyThread_join_thread(Py_uintptr_t);
136+
PyAPI_FUNC(int) PyThread_join_thread(PyThread_handle_t);
132137
/*
133138
* Detach a thread started with `PyThread_start_joinable_thread`, such
134139
* that its resources are relased as soon as it exits.
135140
* This function cannot be interrupted. It returns 0 on success,
136141
* a non-zero value on failure.
137142
*/
138-
PyAPI_FUNC(int) PyThread_detach_thread(Py_uintptr_t);
143+
PyAPI_FUNC(int) PyThread_detach_thread(PyThread_handle_t);
139144

140145
/*
141146
* Obtain the new thread ident and handle in a forked child process.
142147
*/
143-
PyAPI_FUNC(void) PyThread_update_thread_after_fork(unsigned long long* ident,
144-
Py_uintptr_t* handle);
148+
PyAPI_FUNC(void) PyThread_update_thread_after_fork(PyThread_ident_t* ident,
149+
PyThread_handle_t* handle);
145150

146151
#ifdef __cplusplus
147152
}

Modules/_threadmodule.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ get_thread_state(PyObject *module)
4444

4545
typedef struct {
4646
PyObject_HEAD
47-
unsigned long long ident;
48-
Py_uintptr_t handle;
47+
PyThread_ident_t ident;
48+
PyThread_handle_t handle;
4949
char joinable;
5050
} ThreadHandleObject;
5151

@@ -80,7 +80,7 @@ ThreadHandle_dealloc(ThreadHandleObject *self)
8080
static PyObject *
8181
ThreadHandle_repr(ThreadHandleObject *self)
8282
{
83-
return PyUnicode_FromFormat("<%s object: ident=%llu>",
83+
return PyUnicode_FromFormat("<%s object: ident=" PY_FORMAT_THREAD_IDENT_T ">",
8484
Py_TYPE(self)->tp_name, self->ident);
8585
}
8686

@@ -415,7 +415,7 @@ static PyType_Spec lock_type_spec = {
415415
typedef struct {
416416
PyObject_HEAD
417417
PyThread_type_lock rlock_lock;
418-
unsigned long long rlock_owner;
418+
PyThread_ident_t rlock_owner;
419419
unsigned long rlock_count;
420420
PyObject *in_weakreflist;
421421
} rlockobject;
@@ -452,7 +452,7 @@ static PyObject *
452452
rlock_acquire(rlockobject *self, PyObject *args, PyObject *kwds)
453453
{
454454
_PyTime_t timeout;
455-
unsigned long long tid;
455+
PyThread_ident_t tid;
456456
PyLockStatus r = PY_LOCK_ACQUIRED;
457457

458458
if (lock_acquire_parse_args(args, kwds, &timeout) < 0)
@@ -501,7 +501,7 @@ the lock is taken and its internal counter initialized to 1.");
501501
static PyObject *
502502
rlock_release(rlockobject *self, PyObject *Py_UNUSED(ignored))
503503
{
504-
unsigned long long tid = PyThread_get_thread_ident_ex();
504+
PyThread_ident_t tid = PyThread_get_thread_ident_ex();
505505

506506
if (self->rlock_count == 0 || self->rlock_owner != tid) {
507507
PyErr_SetString(PyExc_RuntimeError,
@@ -585,7 +585,7 @@ For internal use by `threading.Condition`.");
585585
static PyObject *
586586
rlock_recursion_count(rlockobject *self, PyObject *Py_UNUSED(ignored))
587587
{
588-
unsigned long long tid = PyThread_get_thread_ident_ex();
588+
PyThread_ident_t tid = PyThread_get_thread_ident_ex();
589589
return PyLong_FromUnsignedLong(
590590
self->rlock_owner == tid ? self->rlock_count : 0UL);
591591
}
@@ -598,7 +598,7 @@ For internal use by reentrancy checks.");
598598
static PyObject *
599599
rlock_is_owned(rlockobject *self, PyObject *Py_UNUSED(ignored))
600600
{
601-
unsigned long long tid = PyThread_get_thread_ident_ex();
601+
PyThread_ident_t tid = PyThread_get_thread_ident_ex();
602602

603603
if (self->rlock_count > 0 && self->rlock_owner == tid) {
604604
Py_RETURN_TRUE;
@@ -1253,7 +1253,7 @@ static int
12531253
do_start_new_thread(thread_module_state* state,
12541254
PyObject *func, PyObject* args, PyObject* kwargs,
12551255
int joinable,
1256-
unsigned long long* ident, Py_uintptr_t* handle)
1256+
PyThread_ident_t* ident, PyThread_handle_t* handle)
12571257
{
12581258
PyInterpreterState *interp = _PyInterpreterState_GET();
12591259
if (!_PyInterpreterState_HasFeature(interp, Py_RTFLAGS_THREADS)) {
@@ -1334,8 +1334,8 @@ thread_PyThread_start_new_thread(PyObject *module, PyObject *fargs)
13341334
return NULL;
13351335
}
13361336

1337-
unsigned long long ident = 0;
1338-
Py_uintptr_t handle;
1337+
PyThread_ident_t ident = 0;
1338+
PyThread_handle_t handle;
13391339
if (do_start_new_thread(state, func, args, kwargs, /*joinable=*/ 0,
13401340
&ident, &handle)) {
13411341
return NULL;
@@ -1460,7 +1460,7 @@ information about locks.");
14601460
static PyObject *
14611461
thread_get_ident(PyObject *self, PyObject *Py_UNUSED(ignored))
14621462
{
1463-
unsigned long long ident = PyThread_get_thread_ident_ex();
1463+
PyThread_ident_t ident = PyThread_get_thread_ident_ex();
14641464
if (ident == PYTHREAD_INVALID_THREAD_ID) {
14651465
PyErr_SetString(ThreadError, "no current thread ident");
14661466
return NULL;
@@ -1652,8 +1652,8 @@ thread_excepthook_file(PyObject *file, PyObject *exc_type, PyObject *exc_value,
16521652
Py_DECREF(name);
16531653
}
16541654
else {
1655-
unsigned long long ident = PyThread_get_thread_ident_ex();
1656-
PyObject *str = PyUnicode_FromFormat("%llu", ident);
1655+
PyThread_ident_t ident = PyThread_get_thread_ident_ex();
1656+
PyObject *str = PyUnicode_FromFormat("%" PY_FORMAT_THREAD_IDENT_T, ident);
16571657
if (str != NULL) {
16581658
if (PyFile_WriteObject(str, file, Py_PRINT_RAW) < 0) {
16591659
Py_DECREF(str);

Python/thread_nt.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ bootstrap(void *call)
184184

185185
int
186186
PyThread_start_joinable_thread(void (*func)(void *), void *arg,
187-
unsigned long long* ident, Py_uintptr_t* handle) {
187+
PyThread_ident_t* ident, PyThread_handle_t* handle) {
188188
HANDLE hThread;
189189
unsigned threadID;
190190
callobj *obj;
@@ -212,14 +212,14 @@ PyThread_start_joinable_thread(void (*func)(void *), void *arg,
212212
}
213213
*ident = threadID;
214214
// The cast is safe since HANDLE is pointer-sized
215-
*handle = (Py_uintptr_t) hThread;
215+
*handle = (PyThread_handle_t) hThread;
216216
return 0;
217217
}
218218

219219
unsigned long
220220
PyThread_start_new_thread(void (*func)(void *), void *arg) {
221-
Py_uintptr_t handle;
222-
unsigned long long ident;
221+
PyThread_handle_t handle;
222+
PyThread_ident_t ident;
223223
if (PyThread_start_joinable_thread(func, arg, &ident, &handle)) {
224224
return PYTHREAD_INVALID_THREAD_ID;
225225
}
@@ -229,28 +229,28 @@ PyThread_start_new_thread(void (*func)(void *), void *arg) {
229229
}
230230

231231
int
232-
PyThread_join_thread(Py_uintptr_t handle) {
232+
PyThread_join_thread(PyThread_handle_t handle) {
233233
HANDLE hThread = (HANDLE) handle;
234234
int errored = (WaitForSingleObject(hThread, INFINITE) != WAIT_OBJECT_0);
235235
CloseHandle(hThread);
236236
return errored;
237237
}
238238

239239
int
240-
PyThread_detach_thread(Py_uintptr_t handle) {
240+
PyThread_detach_thread(PyThread_handle_t handle) {
241241
HANDLE hThread = (HANDLE) handle;
242242
return (CloseHandle(hThread) == 0);
243243
}
244244

245245
void
246-
PyThread_update_thread_after_fork(unsigned long long* ident, Py_uintptr_t* handle) {
246+
PyThread_update_thread_after_fork(PyThread_ident_t* ident, PyThread_handle_t* handle) {
247247
}
248248

249249
/*
250250
* Return the thread Id instead of a handle. The Id is said to uniquely identify the
251251
* thread in the system
252252
*/
253-
unsigned long long
253+
PyThread_ident_t
254254
PyThread_get_thread_ident_ex(void)
255255
{
256256
if (!initialized)

Python/thread_pthread.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,13 @@ do_start_joinable_thread(void (*func)(void *), void *arg, pthread_t* out_id)
300300

301301
int
302302
PyThread_start_joinable_thread(void (*func)(void *), void *arg,
303-
unsigned long long* ident, Py_uintptr_t* handle) {
303+
PyThread_ident_t* ident, PyThread_handle_t* handle) {
304304
pthread_t th = (pthread_t) 0;
305305
if (do_start_joinable_thread(func, arg, &th)) {
306306
return -1;
307307
}
308-
*ident = (unsigned long long) th;
309-
*handle = (Py_uintptr_t) th;
308+
*ident = (PyThread_ident_t) th;
309+
*handle = (PyThread_handle_t) th;
310310
assert(th == (pthread_t) *ident);
311311
assert(th == (pthread_t) *handle);
312312
return 0;
@@ -328,21 +328,21 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
328328
}
329329

330330
int
331-
PyThread_join_thread(Py_uintptr_t th) {
331+
PyThread_join_thread(PyThread_handle_t th) {
332332
return pthread_join((pthread_t) th, NULL);
333333
}
334334

335335
int
336-
PyThread_detach_thread(Py_uintptr_t th) {
336+
PyThread_detach_thread(PyThread_handle_t th) {
337337
return pthread_detach((pthread_t) th);
338338
}
339339

340340
void
341-
PyThread_update_thread_after_fork(unsigned long long* ident, Py_uintptr_t* handle) {
341+
PyThread_update_thread_after_fork(PyThread_ident_t* ident, PyThread_handle_t* handle) {
342342
// The thread id might have been updated in the forked child
343343
pthread_t th = pthread_self();
344-
*ident = (unsigned long long) th;
345-
*handle = (Py_uintptr_t) th;
344+
*ident = (PyThread_ident_t) th;
345+
*handle = (PyThread_handle_t) th;
346346
assert(th == (pthread_t) *ident);
347347
assert(th == (pthread_t) *handle);
348348
}
@@ -353,14 +353,14 @@ PyThread_update_thread_after_fork(unsigned long long* ident, Py_uintptr_t* handl
353353
- The cast to unsigned long is inherently unsafe.
354354
- It is not clear that the 'volatile' (for AIX?) are any longer necessary.
355355
*/
356-
unsigned long long
356+
PyThread_ident_t
357357
PyThread_get_thread_ident_ex(void) {
358358
volatile pthread_t threadid;
359359
if (!initialized)
360360
PyThread_init_thread();
361361
threadid = pthread_self();
362-
assert(threadid == (pthread_t) (unsigned long long) threadid);
363-
return (unsigned long long) threadid;
362+
assert(threadid == (pthread_t) (PyThread_ident_t) threadid);
363+
return (PyThread_ident_t) threadid;
364364
}
365365

366366
unsigned long

0 commit comments

Comments
 (0)