[Python-Dev] Threading and callbacks - missing documentation (original) (raw)

Tim Peters tim.one@comcast.net
Fri, 12 Apr 2002 14:21:17 -0400


[Tim]

... I believe Mark Hammond has a general set of C++ classes to help with this stuff on Windows, but IIRC they rely on Windows-specific TLS (thread local storage) gimmicks.

[Gordon McMillan]

No TLS. Mark's stuff actually relies on being called (at some point) with the GIL so he can grab the interpreter- and thread-states.

Yes TLS, and all over the place. Look at the code for any of PyWinThreadState_Ensure, PyWinInterpreterLock_Acquire, PyWinInterpreterLock_Release, PyWinThreadState_Free, PyWinThreadState_Clear or DLLMAIN. TLS is used to remember what a thread's PyThreadState is, and to determine whether a thread already has a PyThreadState (it does iff the TLS slot at dwTlsIndex is non-NULL). Simple example:

// Asuming we have a valid thread state, acquire the Python lock. void PyWinInterpreterLock_Acquire() { ThreadData *pData = (ThreadData *)TlsGetValue(dwTlsIndex); PyThreadState *thisThreadState = pData->ts; PyEval_AcquireThread(thisThreadState); }