global.c 6.98 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 12
#include "filter.h"
#include "openssl_stream.h"
13
#include "thread-utils.h"
14
#include "git2/global.h"
15
#include "transports/ssh.h"
16

17 18 19 20
#if defined(GIT_MSVC_CRTDBG)
#include "win32/w32_stack.h"
#include "win32/w32_crtdbg_stacktrace.h"
#endif
21 22 23

git_mutex git__mwindow_mutex;

24 25
#define MAX_SHUTDOWN_CB 8

26 27 28
static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
static git_atomic git__n_shutdown_callbacks;
static git_atomic git__n_inits;
29
char *git__user_agent;
30
char *git__ssl_ciphers;
31 32 33 34

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

39 40 41 42 43 44 45 46 47
static void git__global_state_cleanup(git_global_st *st)
{
	if (!st)
		return;

	git__free(st->error_t.message);
	st->error_t.message = NULL;
}

48 49 50 51 52 53 54 55 56 57 58 59 60
static int init_common(void)
{
	int ret;

	/* Initialize the CRT debug allocator first, before our first malloc */
#if defined(GIT_MSVC_CRTDBG)
	git_win32__crtdbg_stacktrace_init();
	git_win32__stack_init();
#endif

	/* Initialize any other subsystems that have global state */
	if ((ret = git_hash_global_init()) == 0 &&
		(ret = git_sysdir_global_init()) == 0 &&
61 62
		(ret = git_filter_global_init()) == 0 &&
		(ret = git_transport_ssh_global_init()) == 0)
63
		ret = git_openssl_stream_global_init();
64 65 66 67 68 69 70

	GIT_MEMORY_BARRIER;

	return ret;
}

static void shutdown_common(void)
71 72 73
{
	int pos;

74
	/* Shutdown subsystems that have registered */
75 76 77 78 79 80 81
	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);

82 83 84
		if (cb != NULL)
			cb();
	}
85 86

	git__free(git__user_agent);
87
	git__free(git__ssl_ciphers);
88 89 90 91 92

#if defined(GIT_MSVC_CRTDBG)
	git_win32__crtdbg_stacktrace_cleanup();
	git_win32__stack_cleanup();
#endif
93 94
}

95 96 97 98
/**
 * Handle the global state with TLS
 *
 * If libgit2 is built with GIT_THREADS enabled,
99
 * the `git_libgit2_init()` function must be called
100 101 102 103 104 105 106 107 108 109 110 111
 * 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
112
 * `git_libgit2_shutdown` method must be called to free
113 114 115 116 117 118 119 120
 * 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.
 */

121
/*
122
 * `git_libgit2_init()` allows subsystems to perform global setup,
123
 * which may take place in the global scope.  An explicit memory
124
 * fence exists at the exit of `git_libgit2_init()`.  Without this,
125 126 127 128
 * CPU cores are free to reorder cache invalidation of `_tls_init`
 * before cache invalidation of the subsystems' newly written global
 * state.
 */
129 130 131
#if defined(GIT_THREADS) && defined(GIT_WIN32)

static DWORD _tls_index;
132
static volatile LONG _mutex = 0;
133

134
static int synchronized_threads_init(void)
135
{
136 137
	int error;

138
	_tls_index = TlsAlloc();
139 140 141

	win32_pthread_initialize();

Russell Belfer committed
142 143
	if (git_mutex_init(&git__mwindow_mutex))
		return -1;
144

145
	error = init_common();
146

147 148 149
	return error;
}

150
int git_libgit2_init(void)
151
{
152
	int ret;
153 154 155

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

157
	/* Only do work on a 0 -> 1 transition of the refcount */
158 159 160 161
	if ((ret = git_atomic_inc(&git__n_inits)) == 1) {
		if (synchronized_threads_init() < 0)
			ret = -1;
	}
162

163 164 165
	/* Exit the lock */
	InterlockedExchange(&_mutex, 0);

166
	return ret;
167 168
}

169
int git_libgit2_shutdown(void)
170
{
171 172
	int ret;

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

176
	/* Only do work on a 1 -> 0 transition of the refcount */
177
	if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
178
		shutdown_common();
179

180
		git__free_tls_data();
181

182 183
		TlsFree(_tls_index);
		git_mutex_free(&git__mwindow_mutex);
184 185
	}

186 187
	/* Exit the lock */
	InterlockedExchange(&_mutex, 0);
188 189

	return ret;
190 191 192 193
}

git_global_st *git__global_state(void)
{
194
	git_global_st *ptr;
195

196
	assert(git_atomic_get(&git__n_inits) > 0);
197

198 199 200
	if ((ptr = TlsGetValue(_tls_index)) != NULL)
		return ptr;

201
	ptr = git__calloc(1, sizeof(git_global_st));
202 203 204
	if (!ptr)
		return NULL;

205 206
	git_buf_init(&ptr->error_buf, 0);

207 208 209 210
	TlsSetValue(_tls_index, ptr);
	return ptr;
}

211 212 213 214 215 216
/**
 * Free the TLS data associated with this thread.
 * This should only be used by the thread as it
 * is exiting.
 */
void git__free_tls_data(void)
217 218 219 220 221 222 223 224 225 226
{
	void *ptr = TlsGetValue(_tls_index);
	if (!ptr)
		return;

	git__global_state_cleanup(ptr);
	git__free(ptr);
	TlsSetValue(_tls_index, NULL);
}

227 228 229
#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)

static pthread_key_t _tls_key;
230 231
static pthread_once_t _once_init = PTHREAD_ONCE_INIT;
int init_error = 0;
232 233 234

static void cb__free_status(void *st)
{
235
	git__global_state_cleanup(st);
236
	git__free(st);
237 238
}

239
static void init_once(void)
240
{
241 242
	if ((init_error = git_mutex_init(&git__mwindow_mutex)) != 0)
		return;
243

244
	pthread_key_create(&_tls_key, &cb__free_status);
245

246
	init_error = init_common();
247
}
248

249
int git_libgit2_init(void)
250
{
251 252 253
	int ret;

	ret = git_atomic_inc(&git__n_inits);
254
	pthread_once(&_once_init, init_once);
255 256

	return init_error ? init_error : ret;
257 258
}

259
int git_libgit2_shutdown(void)
260
{
261
	void *ptr = NULL;
262
	pthread_once_t new_once = PTHREAD_ONCE_INIT;
263
	int ret;
264

265
	if ((ret = git_atomic_dec(&git__n_inits)) != 0)
266
		return ret;
267

268
	/* Shut down any subsystems that have global state */
269
	shutdown_common();
270

271
	ptr = pthread_getspecific(_tls_key);
272
	pthread_setspecific(_tls_key, NULL);
273 274

	git__global_state_cleanup(ptr);
275
	git__free(ptr);
Russell Belfer committed
276

277
	pthread_key_delete(_tls_key);
278
	git_mutex_free(&git__mwindow_mutex);
279
	_once_init = new_once;
280

281
	return 0;
282 283 284 285
}

git_global_st *git__global_state(void)
{
286
	git_global_st *ptr;
287

288
	assert(git_atomic_get(&git__n_inits) > 0);
289

290 291 292
	if ((ptr = pthread_getspecific(_tls_key)) != NULL)
		return ptr;

293
	ptr = git__calloc(1, sizeof(git_global_st));
294 295 296
	if (!ptr)
		return NULL;

297
	git_buf_init(&ptr->error_buf, 0);
298 299 300 301 302 303 304 305
	pthread_setspecific(_tls_key, ptr);
	return ptr;
}

#else

static git_global_st __state;

306
int git_libgit2_init(void)
307
{
308
	int ret;
309

310 311 312 313 314 315
	/* Only init SSL the first time */
	if ((ret = git_atomic_inc(&git__n_inits)) != 1)
		return ret;

	if ((ret = init_common()) < 0)
		return ret;
316

317
	return 1;
318 319
}

320
int git_libgit2_shutdown(void)
321
{
322 323
	int ret;

324
	/* Shut down any subsystems that have global state */
325 326 327 328
	if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
		shutdown_common();
		git__global_state_cleanup(&__state);
	}
329

330
	return ret;
331 332 333 334 335 336 337 338
}

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

#endif /* GIT_THREADS */