2 * Copyright (c) 2011, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Thread local data abstraction, using pthreads on Unix and the TlsXXX
38 #include "gssapiP_eap.h"
40 /* Clean up thread-local data; called on thread detach */
42 destroyThreadLocalData(struct gss_eap_thread_local_data *tld)
44 if (tld->statusInfo != NULL)
45 gssEapDestroyStatusInfo(tld->statusInfo);
46 if (tld->krbContext != NULL)
47 gssEapDestroyKrbContext(tld->krbContext);
54 * This is the TLS index returned by TlsAlloc() on process init.
55 * Each thread, on thread attach in DllMain(), allocates its thread-local
56 * data and uses this index with TlsSetValue() to store it.
57 * It can then subsequently be retrieved with TlsGetValue().
59 static DWORD tlsIndex = TLS_OUT_OF_INDEXES;
61 /* Access thread-local data */
62 struct gss_eap_thread_local_data *
63 gssEapGetThreadLocalData(void)
65 struct gss_eap_thread_local_data *tlsData;
67 GSSEAP_ASSERT(tlsIndex != TLS_OUT_OF_INDEXES);
69 tlsData = TlsGetValue(tlsIndex);
70 if (tlsData == NULL) {
71 tlsData = GSSEAP_CALLOC(1, sizeof(*tlsData));
72 TlsSetValue(tlsIndex, tlsData);
79 DllMain(HINSTANCE hDLL, /* DLL module handle */
80 DWORD reason, /* reason called */
81 LPVOID reserved) /* reserved */
83 struct gss_eap_thread_local_data *tlsData;
84 OM_uint32 major, minor;
87 case DLL_PROCESS_ATTACH:
88 /* Allocate a TLS index. */
89 major = gssEapInitiatorInit(&minor);
93 tlsIndex = TlsAlloc();
94 if (tlsIndex == TLS_OUT_OF_INDEXES)
96 /* No break: Initialize the index for first thread.*/
97 case DLL_THREAD_ATTACH:
98 /* Initialize the TLS index for this thread. */
99 tlsData = GSSEAP_CALLOC(1, sizeof(*tlsData));
102 TlsSetValue(tlsIndex, tlsData);
104 case DLL_THREAD_DETACH:
105 /* Release the allocated memory for this thread. */
106 tlsData = TlsGetValue(tlsIndex);
107 if (tlsData != NULL) {
108 destroyThreadLocalData(tlsData);
109 TlsSetValue(tlsIndex, NULL);
112 case DLL_PROCESS_DETACH:
113 /* Release the TLS index. */
122 UNREFERENCED_PARAMETER(hDLL);
123 UNREFERENCED_PARAMETER(reserved);
128 /* pthreads implementation */
130 static GSSEAP_THREAD_ONCE tldKeyOnce = GSSEAP_ONCE_INITIALIZER;
131 static GSSEAP_THREAD_KEY tldKey;
134 pthreadDestroyThreadLocalData(void *arg)
136 struct gss_eap_thread_local_data* tld = arg;
139 destroyThreadLocalData(tld);
143 createThreadLocalDataKey(void)
145 GSSEAP_KEY_CREATE(&tldKey, pthreadDestroyThreadLocalData);
148 struct gss_eap_thread_local_data *
149 gssEapGetThreadLocalData()
151 struct gss_eap_thread_local_data *tld;
153 GSSEAP_ONCE(&tldKeyOnce, createThreadLocalDataKey);
155 tld = GSSEAP_GETSPECIFIC(tldKey);
157 tld = GSSEAP_CALLOC(1, sizeof(*tld));
161 GSSEAP_SETSPECIFIC(tldKey, tld);