line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
100
|
|
|
|
|
for (i = 0; i < cnt; i++) { |
23
|
1305
|
50
|
|
|
|
|
if ((ret = init_fns[i]()) != 0) |
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
|
0
|
|
|
|
|
for (pos = git_atomic32_get(&shutdown_callback_count); |
38
|
|
|
|
|
|
|
pos > 0; |
39
|
0
|
|
|
|
|
|
pos = git_atomic32_dec(&shutdown_callback_count)) { |
40
|
0
|
|
|
|
|
|
cb = git_atomic_swap(shutdown_callback[pos - 1], NULL); |
41
|
|
|
|
|
|
|
|
42
|
0
|
0
|
|
|
|
|
if (cb != NULL) |
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
|
50
|
|
|
|
|
if (count > (int)ARRAY_SIZE(shutdown_callback) || count == 0) { |
|
|
50
|
|
|
|
|
|
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
|
50
|
|
|
|
|
return pthread_mutex_lock(&init_mutex) == 0 ? 0 : -1; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
154
|
|
|
|
|
|
GIT_INLINE(int) init_unlock(void) |
98
|
|
|
|
|
|
|
{ |
99
|
154
|
50
|
|
|
|
|
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
|
|
|
|
|
|
|
|
111
|
87
|
|
|
|
|
|
int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt) |
112
|
|
|
|
|
|
|
{ |
113
|
|
|
|
|
|
|
int ret; |
114
|
|
|
|
|
|
|
|
115
|
87
|
50
|
|
|
|
|
if (init_lock() < 0) |
116
|
0
|
|
|
|
|
|
return -1; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
/* Only do work on a 0 -> 1 transition of the refcount */ |
119
|
87
|
50
|
|
|
|
|
if ((ret = git_atomic32_inc(&init_count)) == 1) { |
120
|
87
|
50
|
|
|
|
|
if (init_common(init_fns, cnt) < 0) |
121
|
0
|
|
|
|
|
|
ret = -1; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
87
|
50
|
|
|
|
|
if (init_unlock() < 0) |
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
|
50
|
|
|
|
|
if (init_lock() < 0) |
135
|
0
|
|
|
|
|
|
return -1; |
136
|
|
|
|
|
|
|
|
137
|
67
|
|
|
|
|
|
ret = git_atomic32_get(&init_count); |
138
|
|
|
|
|
|
|
|
139
|
67
|
50
|
|
|
|
|
if (init_unlock() < 0) |
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
|
0
|
|
|
|
|
if (init_lock() < 0) |
151
|
0
|
|
|
|
|
|
return -1; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
/* Only do work on a 1 -> 0 transition of the refcount */ |
154
|
0
|
0
|
|
|
|
|
if ((ret = git_atomic32_dec(&init_count)) == 0) |
155
|
0
|
|
|
|
|
|
shutdown_common(); |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
/* Exit the lock */ |
158
|
0
|
0
|
|
|
|
|
if (init_unlock() < 0) |
159
|
0
|
|
|
|
|
|
return -1; |
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
return ret; |
162
|
|
|
|
|
|
|
} |