Hash :
971b7187
Author :
Date :
2020-08-06T16:24:18
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
/*
* 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