* SUCH DAMAGE.
*/
-/* Access all thread-local data through these methods which
- * use pthreads to manage thread-local memory on Unix and TlsFoo() on Windows.
- * This would be more flexible, scalable, and extensible
- * if implemented through a callback interface, but given that
- * there are currently only two 'clients', hard-coding seems more
- * straightforward
+/*
+ * Thread local data abstraction, using pthreads on Unix and the TlsXXX
+ * APIs on Windows.
*/
+
#include "gssapiP_eap.h"
/* Clean up thread-local data; called on thread detach */
static void
-destroyThreadLocalData(struct gss_eap_thread_local_data* tld)
+destroyThreadLocalData(struct gss_eap_thread_local_data *tld)
{
- if (tld->status_info)
- gssEapDestroyStatusInfo(tld->status_info);
- if (tld->context)
- gssEapDestroyKrbContext(tld->context);
+ if (tld->statusInfo != NULL)
+ gssEapDestroyStatusInfo(tld->statusInfo);
+ if (tld->krbContext != NULL)
+ gssEapDestroyKrbContext(tld->krbContext);
GSSEAP_FREE(tld);
}
#ifdef WIN32
-/* 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()
+/*
+ * 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;
/* Access thread-local data */
struct gss_eap_thread_local_data *
-gssEapGetThreadLocalData()
+gssEapGetThreadLocalData(void)
{
return TlsGetValue(tlsIndex);
}
-
-/* DllMain() is the entry-point function for this DLL. */
-BOOL WINAPI DllMain(HINSTANCE hDLL, /* DLL module handle */
- DWORD reason, /* reason called */
- LPVOID reserved) /* reserved */
-{
+
+BOOL WINAPI
+DllMain(HINSTANCE hDLL, /* DLL module handle */
+ DWORD reason, /* reason called */
+ LPVOID reserved) /* reserved */
+{
struct gss_eap_thread_local_data *tlsData;
- switch (reason)
- {
- // The DLL is loading due to process
- // initialization or a call to LoadLibrary.
- case DLL_PROCESS_ATTACH:
+ switch (reason) {
+ case DLL_PROCESS_ATTACH:
/* Allocate a TLS index. */
- if ((tlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)
- return FALSE;
+ gssEapInitiatorInit();
+ tlsIndex = TlsAlloc();
+ if (tlsIndex == TLS_OUT_OF_INDEXES)
+ return FALSE;
/* No break: Initialize the index for first thread.*/
-
- /* The attached process creates a new thread. */
- case DLL_THREAD_ATTACH:
+ case DLL_THREAD_ATTACH:
/* Initialize the TLS index for this thread. */
tlsData = GSSEAP_CALLOC(1, sizeof(*tlsData));
- if (tlsData != NULL)
- TlsSetValue(tlsIndex, tlsData);
-
- break;
-
- /* The thread of the attached process terminates. */
- case DLL_THREAD_DETACH:
+ if (tlsData != NULL)
+ TlsSetValue(tlsIndex, tlsData);
+ break;
+ case DLL_THREAD_DETACH:
/* Release the allocated memory for this thread. */
- tlsData = TlsGetValue(tlsIndex);
- if (tlsData != NULL)
- {
- destroyThreadLocalData(tlsData);
+ tlsData = TlsGetValue(tlsIndex);
+ if (tlsData != NULL) {
+ destroyThreadLocalData(tlsData);
TlsSetValue(tlsIndex, NULL);
}
-
- break;
-
- /* DLL unload due to process termination or FreeLibrary. */
- case DLL_PROCESS_DETACH:
+ break;
+ case DLL_PROCESS_DETACH:
/* Release the allocated memory for this thread. */
- tlsData = TlsGetValue(tlsIndex);
- if (tlsData != NULL)
- destroyThreadLocalData(tlsData);
+ tlsData = TlsGetValue(tlsIndex);
+ if (tlsData != NULL)
+ destroyThreadLocalData(tlsData);
/* Release the TLS index. */
- TlsFree(tlsIndex);
- break;
-
- default:
- break;
- }
-
- return TRUE;
- UNREFERENCED_PARAMETER(hDLL);
- UNREFERENCED_PARAMETER(reserved);
+ TlsFree(tlsIndex);
+ gssEapFinalize();
+ break;
+ default:
+ break;
+ }
+
+ return TRUE;
+ UNREFERENCED_PARAMETER(hDLL);
+ UNREFERENCED_PARAMETER(reserved);
}
#else /* WIN32 */
-/* PTHREAD implementation */
+/* pthreads implementation */
+
static GSSEAP_THREAD_ONCE tldKeyOnce = GSSEAP_ONCE_INITIALIZER;
static GSSEAP_THREAD_KEY tldKey;
-static void
-pthreadDestroyThreadLocalData(void* arg)
+
+static void
+pthreadDestroyThreadLocalData(void *arg)
{
struct gss_eap_thread_local_data* tld = arg;
- if (tld)
- {
+
+ if (tld != NULL)
destroyThreadLocalData(tld);
- }
}
-static void
+static void
createThreadLocalDataKey(void)
{
GSSEAP_KEY_CREATE(&tldKey, pthreadDestroyThreadLocalData);
}
-struct gss_eap_thread_local_data *
+struct gss_eap_thread_local_data *
gssEapGetThreadLocalData()
{
struct gss_eap_thread_local_data *tld;
+
GSSEAP_ONCE(&tldKeyOnce, createThreadLocalDataKey);
+
tld = GSSEAP_GETSPECIFIC(tldKey);
- if (!tld)
- {
+ if (tld == NULL) {
tld = GSSEAP_CALLOC(1, sizeof(*tld));
+ if (tld == NULL)
+ return NULL;
+
GSSEAP_SETSPECIFIC(tldKey, tld);
}
+
return tld;
}