Skip to content

Commit 971b718

Browse files
implausibleethomson
authored andcommitted
Implement generic TLS interface
This adds a generic TLS interface for anyone to store TLS data. It is designed to work regardless of whether threading support is built into the library or not. Nobody in the library should directly interface with the data on the TLS struct, so it's been built to be opaque even in the library. Requires the allocator to be initialized before use.
1 parent 404dd02 commit 971b718

File tree

2 files changed

+261
-0
lines changed

2 files changed

+261
-0
lines changed

src/thread.c

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include "common.h"
9+
10+
#ifndef GIT_THREADS
11+
12+
struct git_tls_data {
13+
void GIT_CALLBACK(free_fn)(void *payload);
14+
void *storage;
15+
};
16+
17+
int git_tls_data__init(git_tls_data **out,
18+
void GIT_CALLBACK(free_fn)(void *storage))
19+
{
20+
struct git_tls_data *tls = git__malloc(sizeof(struct git_tls_data));
21+
GIT_ERROR_CHECK_ALLOC(tls);
22+
23+
tls->storage = NULL;
24+
tls->free_fn = free_fn;
25+
*out = tls;
26+
27+
return 0;
28+
}
29+
30+
int git_tls_data__set(git_tls_data *tls, void *payload)
31+
{
32+
tls->storage = payload;
33+
return 0;
34+
}
35+
36+
void *git_tls_data__get(git_tls_data *tls)
37+
{
38+
return tls->storage;
39+
}
40+
41+
void git_tls_data__free(git_tls_data *tls)
42+
{
43+
tls->free_fn(tls->storage);
44+
git__free(tls);
45+
}
46+
47+
#elif defined(GIT_WIN32)
48+
49+
struct git_tls_data {
50+
void GIT_CALLBACK(free_fn)(void *payload);
51+
DWORD fls_index;
52+
};
53+
54+
struct git_tls_cell {
55+
void GIT_CALLBACK(free_fn)(void *storage);
56+
void *storage;
57+
};
58+
59+
static void WINAPI git_tls_cell__free(void *sc)
60+
{
61+
struct git_tls_cell *storage_cell = sc;
62+
if (storage_cell == NULL) {
63+
return;
64+
}
65+
66+
storage_cell->free_fn(storage_cell->storage);
67+
git__free(storage_cell);
68+
}
69+
70+
int git_tls_data__init(git_tls_data **out,
71+
void GIT_CALLBACK(free_fn)(void *payload))
72+
{
73+
struct git_tls_data *tls = git__malloc(sizeof(struct git_tls_data));
74+
GIT_ERROR_CHECK_ALLOC(tls);
75+
76+
if ((tls->fls_index = FlsAlloc(git_tls_cell__free)) == FLS_OUT_OF_INDEXES) {
77+
git__free(tls);
78+
return -1;
79+
}
80+
81+
tls->free_fn = free_fn;
82+
*out = tls;
83+
84+
return 0;
85+
}
86+
87+
int git_tls_data__set(git_tls_data *tls, void *payload)
88+
{
89+
struct git_tls_cell *storage_cell;
90+
91+
if (payload == NULL) {
92+
if ((storage_cell = FlsGetValue(tls->fls_index)) != NULL)
93+
git_tls_cell__free(storage_cell);
94+
95+
if (FlsSetValue(tls->fls_index, NULL) == 0)
96+
return -1;
97+
98+
return 0;
99+
}
100+
101+
storage_cell = git__malloc(sizeof(struct git_tls_cell));
102+
GIT_ERROR_CHECK_ALLOC(storage_cell);
103+
104+
storage_cell->free_fn = tls->free_fn;
105+
storage_cell->storage = payload;
106+
107+
if (FlsSetValue(tls->fls_index, storage_cell) == 0) {
108+
git__free(storage_cell);
109+
return -1;
110+
}
111+
112+
return 0;
113+
}
114+
115+
void *git_tls_data__get(git_tls_data *tls)
116+
{
117+
struct git_tls_cell *storage_cell = FlsGetValue(tls->fls_index);
118+
if (storage_cell == NULL)
119+
return NULL;
120+
121+
return storage_cell->storage;
122+
}
123+
124+
void git_tls_data__free(git_tls_data *tls)
125+
{
126+
FlsFree(tls->fls_index);
127+
tls->free_fn = NULL;
128+
git__free(tls);
129+
}
130+
131+
#elif defined(_POSIX_THREADS)
132+
133+
struct git_tls_data {
134+
void GIT_CALLBACK(free_fn)(void *payload);
135+
pthread_key_t tls_key;
136+
};
137+
138+
struct git_tls_cell {
139+
void GIT_CALLBACK(free_fn)(void *storage);
140+
void *storage;
141+
};
142+
143+
static void git_tls_cell__free(void *sc)
144+
{
145+
struct git_tls_cell *storage_cell = sc;
146+
storage_cell->free_fn(storage_cell->storage);
147+
git__free(storage_cell);
148+
}
149+
150+
int git_tls_data__init(git_tls_data **out,
151+
void GIT_CALLBACK(free_fn)(void *payload))
152+
{
153+
struct git_tls_data *tls = git__malloc(sizeof(struct git_tls_data));
154+
GIT_ERROR_CHECK_ALLOC(tls);
155+
156+
if (pthread_key_create(&tls->tls_key, git_tls_cell__free) != 0) {
157+
git__free(tls);
158+
return -1;
159+
}
160+
161+
tls->free_fn = free_fn;
162+
*out = tls;
163+
164+
return 0;
165+
}
166+
167+
int git_tls_data__set(git_tls_data *tls, void *payload)
168+
{
169+
struct git_tls_cell *storage_cell;
170+
171+
if (payload == NULL) {
172+
if ((storage_cell = pthread_getspecific(tls->tls_key)) != NULL)
173+
git_tls_cell__free(storage_cell);
174+
175+
if (pthread_setspecific(tls->tls_key, NULL) != 0)
176+
return -1;
177+
178+
return 0;
179+
}
180+
181+
storage_cell = git__malloc(sizeof(struct git_tls_cell));
182+
GIT_ERROR_CHECK_ALLOC(storage_cell);
183+
184+
storage_cell->free_fn = tls->free_fn;
185+
storage_cell->storage = payload;
186+
187+
if (pthread_setspecific(tls->tls_key, storage_cell) != 0) {
188+
git__free(storage_cell);
189+
return -1;
190+
}
191+
192+
return 0;
193+
}
194+
195+
void *git_tls_data__get(git_tls_data *tls)
196+
{
197+
struct git_tls_cell *storage_cell = pthread_getspecific(tls->tls_key);
198+
if (storage_cell == NULL)
199+
return NULL;
200+
201+
return storage_cell->storage;
202+
}
203+
204+
void git_tls_data__free(git_tls_data *tls)
205+
{
206+
git_tls_data__set(tls, NULL);
207+
pthread_key_delete(tls->tls_key);
208+
git__free(tls);
209+
}
210+
211+
#else
212+
# error unknown threading model
213+
#endif

src/thread.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,52 @@ GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a)
367367

368368
#endif
369369

370+
/**
371+
* An opaque structure for managing TLS in the library
372+
*/
373+
typedef struct git_tls_data git_tls_data;
374+
375+
/**
376+
* Initializes a thread local storage container.
377+
* This has an implementation even without GIT_THREADS
378+
* which just serves to encourage use of this where TLS
379+
* is necessary.
380+
*
381+
* Do not call this before the allocator has been initialized.
382+
*
383+
* @param out a pointer to store the TLS container in
384+
* @param free_fn the method that should be called when
385+
* deleting something in the TLS. Will be
386+
* registered as the clean up callback for
387+
* the OS specific TLS construct.
388+
* @return 0 on success, non-zero on failure
389+
*/
390+
int git_tls_data__init(git_tls_data **out,
391+
void GIT_CALLBACK(free_fn)(void *payload));
392+
393+
/**
394+
* Will set a thread specific value on the TLS. Passing NULL will free the
395+
* currently held thread specific value.
396+
*
397+
* @param tls the TLS instance to store data on
398+
* @param payload the pointer to store
399+
* @return 0 on success, non-zero on failure
400+
*/
401+
int git_tls_data__set(git_tls_data *tls, void *payload);
402+
403+
/**
404+
* Will get the thread specific value stored in TLS.
405+
*
406+
* @param tls the TLS instance to retrieve data from
407+
*/
408+
void *git_tls_data__get(git_tls_data *tls);
409+
410+
/**
411+
* Must call this to clean up the TLS when no longer in use.
412+
* The TLS pointer is unusable after a call to this.
413+
*
414+
* @param tls the TLS to free
415+
*/
416+
void git_tls_data__free(git_tls_data *tls);
417+
370418
#endif

0 commit comments

Comments
 (0)