|
| 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 |
0 commit comments