From 94cfb27da3941eea2b20867eacc09a5b91168438 Mon Sep 17 00:00:00 2001 From: rpj Date: Thu, 30 Jul 1998 13:51:57 +0000 Subject: Thu Jul 30 23:12:45 1998 Ross Johnson * implement.h: Remove _pthread_find_entry() prototype. * private.c: Extend comments. Remove _pthread_find_entry() - no longer needed. * create.c (_pthread_start_call): Add call to TlsSetValue() to store the thread ID. * dll.c (PthreadsEntryPoint): Implement. This is called whenever a process loads the DLL. Used to initialise thread local storage. * implement.h: Add _pthread_threadID_TlsIndex. Add ()s around _PTHREAD_VALID expression. * misc.c (pthread_self): Re-implement using Win32 TLS to store the threads own ID. --- ChangeLog | 20 ++++++++++++++++++++ create.c | 2 ++ dll.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ implement.h | 13 ++++++++----- misc.c | 23 ++++++++--------------- private.c | 22 ++++++++++++++++------ 6 files changed, 106 insertions(+), 26 deletions(-) create mode 100644 dll.c diff --git a/ChangeLog b/ChangeLog index f3fddb7..d7dd4da 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +Thu Jul 30 23:12:45 1998 Ross Johnson + + * implement.h: Remove _pthread_find_entry() prototype. + + * private.c: Extend comments. + Remove _pthread_find_entry() - no longer needed. + + * create.c (_pthread_start_call): Add call to TlsSetValue() to + store the thread ID. + + * dll.c (PthreadsEntryPoint): Implement. This is called + whenever a process loads the DLL. Used to initialise thread + local storage. + + * implement.h: Add _pthread_threadID_TlsIndex. + Add ()s around _PTHREAD_VALID expression. + + * misc.c (pthread_self): Re-implement using Win32 TLS to store + the threads own ID. + Wed Jul 29 11:39:03 1998 Ross Johnson * private.c: Corrections in comments. diff --git a/create.c b/create.c index 85b5a0f..87a8017 100644 --- a/create.c +++ b/create.c @@ -28,6 +28,8 @@ _pthread_start_call(void * us_arg) us = (pthread_t) us_arg; + (void) TlsSetValue(_pthread_threadID_TlsIndex, (LPVOID) us); + /* FIXME: For now, if priority setting fails then at least ensure that our records reflect true reality. */ if (SetThreadPriority((HANDLE) us->win32handle, us->attr.priority) == FALSE) diff --git a/dll.c b/dll.c new file mode 100644 index 0000000..518d443 --- /dev/null +++ b/dll.c @@ -0,0 +1,52 @@ +/* + * dll.c + * + * Description: + * This translation unit implements DLL initialisation. + */ + +/* We use the DLL entry point function to set up per thread storage + specifically to hold the threads own thread ID. + + The thread ID is stored by _pthread_start_call(). + + The thread ID is retrieved by pthread_self(). + + */ + +#include + +/* Global index for TLS data. */ +DWORD _pthread_threadID_TlsIndex; + +BOOL WINAPI PthreadsEntryPoint(HINSTANCE dllHandle, + DWORD reason, + LPVOID situation) +{ + + + switch (reason) + { + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + break; + + case DLL_PROCESS_ATTACH: + _pthread_threadID_TlsIndex = TlsAlloc(); + + if (_pthread_threadID_TlsIndex == 0xFFFFFFFF) + { + return FALSE; + } + break; + + case DLL_PROCESS_DETACH: + (void) TlsFree(_pthread_threadID_TlsIndex); + break; + + default: + return FALSE; + } + + return TRUE; +} diff --git a/implement.h b/implement.h index 5ee9ba3..ba8db6e 100644 --- a/implement.h +++ b/implement.h @@ -17,9 +17,9 @@ enum { }; #define _PTHREAD_VALID(T) \ - (T) != NULL \ - && ((T)->ptstatus == _PTHREAD_NEW - || (T)->ptstatus == _PTHREAD_INUSE) + ((T) != NULL \ + && ((T)->ptstatus == _PTHREAD_NEW + || (T)->ptstatus == _PTHREAD_INUSE)) /* Handler execution flags. */ #define _PTHREAD_HANDLER_NOEXECUTE 0 @@ -114,8 +114,6 @@ void _pthread_handler_pop_all(int stack, int _pthread_new_thread(pthread_t * thread); -pthread_t _pthread_find_thread(HANDLE win32handle); - int _pthread_delete_thread(pthread_t thread); /* Thread cleanup. */ @@ -129,6 +127,11 @@ void _pthread_exit(pthread_t thread, void * value, int return_code); #endif /* __cplusplus */ +/* Global declared dll.c */ + +extern DWORD _pthread_threadID_TlsIndex; + + /* Global data declared in global.c */ extern pthread_mutex_t _pthread_table_mutex; diff --git a/misc.c b/misc.c index 9834630..ca39513 100644 --- a/misc.c +++ b/misc.c @@ -44,23 +44,16 @@ pthread_once(pthread_once_t *once_control, pthread_t pthread_self(void) { - /* It looks like a pthread_t needs to be a HANDLE, but Win32 also has - another way of identifying threads: their thread id. We hope - that all of the Win32 functions we are going to use only need - HANDLEs. The morons. */ + pthread_t ret; + + ret = (pthread_t) TlsGetValue(_pthread_threadID_TlsIndex); - /* FIXME: Need a new lookup method with the new thread allocation - scheme. - - We can use the Win32 handle though as a basis (perhaps - to look up a table) because pthread_self() will never be called - after the Win32 thread has terminated (unless we can raise - ourselves from the dead!), and therefore the Win32 handle cannot - have been reused yet. */ + if (ret == 0) + { + /* FIXME: Oh no! This can't happen. */ + } -#if 0 - return GetCurrentThread(); -#endif + return ret; } int diff --git a/private.c b/private.c index 2ac2e31..454695d 100644 --- a/private.c +++ b/private.c @@ -95,6 +95,22 @@ _pthread_reuse[++_pthread_reuse_top] = thread; + + We still need a means for pthread_self() to return its own thread + ID. + + We use the Win32 Thread Local Storage mechanism. A single call to + TlsAlloc() will make available a single 32 bit location to every + thread in the process, including those created after the call is + made. + + Provided we don't need to call pthread_self() after the Win32 + thread has terminated we can use the DLL entry point routine to + initialise TLS for each thread. Or we can use pthread_once() in + pthread_create() to do it. + + We can use either option. We'll use the DLL entry point routine. + */ int @@ -136,12 +152,6 @@ _pthread_new_thread(pthread_t * thread) return 0; } -pthread_t -_pthread_find_thread((HANDLE) win32handle) -{ - /* FIXME: No-op at present */ -} - int _pthread_delete_thread(_pthread_t * thread) { -- cgit v1.2.3