Merge pull request #4645 from pks-t/pks/racy-init-deinit global: adjust init count under lock
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
diff --git a/src/global.c b/src/global.c
index 02aedf5..53e6609 100644
--- a/src/global.c
+++ b/src/global.c
@@ -276,10 +276,10 @@ int git_libgit2_init(void)
{
int ret, err;
- ret = git_atomic_inc(&git__n_inits);
-
if ((err = pthread_mutex_lock(&_init_mutex)) != 0)
return err;
+
+ ret = git_atomic_inc(&git__n_inits);
err = pthread_once(&_once_init, init_once);
err |= pthread_mutex_unlock(&_init_mutex);
@@ -293,13 +293,13 @@ int git_libgit2_shutdown(void)
{
void *ptr = NULL;
pthread_once_t new_once = PTHREAD_ONCE_INIT;
- int ret;
+ int error, ret;
- if ((ret = git_atomic_dec(&git__n_inits)) != 0)
- return ret;
+ if ((error = pthread_mutex_lock(&_init_mutex)) != 0)
+ return error;
- if ((ret = pthread_mutex_lock(&_init_mutex)) != 0)
- return ret;
+ if ((ret = git_atomic_dec(&git__n_inits)) != 0)
+ goto out;
/* Shut down any subsystems that have global state */
shutdown_common();
@@ -314,10 +314,11 @@ int git_libgit2_shutdown(void)
git_mutex_free(&git__mwindow_mutex);
_once_init = new_once;
- if ((ret = pthread_mutex_unlock(&_init_mutex)) != 0)
- return ret;
+out:
+ if ((error = pthread_mutex_unlock(&_init_mutex)) != 0)
+ return error;
- return 0;
+ return ret;
}
git_global_st *git__global_state(void)