summaryrefslogtreecommitdiff
path: root/src/global.c
blob: c4e925b739050caa1d6e4f4af63926e452cff6d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
65
66
67
68
69
70
71
72
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
101
102
103
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * 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 "global.h"

#include "alloc.h"
#include "threadstate.h"
#include "hash.h"
#include "sysdir.h"
#include "filter.h"
#include "settings.h"
#include "mwindow.h"
#include "merge_driver.h"
#include "pool.h"
#include "streams/registry.h"
#include "streams/mbedtls.h"
#include "streams/openssl.h"
#include "thread-utils.h"
#include "git2/global.h"
#include "transports/ssh.h"
#include "win32/w32_stack.h"

typedef int (*git_global_init_fn)(void);

static git_global_init_fn git__init_callbacks[] = {
	git_allocator_global_init,
	git_threadstate_global_init,
	git_threads_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,
	git_pool_global_init,
	git_settings_global_init
};

static git_global_shutdown_fn git__shutdown_callbacks[ARRAY_SIZE(git__init_callbacks)];

static git_atomic git__n_shutdown_callbacks;
static git_atomic git__n_inits;

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

static int init_common(void)
{
	size_t i;
	int ret;

	/* Initialize subsystems that have global state */
	for (i = 0; i < ARRAY_SIZE(git__init_callbacks); i++)
		if ((ret = git__init_callbacks[i]()) != 0)
			break;

	GIT_MEMORY_BARRIER;

	return ret;
}

static void shutdown_common(void)
{
	int pos;

	/* Shutdown subsystems that have registered */
	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();
	}
}

/*
 * `git_libgit2_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_libgit2_init()`.  Without this,
 * CPU cores are free to reorder cache invalidation of `_tls_init`
 * before cache invalidation of the subsystems' newly written global
 * state.
 */
#if defined(GIT_THREADS) && defined(GIT_WIN32)

/*
 * On Win32, we use a spinlock to provide locking semantics.  This is
 * lighter-weight than a proper critical section.
 */
static volatile LONG init_spinlock = 0;

GIT_INLINE(int) init_lock(void)
{
	while (InterlockedCompareExchange(&init_spinlock, 1, 0)) { Sleep(0); }
	return 0;
}

GIT_INLINE(int) init_unlock(void)
{
	InterlockedExchange(&init_spinlock, 0);
	return 0;
}

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

/*
 * On POSIX, we need to use a proper mutex for locking.  We might prefer
 * a spinlock here, too, but there's no static initializer for a
 * pthread_spinlock_t.
 */
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

GIT_INLINE(int) init_lock(void)
{
	return pthread_mutex_lock(&mutex) == 0 ? 0 : -1;
}

GIT_INLINE(int) init_unlock(void)
{
	return pthread_mutex_unlock(&mutex) == 0 ? 0 : -1;
}

#elif defined(GIT_THREADS)
# error unknown threading model
#else

# define init_lock() 0
# define init_unlock() 0

#endif

int git_libgit2_init(void)
{
	int ret;

	if (init_lock() < 0)
		return -1;

	/* Only do work on a 0 -> 1 transition of the refcount */
	if ((ret = git_atomic_inc(&git__n_inits)) == 1) {
		if (init_common() < 0)
			ret = -1;
	}

	if (init_unlock() < 0)
		return -1;

	return ret;
}

int git_libgit2_shutdown(void)
{
	int ret;

	/* Enter the lock */
	if (init_lock() < 0)
		return -1;

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

	/* Exit the lock */
	if (init_unlock() < 0)
		return -1;

	return ret;
}