X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=mech_eap%2Futil_tld.c;h=05bc3d11923030141bcc40fc408793f03f5b6f2f;hb=23bbef1fbb4bcaffb9b954a409942b2807476a08;hp=2e1ddfac1effa1c7a326b49ba6a33f89395919ff;hpb=7a796448997e185aa509ae195c719f834ed58500;p=mech_eap.orig diff --git a/mech_eap/util_tld.c b/mech_eap/util_tld.c index 2e1ddfa..05bc3d1 100644 --- a/mech_eap/util_tld.c +++ b/mech_eap/util_tld.c @@ -31,7 +31,8 @@ */ /* - * Thread local data abstraction. + * Thread local data abstraction, using pthreads on Unix and the TlsXXX + * APIs on Windows. */ #include "gssapiP_eap.h" @@ -50,18 +51,28 @@ destroyThreadLocalData(struct gss_eap_thread_local_data *tld) #ifdef WIN32 /* - * This is the tls index returned by TlsAlloc() on process init. + * This is the TLS index returned by TlsAlloc() on process init. * Each thread, on thread attach in DllMain(), allocates its thread-local * data and uses this index with TlsSetValue() to store it. * It can then subsequently be retrieved with TlsGetValue(). */ -static DWORD tlsIndex; +static DWORD tlsIndex = TLS_OUT_OF_INDEXES; /* Access thread-local data */ struct gss_eap_thread_local_data * gssEapGetThreadLocalData(void) { - return TlsGetValue(tlsIndex); + struct gss_eap_thread_local_data *tlsData; + + GSSEAP_ASSERT(tlsIndex != TLS_OUT_OF_INDEXES); + + tlsData = TlsGetValue(tlsIndex); + if (tlsData == NULL) { + tlsData = GSSEAP_CALLOC(1, sizeof(*tlsData)); + TlsSetValue(tlsIndex, tlsData); + } + + return tlsData; } BOOL WINAPI @@ -70,11 +81,15 @@ DllMain(HINSTANCE hDLL, /* DLL module handle */ LPVOID reserved) /* reserved */ { struct gss_eap_thread_local_data *tlsData; + OM_uint32 major, minor; switch (reason) { case DLL_PROCESS_ATTACH: /* Allocate a TLS index. */ - gssEapInitiatorInit(); + major = gssEapInitiatorInit(&minor); + if (GSS_ERROR(major)) + return FALSE; + tlsIndex = TlsAlloc(); if (tlsIndex == TLS_OUT_OF_INDEXES) return FALSE; @@ -82,8 +97,9 @@ DllMain(HINSTANCE hDLL, /* DLL module handle */ case DLL_THREAD_ATTACH: /* Initialize the TLS index for this thread. */ tlsData = GSSEAP_CALLOC(1, sizeof(*tlsData)); - if (tlsData != NULL) - TlsSetValue(tlsIndex, tlsData); + if (tlsData == NULL) + return FALSE; + TlsSetValue(tlsIndex, tlsData); break; case DLL_THREAD_DETACH: /* Release the allocated memory for this thread. */ @@ -94,10 +110,6 @@ DllMain(HINSTANCE hDLL, /* DLL module handle */ } break; case DLL_PROCESS_DETACH: - /* Release the allocated memory for this thread. */ - tlsData = TlsGetValue(tlsIndex); - if (tlsData != NULL) - destroyThreadLocalData(tlsData); /* Release the TLS index. */ TlsFree(tlsIndex); gssEapFinalize(); @@ -151,4 +163,5 @@ gssEapGetThreadLocalData() return tld; } + #endif /* WIN32 */