Skip to content

Commit 3baef46

Browse files
author
Andi Gutmans
committed
- Also cache TSRM lookups in thread local storage on Windows.
1 parent b0224d5 commit 3baef46

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

TSRM/TSRM.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ static FILE *tsrm_error_file;
9494
static pthread_key_t tls_key;
9595
#elif defined(TSRM_ST)
9696
static int tls_key;
97+
#elif defined(TSRM_WIN32)
98+
static DWORD tls_key;
9799
#endif
98100

99101

@@ -107,6 +109,8 @@ TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debu
107109
#elif defined(TSRM_ST)
108110
st_init();
109111
st_key_create(&tls_key, 0);
112+
#elif defined(TSRM_WIN32)
113+
tls_key = TlsAlloc();
110114
#endif
111115

112116
tsrm_error_file = stderr;
@@ -175,7 +179,9 @@ TSRM_API void tsrm_shutdown(void)
175179
#if defined(GNUPTH)
176180
pth_kill();
177181
#elif defined(PTHREADS)
178-
pthread_key_delete( tls_key );
182+
pthread_key_delete(tls_key);
183+
#elif defined(TSRM_WIN32)
184+
TlsFree(tls_key);
179185
#endif
180186
}
181187

@@ -248,9 +254,11 @@ static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_
248254

249255
#if defined(PTHREADS)
250256
/* Set thread local storage to this new thread resources structure */
251-
pthread_setspecific( tls_key, (void *)*thread_resources_ptr );
257+
pthread_setspecific(tls_key, (void *) *thread_resources_ptr);
252258
#elif defined(TSRM_ST)
253259
st_thread_setspecific(tls_key, (void *) *thread_resources_ptr);
260+
#elif defined(TSRM_WIN32)
261+
TlsSetValue(tls_key, (void *) *thread_resources_ptr);
254262
#endif
255263

256264
if (tsrm_new_thread_begin_handler) {
@@ -288,6 +296,8 @@ TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id)
288296
thread_resources = pthread_getspecific(tls_key);
289297
#elif defined(TSRM_ST)
290298
thread_resources = st_thread_getspecific(tls_key);
299+
#elif defined(TSRM_WIN32)
300+
thread_resources = TlsGetValue(tls_key);
291301
#else
292302
thread_resources = NULL;
293303
#endif
@@ -370,6 +380,8 @@ void ts_free_thread(void)
370380
}
371381
#if defined(PTHREADS)
372382
pthread_setspecific(tls_key, 0);
383+
#elif defined(TSRM_WIN32)
384+
TlsSetValue(tls_key, 0);
373385
#endif
374386
free(thread_resources);
375387
break;

0 commit comments

Comments
 (0)