global.c 6.93 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8
 *
 * 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"
#include "global.h"
9
#include "hash.h"
10
#include "sysdir.h"
11
#include "git2/threads.h"
12 13
#include "thread-utils.h"

14 15 16

git_mutex git__mwindow_mutex;

17 18
#define MAX_SHUTDOWN_CB 8

19 20 21
#ifdef GIT_SSL
# include <openssl/ssl.h>
SSL_CTX *git__ssl_ctx;
22
static git_mutex *openssl_locks;
23 24
#endif

25 26 27
static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
static git_atomic git__n_shutdown_callbacks;
static git_atomic git__n_inits;
28 29 30 31

void git__on_shutdown(git_global_shutdown_fn callback)
{
	int count = git_atomic_inc(&git__n_shutdown_callbacks);
32
	assert(count <= MAX_SHUTDOWN_CB && count > 0);
33 34 35 36 37 38 39
	git__shutdown_callbacks[count - 1] = callback;
}

static void git__shutdown(void)
{
	int pos;

40 41 42 43 44
	for (pos = git_atomic_get(&git__n_shutdown_callbacks); pos > 0; pos = git_atomic_dec(&git__n_shutdown_callbacks)) {
		git_global_shutdown_fn cb = git__swap(git__shutdown_callbacks[pos - 1], NULL);
		if (cb != NULL)
			cb();
	}
45

46 47
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#if defined(GIT_THREADS) && defined(GIT_SSL)
void openssl_locking_function(int mode, int n, const char *file, int line)
{
	int lock;

	GIT_UNUSED(file);
	GIT_UNUSED(line);

	lock = mode & CRYPTO_LOCK;

	if (lock) {
		git_mutex_lock(&openssl_locks[n]);
	} else {
		git_mutex_unlock(&openssl_locks[n]);
	}
}
#endif


67 68 69 70 71 72
static void init_ssl(void)
{
#ifdef GIT_SSL
	SSL_load_error_strings();
	OpenSSL_add_ssl_algorithms();
	git__ssl_ctx = SSL_CTX_new(SSLv23_method());
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
	SSL_CTX_set_mode(git__ssl_ctx, SSL_MODE_AUTO_RETRY);
	SSL_CTX_set_verify(git__ssl_ctx, SSL_VERIFY_NONE, NULL);
	if (!SSL_CTX_set_default_verify_paths(git__ssl_ctx)) {
		SSL_CTX_free(git__ssl_ctx);
		git__ssl_ctx = NULL;
	}

# ifdef GIT_THREADS
	{
		int num_locks, i;

		num_locks = CRYPTO_num_locks();
		openssl_locks = git__calloc(num_locks, sizeof(git_mutex));
		if (openssl_locks == NULL) {
			SSL_CTX_free(git__ssl_ctx);
			git__ssl_ctx = NULL;
		}

		for (i = 0; i < num_locks; i++) {
			if (git_mutex_init(&openssl_locks[i]) != 0) {
				SSL_CTX_free(git__ssl_ctx);
				git__ssl_ctx = NULL;
			}
		}

		CRYPTO_set_locking_callback(openssl_locking_function);
	}
# endif
101 102 103
#endif
}

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
/**
 * Handle the global state with TLS
 *
 * If libgit2 is built with GIT_THREADS enabled,
 * the `git_threads_init()` function must be called
 * before calling any other function of the library.
 *
 * This function allocates a TLS index (using pthreads
 * or the native Win32 API) to store the global state
 * on a per-thread basis.
 *
 * Any internal method that requires global state will
 * then call `git__global_state()` which returns a pointer
 * to the global state structure; this pointer is lazily
 * allocated on each thread.
 *
 * Before shutting down the library, the
 * `git_threads_shutdown` method must be called to free
 * the previously reserved TLS index.
 *
 * If libgit2 is built without threading support, the
 * `git__global_statestate()` call returns a pointer to a single,
 * statically allocated global state. The `git_thread_`
 * functions are not available in that case.
 */

130 131 132 133 134 135 136 137
/*
 * `git_threads_init()` allows subsystems to perform global setup,
 * which may take place in the global scope.  An explicit memory
 * fence exists at the exit of `git_threads_init()`.  Without this,
 * CPU cores are free to reorder cache invalidation of `_tls_init`
 * before cache invalidation of the subsystems' newly written global
 * state.
 */
138 139 140
#if defined(GIT_THREADS) && defined(GIT_WIN32)

static DWORD _tls_index;
141
static volatile LONG _mutex = 0;
142

143
static int synchronized_threads_init(void)
144
{
145 146
	int error;

147
	_tls_index = TlsAlloc();
Russell Belfer committed
148 149
	if (git_mutex_init(&git__mwindow_mutex))
		return -1;
150 151

	/* Initialize any other subsystems that have global state */
152
	if ((error = git_hash_global_init()) >= 0)
153
		error = git_sysdir_global_init();
154

155
	win32_pthread_initialize();
156

157 158 159 160 161
	return error;
}

int git_threads_init(void)
{
162 163 164 165
	int error = 0;

	/* Enter the lock */
	while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
166

167
	/* Only do work on a 0 -> 1 transition of the refcount */
168
	if (1 == git_atomic_inc(&git__n_inits))
169
		error = synchronized_threads_init();
170

171 172 173
	/* Exit the lock */
	InterlockedExchange(&_mutex, 0);

174
	return error;
175 176
}

177
static void synchronized_threads_shutdown(void)
178
{
179
	/* Shut down any subsystems that have global state */
180
	git__shutdown();
181
	TlsFree(_tls_index);
182
	git_mutex_free(&git__mwindow_mutex);
183 184 185 186
}

void git_threads_shutdown(void)
{
187 188
	/* Enter the lock */
	while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
189

190
	/* Only do work on a 1 -> 0 transition of the refcount */
191
	if (0 == git_atomic_dec(&git__n_inits))
192
		synchronized_threads_shutdown();
193 194 195

	/* Exit the lock */
	InterlockedExchange(&_mutex, 0);
196 197 198 199 200 201
}

git_global_st *git__global_state(void)
{
	void *ptr;

202
	assert(git_atomic_get(&git__n_inits) > 0);
203

204 205 206
	if ((ptr = TlsGetValue(_tls_index)) != NULL)
		return ptr;

207
	ptr = git__malloc(sizeof(git_global_st));
208 209 210 211 212 213 214 215 216 217 218
	if (!ptr)
		return NULL;

	memset(ptr, 0x0, sizeof(git_global_st));
	TlsSetValue(_tls_index, ptr);
	return ptr;
}

#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)

static pthread_key_t _tls_key;
219 220
static pthread_once_t _once_init = PTHREAD_ONCE_INIT;
int init_error = 0;
221 222 223

static void cb__free_status(void *st)
{
224
	git__free(st);
225 226
}

227
static void init_once(void)
228
{
229 230
	if ((init_error = git_mutex_init(&git__mwindow_mutex)) != 0)
		return;
231
	pthread_key_create(&_tls_key, &cb__free_status);
232

233

234
	/* Initialize any other subsystems that have global state */
235
	if ((init_error = git_hash_global_init()) >= 0)
236
		init_error = git_sysdir_global_init();
237

238 239 240
	/* OpenSSL needs to be initialized from the main thread */
	init_ssl();

241
	GIT_MEMORY_BARRIER;
242
}
243

244 245 246 247 248
int git_threads_init(void)
{
	pthread_once(&_once_init, init_once);
	git_atomic_inc(&git__n_inits);
	return init_error;
249 250 251 252
}

void git_threads_shutdown(void)
{
253
	void *ptr = NULL;
254 255 256 257
	pthread_once_t new_once = PTHREAD_ONCE_INIT;

	if (git_atomic_dec(&git__n_inits) > 0) return;

258 259 260
	/* Shut down any subsystems that have global state */
	git__shutdown();

261
	ptr = pthread_getspecific(_tls_key);
262 263
	pthread_setspecific(_tls_key, NULL);
	git__free(ptr);
Russell Belfer committed
264

265
	pthread_key_delete(_tls_key);
266
	git_mutex_free(&git__mwindow_mutex);
267
	_once_init = new_once;
268 269 270 271 272 273
}

git_global_st *git__global_state(void)
{
	void *ptr;

274
	assert(git_atomic_get(&git__n_inits) > 0);
275

276 277 278
	if ((ptr = pthread_getspecific(_tls_key)) != NULL)
		return ptr;

279
	ptr = git__malloc(sizeof(git_global_st));
280 281 282 283 284 285 286 287 288 289 290 291
	if (!ptr)
		return NULL;

	memset(ptr, 0x0, sizeof(git_global_st));
	pthread_setspecific(_tls_key, ptr);
	return ptr;
}

#else

static git_global_st __state;

292
int git_threads_init(void)
293
{
294 295 296 297 298 299 300
	static int ssl_inited = 0;

	if (!ssl_inited) {
		init_ssl();
		ssl_inited = 1;
	}

301
	git_atomic_inc(&git__n_inits);
302
	return 0;
303 304 305 306
}

void git_threads_shutdown(void)
{
307
	/* Shut down any subsystems that have global state */
308 309
	if (0 == git_atomic_dec(&git__n_inits))
		git__shutdown();
310 311 312 313 314 315 316 317
}

git_global_st *git__global_state(void)
{
	return &__state;
}

#endif /* GIT_THREADS */