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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
diff --git a/src/thread.c b/src/thread.c
new file mode 100644
index 0000000..d9ca374
--- /dev/null
+++ b/src/thread.c
@@ -0,0 +1,213 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "common.h"
+
+#ifndef GIT_THREADS
+
+struct git_tls_data {
+ void GIT_CALLBACK(free_fn)(void *payload);
+ void *storage;
+};
+
+int git_tls_data__init(git_tls_data **out,
+ void GIT_CALLBACK(free_fn)(void *storage))
+{
+ struct git_tls_data *tls = git__malloc(sizeof(struct git_tls_data));
+ GIT_ERROR_CHECK_ALLOC(tls);
+
+ tls->storage = NULL;
+ tls->free_fn = free_fn;
+ *out = tls;
+
+ return 0;
+}
+
+int git_tls_data__set(git_tls_data *tls, void *payload)
+{
+ tls->storage = payload;
+ return 0;
+}
+
+void *git_tls_data__get(git_tls_data *tls)
+{
+ return tls->storage;
+}
+
+void git_tls_data__free(git_tls_data *tls)
+{
+ tls->free_fn(tls->storage);
+ git__free(tls);
+}
+
+#elif defined(GIT_WIN32)
+
+struct git_tls_data {
+ void GIT_CALLBACK(free_fn)(void *payload);
+ DWORD fls_index;
+};
+
+struct git_tls_cell {
+ void GIT_CALLBACK(free_fn)(void *storage);
+ void *storage;
+};
+
+static void WINAPI git_tls_cell__free(void *sc)
+{
+ struct git_tls_cell *storage_cell = sc;
+ if (storage_cell == NULL) {
+ return;
+ }
+
+ storage_cell->free_fn(storage_cell->storage);
+ git__free(storage_cell);
+}
+
+int git_tls_data__init(git_tls_data **out,
+ void GIT_CALLBACK(free_fn)(void *payload))
+{
+ struct git_tls_data *tls = git__malloc(sizeof(struct git_tls_data));
+ GIT_ERROR_CHECK_ALLOC(tls);
+
+ if ((tls->fls_index = FlsAlloc(git_tls_cell__free)) == FLS_OUT_OF_INDEXES) {
+ git__free(tls);
+ return -1;
+ }
+
+ tls->free_fn = free_fn;
+ *out = tls;
+
+ return 0;
+}
+
+int git_tls_data__set(git_tls_data *tls, void *payload)
+{
+ struct git_tls_cell *storage_cell;
+
+ if (payload == NULL) {
+ if ((storage_cell = FlsGetValue(tls->fls_index)) != NULL)
+ git_tls_cell__free(storage_cell);
+
+ if (FlsSetValue(tls->fls_index, NULL) == 0)
+ return -1;
+
+ return 0;
+ }
+
+ storage_cell = git__malloc(sizeof(struct git_tls_cell));
+ GIT_ERROR_CHECK_ALLOC(storage_cell);
+
+ storage_cell->free_fn = tls->free_fn;
+ storage_cell->storage = payload;
+
+ if (FlsSetValue(tls->fls_index, storage_cell) == 0) {
+ git__free(storage_cell);
+ return -1;
+ }
+
+ return 0;
+}
+
+void *git_tls_data__get(git_tls_data *tls)
+{
+ struct git_tls_cell *storage_cell = FlsGetValue(tls->fls_index);
+ if (storage_cell == NULL)
+ return NULL;
+
+ return storage_cell->storage;
+}
+
+void git_tls_data__free(git_tls_data *tls)
+{
+ FlsFree(tls->fls_index);
+ tls->free_fn = NULL;
+ git__free(tls);
+}
+
+#elif defined(_POSIX_THREADS)
+
+struct git_tls_data {
+ void GIT_CALLBACK(free_fn)(void *payload);
+ pthread_key_t tls_key;
+};
+
+struct git_tls_cell {
+ void GIT_CALLBACK(free_fn)(void *storage);
+ void *storage;
+};
+
+static void git_tls_cell__free(void *sc)
+{
+ struct git_tls_cell *storage_cell = sc;
+ storage_cell->free_fn(storage_cell->storage);
+ git__free(storage_cell);
+}
+
+int git_tls_data__init(git_tls_data **out,
+ void GIT_CALLBACK(free_fn)(void *payload))
+{
+ struct git_tls_data *tls = git__malloc(sizeof(struct git_tls_data));
+ GIT_ERROR_CHECK_ALLOC(tls);
+
+ if (pthread_key_create(&tls->tls_key, git_tls_cell__free) != 0) {
+ git__free(tls);
+ return -1;
+ }
+
+ tls->free_fn = free_fn;
+ *out = tls;
+
+ return 0;
+}
+
+int git_tls_data__set(git_tls_data *tls, void *payload)
+{
+ struct git_tls_cell *storage_cell;
+
+ if (payload == NULL) {
+ if ((storage_cell = pthread_getspecific(tls->tls_key)) != NULL)
+ git_tls_cell__free(storage_cell);
+
+ if (pthread_setspecific(tls->tls_key, NULL) != 0)
+ return -1;
+
+ return 0;
+ }
+
+ storage_cell = git__malloc(sizeof(struct git_tls_cell));
+ GIT_ERROR_CHECK_ALLOC(storage_cell);
+
+ storage_cell->free_fn = tls->free_fn;
+ storage_cell->storage = payload;
+
+ if (pthread_setspecific(tls->tls_key, storage_cell) != 0) {
+ git__free(storage_cell);
+ return -1;
+ }
+
+ return 0;
+}
+
+void *git_tls_data__get(git_tls_data *tls)
+{
+ struct git_tls_cell *storage_cell = pthread_getspecific(tls->tls_key);
+ if (storage_cell == NULL)
+ return NULL;
+
+ return storage_cell->storage;
+}
+
+void git_tls_data__free(git_tls_data *tls)
+{
+ git_tls_data__set(tls, NULL);
+ pthread_key_delete(tls->tls_key);
+ git__free(tls);
+}
+
+#else
+# error unknown threading model
+#endif
diff --git a/src/thread.h b/src/thread.h
index 561e6ec..6cdf738 100644
--- a/src/thread.h
+++ b/src/thread.h
@@ -367,4 +367,52 @@ GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a)
#endif
+/**
+ * An opaque structure for managing TLS in the library
+ */
+typedef struct git_tls_data git_tls_data;
+
+/**
+ * Initializes a thread local storage container.
+ * This has an implementation even without GIT_THREADS
+ * which just serves to encourage use of this where TLS
+ * is necessary.
+ *
+ * Do not call this before the allocator has been initialized.
+ *
+ * @param out a pointer to store the TLS container in
+ * @param free_fn the method that should be called when
+ * deleting something in the TLS. Will be
+ * registered as the clean up callback for
+ * the OS specific TLS construct.
+ * @return 0 on success, non-zero on failure
+ */
+int git_tls_data__init(git_tls_data **out,
+ void GIT_CALLBACK(free_fn)(void *payload));
+
+/**
+ * Will set a thread specific value on the TLS. Passing NULL will free the
+ * currently held thread specific value.
+ *
+ * @param tls the TLS instance to store data on
+ * @param payload the pointer to store
+ * @return 0 on success, non-zero on failure
+ */
+int git_tls_data__set(git_tls_data *tls, void *payload);
+
+/**
+ * Will get the thread specific value stored in TLS.
+ *
+ * @param tls the TLS instance to retrieve data from
+ */
+void *git_tls_data__get(git_tls_data *tls);
+
+/**
+ * Must call this to clean up the TLS when no longer in use.
+ * The TLS pointer is unusable after a call to this.
+ *
+ * @param tls the TLS to free
+ */
+void git_tls_data__free(git_tls_data *tls);
+
#endif