deps/libgit2/src/util/runtime.c

filter: Statementstmt 50.0% 25 / 50 Branchbran 38.2% 13 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 45.2% 38 / 84 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/mwindow.c ↑ Summary deps/libgit2/src/util/util.c »
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 "git2_util.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  
16 87 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 1392 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 1305 87 1392
23 1305 ▶ if ((ret = init_fns[i]()) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = init_fns[i]()) != 0) 0 1305 1305
24 0 break;
25 }
26  
27 87 GIT_MEMORY_BARRIER;
28  
29 87 return ret;
30 }
31  
32 0 static void shutdown_common(void)
33 {
34 git_runtime_shutdown_fn cb;
35 int pos;
36  
37 0 for (pos = git_atomic32_get(&shutdown_callback_count);
38 0 ▶ pos > 0;
Branch: 1 branch
BranchTrueFalseTotal
pos > 0; 0 0 0
39 0 pos = git_atomic32_dec(&shutdown_callback_count)) {
40 0 cb = git_atomic_swap(shutdown_callback[pos - 1], NULL);
41  
42 0 ▶ if (cb != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (cb != NULL) 0 0 0
43 0 cb();
44 }
45 0 }
46  
47 696 int git_runtime_shutdown_register(git_runtime_shutdown_fn callback)
48 {
49 696 int count = git_atomic32_inc(&shutdown_callback_count);
50  
51 696 ▶ if (count > (int)ARRAY_SIZE(shutdown_callback) || count == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (count > (int)ARRAY_SIZE(shutdown_callback) || count == 0) { 696 0 696
if (count > (int)ARRAY_SIZE(shutdown_callback) || count == 0) { 0 696 696
52 0 git_error_set(GIT_ERROR_INVALID,
53 "too many shutdown callbacks registered");
54 0 git_atomic32_dec(&shutdown_callback_count);
55 0 return -1;
56 }
57  
58 696 shutdown_callback[count - 1] = callback;
59  
60 696 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  
71 GIT_INLINE(int) init_lock(void)
72 {
73 while (InterlockedCompareExchange(&init_spinlock, 1, 0)) { Sleep(0); }
74 return 0;
75 }
76  
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  
92 154 GIT_INLINE(int) init_lock(void)
93 {
94 154 ▶ return pthread_mutex_lock(&init_mutex) == 0 ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
return pthread_mutex_lock(&init_mutex) == 0 ? 0 : -1; 154 0 154
95 }
96  
97 154 GIT_INLINE(int) init_unlock(void)
98 {
99 154 ▶ return pthread_mutex_unlock(&init_mutex) == 0 ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
return pthread_mutex_unlock(&init_mutex) == 0 ? 0 : -1; 154 0 154
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  
111 87 int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt)
112 {
113 int ret;
114  
115 87 ▶ if (init_lock() < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (init_lock() < 0) 0 87 87
116 0 return -1;
117  
118 /* Only do work on a 0 -> 1 transition of the refcount */
119 87 ▶ if ((ret = git_atomic32_inc(&init_count)) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = git_atomic32_inc(&init_count)) == 1) { 87 0 87
120 87 ▶ if (init_common(init_fns, cnt) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (init_common(init_fns, cnt) < 0) 0 87 87
121 0 ret = -1;
122 }
123  
124 87 ▶ if (init_unlock() < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (init_unlock() < 0) 0 87 87
125 0 return -1;
126  
127 87 return ret;
128 }
129  
130 67 int git_runtime_init_count(void)
131 {
132 int ret;
133  
134 67 ▶ if (init_lock() < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (init_lock() < 0) 0 67 67
135 0 return -1;
136  
137 67 ret = git_atomic32_get(&init_count);
138  
139 67 ▶ if (init_unlock() < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (init_unlock() < 0) 0 67 67
140 0 return -1;
141  
142 67 return ret;
143 }
144  
145 0 int git_runtime_shutdown(void)
146 {
147 int ret;
148  
149 /* Enter the lock */
150 0 ▶ if (init_lock() < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (init_lock() < 0) 0 0 0
151 0 return -1;
152  
153 /* Only do work on a 1 -> 0 transition of the refcount */
154 0 ▶ if ((ret = git_atomic32_dec(&init_count)) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = git_atomic32_dec(&init_count)) == 0) 0 0 0
155 0 shutdown_common();
156  
157 /* Exit the lock */
158 0 ▶ if (init_unlock() < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (init_unlock() < 0) 0 0 0
159 0 return -1;
160  
161 0 return ret;
162 }
« deps/libgit2/src/libgit2/mwindow.c ↑ Summary deps/libgit2/src/util/util.c »