global.c 7.85 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 "hash.h"
11
#include "sysdir.h"
12
#include "filter.h"
13
#include "merge_driver.h"
14
#include "streams/openssl.h"
15
#include "thread-utils.h"
16
#include "git2/global.h"
17
#include "transports/ssh.h"
18

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

git_mutex git__mwindow_mutex;

26
#define MAX_SHUTDOWN_CB 9
27

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

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

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

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

50 51 52 53 54 55 56 57 58 59 60 61 62
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 &&
63
		(ret = git_filter_global_init()) == 0 &&
64
		(ret = git_merge_driver_global_init()) == 0 &&
65 66 67
		(ret = git_transport_ssh_global_init()) == 0 &&
		(ret = git_openssl_stream_global_init()) == 0)
		ret = git_mwindow_global_init();
68 69 70 71 72 73 74

	GIT_MEMORY_BARRIER;

	return ret;
}

static void shutdown_common(void)
75 76 77
{
	int pos;

78
	/* Shutdown subsystems that have registered */
79 80 81 82 83 84 85
	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);

86 87 88
		if (cb != NULL)
			cb();
	}
89 90

	git__free(git__user_agent);
91
	git__free(git__ssl_ciphers);
92 93
}

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

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

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

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

137
	_tls_index = TlsAlloc();
138

139
	git_threads_init();
140

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

144
	error = init_common();
145

146 147 148
	return error;
}

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

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

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

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

165
	return ret;
166 167
}

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

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

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

179
		git__free_tls_data();
180

181 182
		TlsFree(_tls_index);
		git_mutex_free(&git__mwindow_mutex);
183 184 185 186 187

#if defined(GIT_MSVC_CRTDBG)
		git_win32__crtdbg_stacktrace_cleanup();
		git_win32__stack_cleanup();
#endif
188 189
	}

190 191
	/* Exit the lock */
	InterlockedExchange(&_mutex, 0);
192 193

	return ret;
194 195 196 197
}

git_global_st *git__global_state(void)
{
198
	git_global_st *ptr;
199

200
	assert(git_atomic_get(&git__n_inits) > 0);
201

202 203 204
	if ((ptr = TlsGetValue(_tls_index)) != NULL)
		return ptr;

205
	ptr = git__calloc(1, sizeof(git_global_st));
206 207 208
	if (!ptr)
		return NULL;

209 210
	git_buf_init(&ptr->error_buf, 0);

211 212 213 214
	TlsSetValue(_tls_index, ptr);
	return ptr;
}

215 216 217 218 219 220
/**
 * 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)
221 222 223 224 225 226 227 228 229 230
{
	void *ptr = TlsGetValue(_tls_index);
	if (!ptr)
		return;

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

231 232
BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
{
233 234 235
	GIT_UNUSED(hInstDll);
	GIT_UNUSED(lpvReserved);

236 237 238 239 240 241 242 243 244 245 246 247
	/* This is how Windows lets us know our thread is being shut down */
	if (fdwReason == DLL_THREAD_DETACH) {
		git__free_tls_data();
	}

	/*
	 * Windows pays attention to this during library loading. We don't do anything
	 * so we trivially succeed.
	 */
	return TRUE;
}

248 249 250
#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)

static pthread_key_t _tls_key;
251
static pthread_mutex_t _init_mutex = PTHREAD_MUTEX_INITIALIZER;
252 253
static pthread_once_t _once_init = PTHREAD_ONCE_INIT;
int init_error = 0;
254 255 256

static void cb__free_status(void *st)
{
257
	git__global_state_cleanup(st);
258
	git__free(st);
259 260
}

261
static void init_once(void)
262
{
263 264
	if ((init_error = git_mutex_init(&git__mwindow_mutex)) != 0)
		return;
265

266
	pthread_key_create(&_tls_key, &cb__free_status);
267

268
	init_error = init_common();
269
}
270

271
int git_libgit2_init(void)
272
{
273
	int ret, err;
274 275 276

	ret = git_atomic_inc(&git__n_inits);

277 278 279 280 281 282 283 284 285
	if ((err = pthread_mutex_lock(&_init_mutex)) != 0)
		return err;
	err = pthread_once(&_once_init, init_once);
	err |= pthread_mutex_unlock(&_init_mutex);

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

	return ret;
286 287
}

288
int git_libgit2_shutdown(void)
289
{
290
	void *ptr = NULL;
291
	pthread_once_t new_once = PTHREAD_ONCE_INIT;
292
	int ret;
293

294
	if ((ret = git_atomic_dec(&git__n_inits)) != 0)
295
		return ret;
296

297 298 299
	if ((ret = pthread_mutex_lock(&_init_mutex)) != 0)
		return ret;

300
	/* Shut down any subsystems that have global state */
301
	shutdown_common();
302

303
	ptr = pthread_getspecific(_tls_key);
304
	pthread_setspecific(_tls_key, NULL);
305 306

	git__global_state_cleanup(ptr);
307
	git__free(ptr);
Russell Belfer committed
308

309
	pthread_key_delete(_tls_key);
310
	git_mutex_free(&git__mwindow_mutex);
311
	_once_init = new_once;
312

313 314 315
	if ((ret = pthread_mutex_unlock(&_init_mutex)) != 0)
		return ret;

316
	return 0;
317 318 319 320
}

git_global_st *git__global_state(void)
{
321
	git_global_st *ptr;
322

323
	assert(git_atomic_get(&git__n_inits) > 0);
324

325 326 327
	if ((ptr = pthread_getspecific(_tls_key)) != NULL)
		return ptr;

328
	ptr = git__calloc(1, sizeof(git_global_st));
329 330 331
	if (!ptr)
		return NULL;

332
	git_buf_init(&ptr->error_buf, 0);
333 334 335 336 337 338 339 340
	pthread_setspecific(_tls_key, ptr);
	return ptr;
}

#else

static git_global_st __state;

341
int git_libgit2_init(void)
342
{
343
	int ret;
344

345
	/* Only init subsystems the first time */
346 347 348 349 350
	if ((ret = git_atomic_inc(&git__n_inits)) != 1)
		return ret;

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

352
	return 1;
353 354
}

355
int git_libgit2_shutdown(void)
356
{
357 358
	int ret;

359
	/* Shut down any subsystems that have global state */
360 361 362
	if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
		shutdown_common();
		git__global_state_cleanup(&__state);
363
		memset(&__state, 0, sizeof(__state));
364
	}
365

366
	return ret;
367 368 369 370 371 372 373 374
}

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

#endif /* GIT_THREADS */