pdox <p...@alum.mit.edu> added the comment:

If we don't want to change the type of get_ident(), there is another option.

We could have PyThread keep track of which thread ids (e.g. pthread_t) values 
are valid (meaning, the thread is still running). This could be tracked in a 
global data structure (protected by a mutex) internal to PyThread. This can be 
done automatically for threads created by PyThread, and threads where Python is 
running (in between PyGILState_Ensure/PyGILState_Release, or other entry 
functions). If PyThread had this ability, then pthread_kill, etc. could raise 
an exception when the thread_id is invalid.

The tricky part would be non-Python-created threads. There two obvious options 
there... one would be to provide explicit PyThread_Register/PyThread_Unregister 
functions that a thread could call to make itself known to PyThread. The other, 
would be to allow the use of unsafe thread_id's, as long as an override flag is 
provided. (e.g. pthread_kill(thread_id, sig, allow_unsafe=True).

To take the abstraction one step further, we could completely hide the OS-level 
thread identifier inside PyThread, and instead generate our own ids, 
guaranteeing that they will always be integers. (and perhaps also guaranteeing 
a higher level of uniqueness)

(This is not so unusual, as pthread_t is itself an abstraction, unrelated to 
kernel-level thread ids. On linux, the kernel identifies threads using globally 
unique TIDs, which can be fetched with SYS_gettid. This value does not match 
pthread_self(). With GLIBC, the value of pthread_t is (usually) a pointer to 
the bottom of the stack for the thread, which holds a thread descriptor (placed 
there by pthread_create). For this reason, pthread doesn't mesh well with 
threads that it didn't create (e.g., calling pthread_self in a thread which was 
created with clone(), will not do the right thing). pthread_kill is essentially 
a wrapper around tkill(), which first resolves the pthread_t into a TID.)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue31622>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to