2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
11 static git_runtime_shutdown_fn shutdown_callback
[32];
12 static git_atomic32 shutdown_callback_count
;
14 static git_atomic32 init_count
;
16 static int init_common(git_runtime_init_fn init_fns
[], size_t cnt
)
21 /* Initialize subsystems that have global state */
22 for (i
= 0; i
< cnt
; i
++) {
23 if ((ret
= init_fns
[i
]()) != 0)
32 static void shutdown_common(void)
34 git_runtime_shutdown_fn cb
;
37 for (pos
= git_atomic32_get(&shutdown_callback_count
);
39 pos
= git_atomic32_dec(&shutdown_callback_count
)) {
40 cb
= git_atomic_swap(shutdown_callback
[pos
- 1], NULL
);
47 int git_runtime_shutdown_register(git_runtime_shutdown_fn callback
)
49 int count
= git_atomic32_inc(&shutdown_callback_count
);
51 if (count
> (int)ARRAY_SIZE(shutdown_callback
) || count
== 0) {
52 git_error_set(GIT_ERROR_INVALID
,
53 "too many shutdown callbacks registered");
54 git_atomic32_dec(&shutdown_callback_count
);
58 shutdown_callback
[count
- 1] = callback
;
63 #if defined(GIT_THREADS) && defined(GIT_WIN32)
66 * On Win32, we use a spinlock to provide locking semantics. This is
67 * lighter-weight than a proper critical section.
69 static volatile LONG init_spinlock
= 0;
71 GIT_INLINE(int) init_lock(void)
73 while (InterlockedCompareExchange(&init_spinlock
, 1, 0)) { Sleep(0); }
77 GIT_INLINE(int) init_unlock(void)
79 InterlockedExchange(&init_spinlock
, 0);
83 #elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
86 * On POSIX, we need to use a proper mutex for locking. We might prefer
87 * a spinlock here, too, but there's no static initializer for a
90 static pthread_mutex_t init_mutex
= PTHREAD_MUTEX_INITIALIZER
;
92 GIT_INLINE(int) init_lock(void)
94 return pthread_mutex_lock(&init_mutex
) == 0 ? 0 : -1;
97 GIT_INLINE(int) init_unlock(void)
99 return pthread_mutex_unlock(&init_mutex
) == 0 ? 0 : -1;
102 #elif defined(GIT_THREADS)
103 # error unknown threading model
106 # define init_lock() git__noop()
107 # define init_unlock() git__noop()
111 int git_runtime_init(git_runtime_init_fn init_fns
[], size_t cnt
)
118 /* Only do work on a 0 -> 1 transition of the refcount */
119 if ((ret
= git_atomic32_inc(&init_count
)) == 1) {
120 if (init_common(init_fns
, cnt
) < 0)
124 if (init_unlock() < 0)
130 int git_runtime_init_count(void)
137 ret
= git_atomic32_get(&init_count
);
139 if (init_unlock() < 0)
145 int git_runtime_shutdown(void)
153 /* Only do work on a 1 -> 0 transition of the refcount */
154 if ((ret
= git_atomic32_dec(&init_count
)) == 0)
158 if (init_unlock() < 0)