|
5 | 5 | * a Linking Exception. For full terms see the included COPYING file. |
6 | 6 | */ |
7 | 7 |
|
8 | | -#include "global.h" |
| 8 | +#include "runtime.h" |
9 | 9 |
|
10 | 10 | #include "alloc.h" |
11 | 11 | #include "threadstate.h" |
|
24 | 24 | #include "transports/ssh.h" |
25 | 25 | #include "win32/w32_stack.h" |
26 | 26 |
|
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 | | - |
146 | 27 | int git_libgit2_init(void) |
147 | 28 | { |
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)); |
163 | 47 | } |
164 | 48 |
|
165 | 49 | int git_libgit2_shutdown(void) |
166 | 50 | { |
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(); |
182 | 52 | } |
0 commit comments