Skip to content

Commit cda4c59

Browse files
author
Vicent Martí
committed
Merge pull request libgit2#1890 from libgit2/multiple-init
Multiple init
2 parents 7f91191 + ccd7f0c commit cda4c59

File tree

2 files changed

+73
-35
lines changed

2 files changed

+73
-35
lines changed

src/global.c

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -73,47 +73,69 @@ static void git__shutdown(void)
7373
#if defined(GIT_THREADS) && defined(GIT_WIN32)
7474

7575
static DWORD _tls_index;
76-
static int _tls_init = 0;
76+
static DWORD _mutex = 0;
77+
static DWORD _n_inits = 0;
7778

78-
int git_threads_init(void)
79+
static int synchronized_threads_init()
7980
{
8081
int error;
8182

82-
if (_tls_init)
83-
return 0;
84-
8583
_tls_index = TlsAlloc();
8684
if (git_mutex_init(&git__mwindow_mutex))
8785
return -1;
8886

8987
/* Initialize any other subsystems that have global state */
90-
if ((error = git_hash_global_init()) >= 0 &&
91-
(error = git_futils_dirs_global_init()) >= 0)
92-
_tls_init = 1;
93-
94-
GIT_MEMORY_BARRIER;
88+
if ((error = git_hash_global_init()) >= 0)
89+
error = git_futils_dirs_global_init();
9590

9691
win32_pthread_initialize();
9792

9893
return error;
9994
}
10095

101-
void git_threads_shutdown(void)
96+
int git_threads_init(void)
97+
{
98+
int error = 0;
99+
100+
/* Enter the lock */
101+
while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
102+
103+
/* Only do work on a 0 -> 1 transition of the refcount */
104+
if (1 == ++_n_inits)
105+
error = synchronized_threads_init();
106+
107+
/* Exit the lock */
108+
InterlockedExchange(&_mutex, 0);
109+
110+
return error;
111+
}
112+
113+
static void synchronized_threads_shutdown()
102114
{
103115
/* Shut down any subsystems that have global state */
104116
git__shutdown();
105-
106117
TlsFree(_tls_index);
107-
_tls_init = 0;
108-
109118
git_mutex_free(&git__mwindow_mutex);
110119
}
111120

121+
void git_threads_shutdown(void)
122+
{
123+
/* Enter the lock */
124+
while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
125+
126+
/* Only do work on a 1 -> 0 transition of the refcount */
127+
if (0 == --_n_inits)
128+
synchronized_threads_shutdown();
129+
130+
/* Exit the lock */
131+
InterlockedExchange(&_mutex, 0);
132+
}
133+
112134
git_global_st *git__global_state(void)
113135
{
114136
void *ptr;
115137

116-
assert(_tls_init);
138+
assert(_n_inits);
117139

118140
if ((ptr = TlsGetValue(_tls_index)) != NULL)
119141
return ptr;
@@ -130,55 +152,58 @@ git_global_st *git__global_state(void)
130152
#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
131153

132154
static pthread_key_t _tls_key;
133-
static int _tls_init = 0;
155+
static pthread_once_t _once_init = PTHREAD_ONCE_INIT;
156+
static git_atomic git__n_inits;
157+
int init_error = 0;
134158

135159
static void cb__free_status(void *st)
136160
{
137161
git__free(st);
138162
}
139163

140-
int git_threads_init(void)
164+
static void init_once(void)
141165
{
142-
int error = 0;
143-
144-
if (_tls_init)
145-
return 0;
146-
147-
if (git_mutex_init(&git__mwindow_mutex))
148-
return -1;
166+
if ((init_error = git_mutex_init(&git__mwindow_mutex)) != 0)
167+
return;
149168
pthread_key_create(&_tls_key, &cb__free_status);
150169

151170
/* Initialize any other subsystems that have global state */
152-
if ((error = git_hash_global_init()) >= 0 &&
153-
(error = git_futils_dirs_global_init()) >= 0)
154-
_tls_init = 1;
171+
if ((init_error = git_hash_global_init()) >= 0)
172+
init_error = git_futils_dirs_global_init();
155173

156174
GIT_MEMORY_BARRIER;
175+
}
157176

158-
return error;
177+
int git_threads_init(void)
178+
{
179+
pthread_once(&_once_init, init_once);
180+
git_atomic_inc(&git__n_inits);
181+
return init_error;
159182
}
160183

161184
void git_threads_shutdown(void)
162185
{
186+
pthread_once_t new_once = PTHREAD_ONCE_INIT;
187+
188+
if (git_atomic_dec(&git__n_inits) > 0) return;
189+
163190
/* Shut down any subsystems that have global state */
164191
git__shutdown();
165192

166-
if (_tls_init) {
167-
void *ptr = pthread_getspecific(_tls_key);
168-
pthread_setspecific(_tls_key, NULL);
169-
git__free(ptr);
170-
}
193+
void *ptr = pthread_getspecific(_tls_key);
194+
pthread_setspecific(_tls_key, NULL);
195+
git__free(ptr);
171196

172197
pthread_key_delete(_tls_key);
173-
_tls_init = 0;
174198
git_mutex_free(&git__mwindow_mutex);
199+
_once_init = new_once;
175200
}
176201

177202
git_global_st *git__global_state(void)
178203
{
179204
void *ptr;
180205

181-
assert(_tls_init);
206+
assert(git__n_inits.val);
182207

183208
if ((ptr = pthread_getspecific(_tls_key)) != NULL)
184209
return ptr;

tests-clar/threads/basic.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,16 @@ void test_threads_basic__cache(void)
2121
// run several threads polling the cache at the same time
2222
cl_assert(1 == 1);
2323
}
24+
25+
void test_threads_basic__multiple_init(void)
26+
{
27+
git_repository *nested_repo;
28+
29+
git_threads_init();
30+
cl_git_pass(git_repository_open(&nested_repo, cl_fixture("testrepo.git")));
31+
git_repository_free(nested_repo);
32+
33+
git_threads_shutdown();
34+
cl_git_pass(git_repository_open(&nested_repo, cl_fixture("testrepo.git")));
35+
git_repository_free(nested_repo);
36+
}

0 commit comments

Comments
 (0)