global.c 7.58 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7

8
#include "global.h"
9

10
#include "alloc.h"
11
#include "hash.h"
12
#include "sysdir.h"
13
#include "filter.h"
14
#include "merge_driver.h"
15
#include "streams/registry.h"
16
#include "streams/mbedtls.h"
17
#include "streams/openssl.h"
18
#include "thread-utils.h"
19
#include "git2/global.h"
20
#include "transports/ssh.h"
21

22 23 24 25
#if defined(GIT_MSVC_CRTDBG)
#include "win32/w32_stack.h"
#include "win32/w32_crtdbg_stacktrace.h"
#endif
26 27 28

git_mutex git__mwindow_mutex;

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
typedef int (*git_global_init_fn)(void);

static git_global_init_fn git__init_callbacks[] = {
	git_allocator_global_init,
	git_hash_global_init,
	git_sysdir_global_init,
	git_filter_global_init,
	git_merge_driver_global_init,
	git_transport_ssh_global_init,
	git_stream_registry_global_init,
	git_openssl_stream_global_init,
	git_mbedtls_stream_global_init,
	git_mwindow_global_init
};

static git_global_shutdown_fn git__shutdown_callbacks[ARRAY_SIZE(git__init_callbacks)];
45

46 47
static git_atomic git__n_shutdown_callbacks;
static git_atomic git__n_inits;
48
char *git__user_agent;
49
char *git__ssl_ciphers;
50 51 52 53

void git__on_shutdown(git_global_shutdown_fn callback)
{
	int count = git_atomic_inc(&git__n_shutdown_callbacks);
54
	assert(count <= (int) ARRAY_SIZE(git__shutdown_callbacks) && count > 0);
55 56 57
	git__shutdown_callbacks[count - 1] = callback;
}

58 59 60 61 62 63 64 65 66
static void git__global_state_cleanup(git_global_st *st)
{
	if (!st)
		return;

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

67 68
static int init_common(void)
{
69
	size_t i;
70 71 72 73 74 75 76 77
	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

78 79 80 81
	/* Initialize subsystems that have global state */
	for (i = 0; i < ARRAY_SIZE(git__init_callbacks); i++)
		if ((ret = git__init_callbacks[i]()) != 0)
			break;
82 83 84 85 86 87 88

	GIT_MEMORY_BARRIER;

	return ret;
}

static void shutdown_common(void)
89 90 91
{
	int pos;

92
	/* Shutdown subsystems that have registered */
93 94 95 96 97 98 99
	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);

100 101 102
		if (cb != NULL)
			cb();
	}
103 104

	git__free(git__user_agent);
105
	git__free(git__ssl_ciphers);
106 107
}

108 109 110 111
/**
 * Handle the global state with TLS
 *
 * If libgit2 is built with GIT_THREADS enabled,
112
 * the `git_libgit2_init()` function must be called
113 114 115 116 117 118 119 120 121 122 123 124
 * 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
125
 * `git_libgit2_shutdown` method must be called to free
126 127 128 129 130 131 132 133
 * 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.
 */

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

144
static DWORD _fls_index;
145
static volatile LONG _mutex = 0;
146

147 148 149 150 151 152
static void WINAPI fls_free(void *st)
{
	git__global_state_cleanup(st);
	git__free(st);
}

153
static int synchronized_threads_init(void)
154
{
155 156
	int error;

157 158
	if ((_fls_index = FlsAlloc(fls_free)) == FLS_OUT_OF_INDEXES)
		return -1;
159

160
	git_threads_init();
161

Russell Belfer committed
162 163
	if (git_mutex_init(&git__mwindow_mutex))
		return -1;
164

165
	error = init_common();
166

167 168 169
	return error;
}

170
int git_libgit2_init(void)
171
{
172
	int ret;
173 174 175

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

177
	/* Only do work on a 0 -> 1 transition of the refcount */
178 179 180 181
	if ((ret = git_atomic_inc(&git__n_inits)) == 1) {
		if (synchronized_threads_init() < 0)
			ret = -1;
	}
182

183 184 185
	/* Exit the lock */
	InterlockedExchange(&_mutex, 0);

186
	return ret;
187 188
}

189
int git_libgit2_shutdown(void)
190
{
191 192
	int ret;

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

196
	/* Only do work on a 1 -> 0 transition of the refcount */
197
	if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
198
		shutdown_common();
199

200
		FlsFree(_fls_index);
201
		git_mutex_free(&git__mwindow_mutex);
202 203 204 205 206

#if defined(GIT_MSVC_CRTDBG)
		git_win32__crtdbg_stacktrace_cleanup();
		git_win32__stack_cleanup();
#endif
207 208
	}

209 210
	/* Exit the lock */
	InterlockedExchange(&_mutex, 0);
211 212

	return ret;
213 214 215 216
}

git_global_st *git__global_state(void)
{
217
	git_global_st *ptr;
218

219
	assert(git_atomic_get(&git__n_inits) > 0);
220

221
	if ((ptr = FlsGetValue(_fls_index)) != NULL)
222 223
		return ptr;

224
	ptr = git__calloc(1, sizeof(git_global_st));
225 226 227
	if (!ptr)
		return NULL;

228 229
	git_buf_init(&ptr->error_buf, 0);

230
	FlsSetValue(_fls_index, ptr);
231 232 233 234 235 236
	return ptr;
}

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

static pthread_key_t _tls_key;
237
static pthread_mutex_t _init_mutex = PTHREAD_MUTEX_INITIALIZER;
238 239
static pthread_once_t _once_init = PTHREAD_ONCE_INIT;
int init_error = 0;
240 241 242

static void cb__free_status(void *st)
{
243
	git__global_state_cleanup(st);
244
	git__free(st);
245 246
}

247
static void init_once(void)
248
{
249 250
	if ((init_error = git_mutex_init(&git__mwindow_mutex)) != 0)
		return;
251

252
	pthread_key_create(&_tls_key, &cb__free_status);
253

254
	init_error = init_common();
255
}
256

257
int git_libgit2_init(void)
258
{
259
	int ret, err;
260

261 262
	if ((err = pthread_mutex_lock(&_init_mutex)) != 0)
		return err;
263 264

	ret = git_atomic_inc(&git__n_inits);
265 266 267 268 269 270 271
	err = pthread_once(&_once_init, init_once);
	err |= pthread_mutex_unlock(&_init_mutex);

	if (err || init_error)
		return err | init_error;

	return ret;
272 273
}

274
int git_libgit2_shutdown(void)
275
{
276
	void *ptr = NULL;
277
	pthread_once_t new_once = PTHREAD_ONCE_INIT;
278
	int error, ret;
279

280 281
	if ((error = pthread_mutex_lock(&_init_mutex)) != 0)
		return error;
282

283 284
	if ((ret = git_atomic_dec(&git__n_inits)) != 0)
		goto out;
285

286
	/* Shut down any subsystems that have global state */
287
	shutdown_common();
288

289
	ptr = pthread_getspecific(_tls_key);
290
	pthread_setspecific(_tls_key, NULL);
291 292

	git__global_state_cleanup(ptr);
293
	git__free(ptr);
Russell Belfer committed
294

295
	pthread_key_delete(_tls_key);
296
	git_mutex_free(&git__mwindow_mutex);
297
	_once_init = new_once;
298

299 300 301
out:
	if ((error = pthread_mutex_unlock(&_init_mutex)) != 0)
		return error;
302

303
	return ret;
304 305 306 307
}

git_global_st *git__global_state(void)
{
308
	git_global_st *ptr;
309

310
	assert(git_atomic_get(&git__n_inits) > 0);
311

312 313 314
	if ((ptr = pthread_getspecific(_tls_key)) != NULL)
		return ptr;

315
	ptr = git__calloc(1, sizeof(git_global_st));
316 317 318
	if (!ptr)
		return NULL;

319
	git_buf_init(&ptr->error_buf, 0);
320 321 322 323 324 325 326 327
	pthread_setspecific(_tls_key, ptr);
	return ptr;
}

#else

static git_global_st __state;

328
int git_libgit2_init(void)
329
{
330
	int ret;
331

332
	/* Only init subsystems the first time */
333 334 335 336 337
	if ((ret = git_atomic_inc(&git__n_inits)) != 1)
		return ret;

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

339
	return 1;
340 341
}

342
int git_libgit2_shutdown(void)
343
{
344 345
	int ret;

346
	/* Shut down any subsystems that have global state */
347 348 349
	if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
		shutdown_common();
		git__global_state_cleanup(&__state);
350
		memset(&__state, 0, sizeof(__state));
351
	}
352

353
	return ret;
354 355 356 357 358 359 360 361
}

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

#endif /* GIT_THREADS */