global.c 4.51 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 11
#include "fileops.h"
#include "git2/threads.h"
12 13
#include "thread-utils.h"

14 15 16

git_mutex git__mwindow_mutex;

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#define MAX_SHUTDOWN_CB 8

git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
git_atomic git__n_shutdown_callbacks;

void git__on_shutdown(git_global_shutdown_fn callback)
{
	int count = git_atomic_inc(&git__n_shutdown_callbacks);
	assert(count <= MAX_SHUTDOWN_CB);
	git__shutdown_callbacks[count - 1] = callback;
}

static void git__shutdown(void)
{
	int pos;

	while ((pos = git_atomic_dec(&git__n_shutdown_callbacks)) >= 0) {
		if (git__shutdown_callbacks[pos])
			git__shutdown_callbacks[pos]();
	}
}

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
/**
 * 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.
 */

65 66 67 68 69 70 71 72
/*
 * `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.
 */
73 74 75 76 77
#if defined(GIT_THREADS) && defined(GIT_WIN32)

static DWORD _tls_index;
static int _tls_init = 0;

78
int git_threads_init(void)
79
{
80 81
	int error;

82
	if (_tls_init)
83
		return 0;
84 85

	_tls_index = TlsAlloc();
Russell Belfer committed
86 87
	if (git_mutex_init(&git__mwindow_mutex))
		return -1;
88 89

	/* Initialize any other subsystems that have global state */
90 91
	if ((error = git_hash_global_init()) >= 0 &&
		(error = git_futils_dirs_global_init()) >= 0)
92 93 94 95
		_tls_init = 1;

	GIT_MEMORY_BARRIER;

96 97
	win32_pthread_initialize();

98
	return error;
99 100 101 102
}

void git_threads_shutdown(void)
{
103
	/* Shut down any subsystems that have global state */
104
	git__shutdown();
105

106 107
	TlsFree(_tls_index);
	_tls_init = 0;
108

109
	git_mutex_free(&git__mwindow_mutex);
110 111 112 113 114 115
}

git_global_st *git__global_state(void)
{
	void *ptr;

116 117
	assert(_tls_init);

118 119 120
	if ((ptr = TlsGetValue(_tls_index)) != NULL)
		return ptr;

121
	ptr = git__malloc(sizeof(git_global_st));
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	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;
static int _tls_init = 0;

static void cb__free_status(void *st)
{
137
	git__free(st);
138 139
}

140
int git_threads_init(void)
141
{
142 143
	int error = 0;

144
	if (_tls_init)
145
		return 0;
146

Russell Belfer committed
147 148
	if (git_mutex_init(&git__mwindow_mutex))
		return -1;
149
	pthread_key_create(&_tls_key, &cb__free_status);
150 151

	/* Initialize any other subsystems that have global state */
152 153
	if ((error = git_hash_global_init()) >= 0 &&
		(error = git_futils_dirs_global_init()) >= 0)
154 155 156 157 158
		_tls_init = 1;

	GIT_MEMORY_BARRIER;

	return error;
159 160 161 162
}

void git_threads_shutdown(void)
{
163 164 165
	/* Shut down any subsystems that have global state */
	git__shutdown();

Russell Belfer committed
166 167 168 169 170 171
	if (_tls_init) {
		void *ptr = pthread_getspecific(_tls_key);
		pthread_setspecific(_tls_key, NULL);
		git__free(ptr);
	}

172 173
	pthread_key_delete(_tls_key);
	_tls_init = 0;
174
	git_mutex_free(&git__mwindow_mutex);
175 176 177 178 179 180
}

git_global_st *git__global_state(void)
{
	void *ptr;

181 182
	assert(_tls_init);

183 184 185
	if ((ptr = pthread_getspecific(_tls_key)) != NULL)
		return ptr;

186
	ptr = git__malloc(sizeof(git_global_st));
187 188 189 190 191 192 193 194 195 196 197 198
	if (!ptr)
		return NULL;

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

#else

static git_global_st __state;

199
int git_threads_init(void)
200
{
201
	/* noop */
202
	return 0;
203 204 205 206
}

void git_threads_shutdown(void)
{
207
	/* Shut down any subsystems that have global state */
208
	git__shutdown();
209 210 211 212 213 214 215 216
}

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

#endif /* GIT_THREADS */