Skip to content

Commit e316b0d

Browse files
committed
runtime: move init/shutdown into the "runtime"
Provide a mechanism for system components to register for initialization and shutdown of the libgit2 runtime.
1 parent 8970acb commit e316b0d

File tree

27 files changed

+262
-226
lines changed

27 files changed

+262
-226
lines changed

src/alloc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include "alloc.h"
9+
#include "runtime.h"
910

1011
#include "allocators/stdalloc.h"
1112
#include "allocators/win32_crtdbg.h"
@@ -40,7 +41,8 @@ int git_allocator_global_init(void)
4041
git_win32__crtdbg_stacktrace_init();
4142
git_win32__stack_init();
4243

43-
git__on_shutdown(allocator_global_shutdown);
44+
if (git_runtime_shutdown_register(allocator_global_shutdown) < 0)
45+
return -1;
4446
#endif
4547

4648
/*

src/filter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "futils.h"
1212
#include "hash.h"
1313
#include "repository.h"
14-
#include "global.h"
14+
#include "runtime.h"
1515
#include "git2/sys/filter.h"
1616
#include "git2/config.h"
1717
#include "blob.h"
@@ -206,7 +206,7 @@ int git_filter_global_init(void)
206206
GIT_FILTER_IDENT, ident, GIT_FILTER_IDENT_PRIORITY) < 0)
207207
error = -1;
208208

209-
git__on_shutdown(git_filter_global_shutdown);
209+
error = git_runtime_shutdown_register(git_filter_global_shutdown);
210210

211211
done:
212212
if (error) {

src/futils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "futils.h"
99

10-
#include "global.h"
10+
#include "runtime.h"
1111
#include "strmap.h"
1212
#include "hash.h"
1313
#include <ctype.h>

src/global.c

Lines changed: 20 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* a Linking Exception. For full terms see the included COPYING file.
66
*/
77

8-
#include "global.h"
8+
#include "runtime.h"
99

1010
#include "alloc.h"
1111
#include "threadstate.h"
@@ -24,159 +24,29 @@
2424
#include "transports/ssh.h"
2525
#include "win32/w32_stack.h"
2626

27-
typedef int (*git_global_init_fn)(void);
28-
29-
static git_global_init_fn git__init_callbacks[] = {
30-
git_allocator_global_init,
31-
git_threadstate_global_init,
32-
git_threads_global_init,
33-
git_hash_global_init,
34-
git_sysdir_global_init,
35-
git_filter_global_init,
36-
git_merge_driver_global_init,
37-
git_transport_ssh_global_init,
38-
git_stream_registry_global_init,
39-
git_openssl_stream_global_init,
40-
git_mbedtls_stream_global_init,
41-
git_mwindow_global_init,
42-
git_pool_global_init,
43-
git_settings_global_init
44-
};
45-
46-
static git_global_shutdown_fn git__shutdown_callbacks[ARRAY_SIZE(git__init_callbacks)];
47-
48-
static git_atomic git__n_shutdown_callbacks;
49-
static git_atomic git__n_inits;
50-
51-
void git__on_shutdown(git_global_shutdown_fn callback)
52-
{
53-
int count = git_atomic_inc(&git__n_shutdown_callbacks);
54-
assert(count <= (int) ARRAY_SIZE(git__shutdown_callbacks) && count > 0);
55-
git__shutdown_callbacks[count - 1] = callback;
56-
}
57-
58-
static int init_common(void)
59-
{
60-
size_t i;
61-
int ret;
62-
63-
/* Initialize subsystems that have global state */
64-
for (i = 0; i < ARRAY_SIZE(git__init_callbacks); i++)
65-
if ((ret = git__init_callbacks[i]()) != 0)
66-
break;
67-
68-
GIT_MEMORY_BARRIER;
69-
70-
return ret;
71-
}
72-
73-
static void shutdown_common(void)
74-
{
75-
int pos;
76-
77-
/* Shutdown subsystems that have registered */
78-
for (pos = git_atomic_get(&git__n_shutdown_callbacks);
79-
pos > 0;
80-
pos = git_atomic_dec(&git__n_shutdown_callbacks)) {
81-
82-
git_global_shutdown_fn cb = git__swap(
83-
git__shutdown_callbacks[pos - 1], NULL);
84-
85-
if (cb != NULL)
86-
cb();
87-
}
88-
}
89-
90-
/*
91-
* `git_libgit2_init()` allows subsystems to perform global setup,
92-
* which may take place in the global scope. An explicit memory
93-
* fence exists at the exit of `git_libgit2_init()`. Without this,
94-
* CPU cores are free to reorder cache invalidation of `_tls_init`
95-
* before cache invalidation of the subsystems' newly written global
96-
* state.
97-
*/
98-
#if defined(GIT_THREADS) && defined(GIT_WIN32)
99-
100-
/*
101-
* On Win32, we use a spinlock to provide locking semantics. This is
102-
* lighter-weight than a proper critical section.
103-
*/
104-
static volatile LONG init_spinlock = 0;
105-
106-
GIT_INLINE(int) init_lock(void)
107-
{
108-
while (InterlockedCompareExchange(&init_spinlock, 1, 0)) { Sleep(0); }
109-
return 0;
110-
}
111-
112-
GIT_INLINE(int) init_unlock(void)
113-
{
114-
InterlockedExchange(&init_spinlock, 0);
115-
return 0;
116-
}
117-
118-
#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
119-
120-
/*
121-
* On POSIX, we need to use a proper mutex for locking. We might prefer
122-
* a spinlock here, too, but there's no static initializer for a
123-
* pthread_spinlock_t.
124-
*/
125-
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
126-
127-
GIT_INLINE(int) init_lock(void)
128-
{
129-
return pthread_mutex_lock(&mutex) == 0 ? 0 : -1;
130-
}
131-
132-
GIT_INLINE(int) init_unlock(void)
133-
{
134-
return pthread_mutex_unlock(&mutex) == 0 ? 0 : -1;
135-
}
136-
137-
#elif defined(GIT_THREADS)
138-
# error unknown threading model
139-
#else
140-
141-
# define init_lock() 0
142-
# define init_unlock() 0
143-
144-
#endif
145-
14627
int git_libgit2_init(void)
14728
{
148-
int ret;
149-
150-
if (init_lock() < 0)
151-
return -1;
152-
153-
/* Only do work on a 0 -> 1 transition of the refcount */
154-
if ((ret = git_atomic_inc(&git__n_inits)) == 1) {
155-
if (init_common() < 0)
156-
ret = -1;
157-
}
158-
159-
if (init_unlock() < 0)
160-
return -1;
161-
162-
return ret;
29+
static git_runtime_init_fn init_fns[] = {
30+
git_allocator_global_init,
31+
git_threadstate_global_init,
32+
git_threads_global_init,
33+
git_hash_global_init,
34+
git_sysdir_global_init,
35+
git_filter_global_init,
36+
git_merge_driver_global_init,
37+
git_transport_ssh_global_init,
38+
git_stream_registry_global_init,
39+
git_openssl_stream_global_init,
40+
git_mbedtls_stream_global_init,
41+
git_mwindow_global_init,
42+
git_pool_global_init,
43+
git_settings_global_init
44+
};
45+
46+
return git_runtime_init(init_fns, ARRAY_SIZE(init_fns));
16347
}
16448

16549
int git_libgit2_shutdown(void)
16650
{
167-
int ret;
168-
169-
/* Enter the lock */
170-
if (init_lock() < 0)
171-
return -1;
172-
173-
/* Only do work on a 1 -> 0 transition of the refcount */
174-
if ((ret = git_atomic_dec(&git__n_inits)) == 0)
175-
shutdown_common();
176-
177-
/* Exit the lock */
178-
if (init_unlock() < 0)
179-
return -1;
180-
181-
return ret;
51+
return git_runtime_shutdown();
18252
}

src/global.h

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/hash/sha1/win32.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "win32.h"
99

10-
#include "global.h"
10+
#include "runtime.h"
1111

1212
#include <wincrypt.h>
1313
#include <strsafe.h>
@@ -129,7 +129,8 @@ int git_hash_sha1_global_init(void)
129129
if ((error = hash_cng_prov_init()) < 0)
130130
error = hash_cryptoapi_prov_init();
131131

132-
git__on_shutdown(sha1_shutdown);
132+
if (!error)
133+
error = git_runtime_shutdown_register(sha1_shutdown);
133134

134135
return error;
135136
}

src/merge_driver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "merge_driver.h"
99

1010
#include "vector.h"
11-
#include "global.h"
11+
#include "runtime.h"
1212
#include "merge.h"
1313
#include "git2/merge.h"
1414
#include "git2/sys/merge.h"
@@ -209,7 +209,7 @@ int git_merge_driver_global_init(void)
209209
merge_driver_name__binary, &git_merge_driver__binary)) < 0)
210210
goto done;
211211

212-
git__on_shutdown(git_merge_driver_global_shutdown);
212+
error = git_runtime_shutdown_register(git_merge_driver_global_shutdown);
213213

214214
done:
215215
if (error < 0)

src/mwindow.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "vector.h"
1111
#include "futils.h"
1212
#include "map.h"
13-
#include "global.h"
13+
#include "runtime.h"
1414
#include "strmap.h"
1515
#include "pack.h"
1616

@@ -50,14 +50,15 @@ static void git_mwindow_global_shutdown(void)
5050

5151
int git_mwindow_global_init(void)
5252
{
53-
assert(!git__pack_cache);
53+
int error;
5454

55-
git__on_shutdown(git_mwindow_global_shutdown);
55+
assert(!git__pack_cache);
5656

57-
if (git_mutex_init(&git__mwindow_mutex) != 0)
58-
return -1;
57+
if ((error = git_mutex_init(&git__mwindow_mutex)) < 0 ||
58+
(error = git_strmap_new(&git__pack_cache)) < 0)
59+
return error;
5960

60-
return git_strmap_new(&git__pack_cache);
61+
return git_runtime_shutdown_register(git_mwindow_global_shutdown);
6162
}
6263

6364
int git_mwindow_get_pack(struct git_pack_file **out, const char *path)

src/net.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "posix.h"
1515
#include "buffer.h"
1616
#include "http_parser.h"
17-
#include "global.h"
17+
#include "runtime.h"
1818

1919
#define DEFAULT_PORT_HTTP "80"
2020
#define DEFAULT_PORT_HTTPS "443"

src/netops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "posix.h"
1414
#include "buffer.h"
1515
#include "http_parser.h"
16-
#include "global.h"
16+
#include "runtime.h"
1717

1818
int gitno_recv(gitno_buffer *buf)
1919
{

0 commit comments

Comments
 (0)