X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=platform%2Fwin32%2Ffunctions%2Fthreads%2Ftss_create.c;h=4d41e0309e96d3314347db0a16695dc2a1a783dc;hp=a9394aff4c7c2d95a4e7b379dd7e140c6678ac44;hb=abc15df6b9fae3374d24c7cf5c3ab94c605b2a6d;hpb=8894c921674bb116d0a7b8f23a55311e7a768019 diff --git a/platform/win32/functions/threads/tss_create.c b/platform/win32/functions/threads/tss_create.c index a9394af..4d41e03 100644 --- a/platform/win32/functions/threads/tss_create.c +++ b/platform/win32/functions/threads/tss_create.c @@ -1,73 +1,73 @@ -#ifndef REGTEST -#include -#include -#include - -/* Pull in TLS support */ -extern char _tls_used[]; - -struct _PDCLIB_tss * _PDCLIB_tss_first = NULL; - -int tss_create( tss_t *key, tss_dtor_t dtor ) -{ - *key = malloc( sizeof *key ); - if( !*key ) { - return thrd_nomem; - } - - (*key)->_Key = TlsAlloc(); - if((*key)->_Key == TLS_OUT_OF_INDEXES) { - return thrd_error; - } - (*key)->_Destructor = dtor; - (*key)->_Next = _PDCLIB_tss_first; - - // TODO: make this atomic (& validate no other TLS blocks have been - // simultaneously allocated) - _PDCLIB_tss_first = *key; - - return thrd_success; -} - -static void NTAPI runTlsDestructors( void * image, DWORD reason, PVOID pv ) -{ - if( reason == DLL_THREAD_DETACH ) { - for(unsigned i = 0; i < TSS_DTOR_ITERATIONS; i++) { - struct _PDCLIB_tss * tss = _PDCLIB_tss_first; - bool destructorsRan = false; - while( tss ) { - void * val = TlsGetValue( tss->_Key ); - if( val ) { - TlsSetValue( tss->_Key, NULL ); - if( tss->_Destructor ) { - tss->_Destructor( val ); - destructorsRan = true; - } - } - - tss = tss->_Next; - } - if(!destructorsRan) break; - } - } -} - -#ifdef __GNUC__ -__attribute__((__section__(".CRT$XLC"))) -#else -__declspec(allocate(".CRT$XLC")) -#endif -PIMAGE_TLS_CALLBACK _PDCLIB_runTlsDestructors = runTlsDestructors; - -#endif - -#ifdef TEST -#include "_PDCLIB_test.h" - -/* Tested in tss_get.c */ -int main( void ) -{ - return TEST_RESULTS; -} - +#ifndef REGTEST +#include +#include +#include + +/* Pull in TLS support */ +extern char _tls_used[]; + +struct _PDCLIB_tss * _PDCLIB_tss_first = NULL; + +int tss_create( tss_t *key, tss_dtor_t dtor ) +{ + *key = malloc( sizeof *key ); + if( !*key ) { + return thrd_nomem; + } + + (*key)->_Key = TlsAlloc(); + if((*key)->_Key == TLS_OUT_OF_INDEXES) { + return thrd_error; + } + (*key)->_Destructor = dtor; + (*key)->_Next = _PDCLIB_tss_first; + + // TODO: make this atomic (& validate no other TLS blocks have been + // simultaneously allocated) + _PDCLIB_tss_first = *key; + + return thrd_success; +} + +static void NTAPI runTlsDestructors( void * image, DWORD reason, PVOID pv ) +{ + if( reason == DLL_THREAD_DETACH ) { + for(unsigned i = 0; i < TSS_DTOR_ITERATIONS; i++) { + struct _PDCLIB_tss * tss = _PDCLIB_tss_first; + bool destructorsRan = false; + while( tss ) { + void * val = TlsGetValue( tss->_Key ); + if( val ) { + TlsSetValue( tss->_Key, NULL ); + if( tss->_Destructor ) { + tss->_Destructor( val ); + destructorsRan = true; + } + } + + tss = tss->_Next; + } + if(!destructorsRan) break; + } + } +} + +#ifdef __GNUC__ +__attribute__((__section__(".CRT$XLC"))) +#else +__declspec(allocate(".CRT$XLC")) +#endif +PIMAGE_TLS_CALLBACK _PDCLIB_runTlsDestructors = runTlsDestructors; + +#endif + +#ifdef TEST +#include "_PDCLIB_test.h" + +/* Tested in tss_get.c */ +int main( void ) +{ + return TEST_RESULTS; +} + #endif \ No newline at end of file