1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
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.
6  */
7 
8 #include "common.h"
9 #include "runtime.h"
10 
11 static git_runtime_shutdown_fn shutdown_callback[32];
12 static git_atomic32 shutdown_callback_count;
13 
14 static git_atomic32 init_count;
15 
init_common(git_runtime_init_fn init_fns[],size_t cnt)16 static int init_common(git_runtime_init_fn init_fns[], size_t cnt)
17 {
18 	size_t i;
19 	int ret;
20 
21 	/* Initialize subsystems that have global state */
22 	for (i = 0; i < cnt; i++) {
23 		if ((ret = init_fns[i]()) != 0)
24 			break;
25 	}
26 
27 	GIT_MEMORY_BARRIER;
28 
29 	return ret;
30 }
31 
shutdown_common(void)32 static void shutdown_common(void)
33 {
34 	git_runtime_shutdown_fn cb;
35 	int pos;
36 
37 	for (pos = git_atomic32_get(&shutdown_callback_count);
38 	     pos > 0;
39 	     pos = git_atomic32_dec(&shutdown_callback_count)) {
40 		cb = git_atomic_swap(shutdown_callback[pos - 1], NULL);
41 
42 		if (cb != NULL)
43 			cb();
44 	}
45 }
46 
git_runtime_shutdown_register(git_runtime_shutdown_fn callback)47 int git_runtime_shutdown_register(git_runtime_shutdown_fn callback)
48 {
49 	int count = git_atomic32_inc(&shutdown_callback_count);
50 
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);
55 		return -1;
56 	}
57 
58 	shutdown_callback[count - 1] = callback;
59 
60 	return 0;
61 }
62 
63 #if defined(GIT_THREADS) && defined(GIT_WIN32)
64 
65 /*
66  * On Win32, we use a spinlock to provide locking semantics.  This is
67  * lighter-weight than a proper critical section.
68  */
69 static volatile LONG init_spinlock = 0;
70 
init_lock(void)71 GIT_INLINE(int) init_lock(void)
72 {
73 	while (InterlockedCompareExchange(&init_spinlock, 1, 0)) { Sleep(0); }
74 	return 0;
75 }
76 
init_unlock(void)77 GIT_INLINE(int) init_unlock(void)
78 {
79 	InterlockedExchange(&init_spinlock, 0);
80 	return 0;
81 }
82 
83 #elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
84 
85 /*
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
88  * pthread_spinlock_t.
89  */
90 static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
91 
init_lock(void)92 GIT_INLINE(int) init_lock(void)
93 {
94 	return pthread_mutex_lock(&init_mutex) == 0 ? 0 : -1;
95 }
96 
init_unlock(void)97 GIT_INLINE(int) init_unlock(void)
98 {
99 	return pthread_mutex_unlock(&init_mutex) == 0 ? 0 : -1;
100 }
101 
102 #elif defined(GIT_THREADS)
103 # error unknown threading model
104 #else
105 
106 # define init_lock() git__noop()
107 # define init_unlock() git__noop()
108 
109 #endif
110 
git_runtime_init(git_runtime_init_fn init_fns[],size_t cnt)111 int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt)
112 {
113 	int ret;
114 
115 	if (init_lock() < 0)
116 		return -1;
117 
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)
121 			ret = -1;
122 	}
123 
124 	if (init_unlock() < 0)
125 		return -1;
126 
127 	return ret;
128 }
129 
git_runtime_init_count(void)130 int git_runtime_init_count(void)
131 {
132 	int ret;
133 
134 	if (init_lock() < 0)
135 		return -1;
136 
137 	ret = git_atomic32_get(&init_count);
138 
139 	if (init_unlock() < 0)
140 		return -1;
141 
142 	return ret;
143 }
144 
git_runtime_shutdown(void)145 int git_runtime_shutdown(void)
146 {
147 	int ret;
148 
149 	/* Enter the lock */
150 	if (init_lock() < 0)
151 		return -1;
152 
153 	/* Only do work on a 1 -> 0 transition of the refcount */
154 	if ((ret = git_atomic32_dec(&init_count)) == 0)
155 		shutdown_common();
156 
157 	/* Exit the lock */
158 	if (init_unlock() < 0)
159 		return -1;
160 
161 	return ret;
162 }
163