diff options
Diffstat (limited to 'src/global.c')
| -rw-r--r-- | src/global.c | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/src/global.c b/src/global.c index 006202a2c..b3e75a0e2 100644 --- a/src/global.c +++ b/src/global.c @@ -194,19 +194,21 @@ static int synchronized_threads_init(void) int git_libgit2_init(void) { - int error = 0; + int ret; /* Enter the lock */ while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); } /* Only do work on a 0 -> 1 transition of the refcount */ - if (1 == git_atomic_inc(&git__n_inits)) - error = synchronized_threads_init(); + if ((ret = git_atomic_inc(&git__n_inits)) == 1) { + if (synchronized_threads_init() < 0) + ret = -1; + } /* Exit the lock */ InterlockedExchange(&_mutex, 0); - return error; + return ret; } static void synchronized_threads_shutdown(void) @@ -217,17 +219,21 @@ static void synchronized_threads_shutdown(void) git_mutex_free(&git__mwindow_mutex); } -void git_libgit2_shutdown(void) +int git_libgit2_shutdown(void) { + int ret; + /* Enter the lock */ while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); } /* Only do work on a 1 -> 0 transition of the refcount */ - if (0 == git_atomic_dec(&git__n_inits)) + if ((ret = git_atomic_dec(&git__n_inits)) == 0) synchronized_threads_shutdown(); /* Exit the lock */ InterlockedExchange(&_mutex, 0); + + return ret; } git_global_st *git__global_state(void) @@ -281,17 +287,22 @@ static void init_once(void) int git_libgit2_init(void) { + int ret; + pthread_once(&_once_init, init_once); - git_atomic_inc(&git__n_inits); - return init_error; + ret = git_atomic_inc(&git__n_inits); + + return init_error ? init_error : ret; } -void git_libgit2_shutdown(void) +int git_libgit2_shutdown(void) { void *ptr = NULL; pthread_once_t new_once = PTHREAD_ONCE_INIT; + int ret; - if (git_atomic_dec(&git__n_inits) > 0) return; + if ((ret = git_atomic_dec(&git__n_inits)) > 0) + return ret; /* Shut down any subsystems that have global state */ git__shutdown(); @@ -303,6 +314,8 @@ void git_libgit2_shutdown(void) pthread_key_delete(_tls_key); git_mutex_free(&git__mwindow_mutex); _once_init = new_once; + + return ret; } git_global_st *git__global_state(void) @@ -336,15 +349,18 @@ int git_libgit2_init(void) ssl_inited = 1; } - git_atomic_inc(&git__n_inits); - return 0; + return git_atomic_inc(&git__n_inits); } -void git_libgit2_shutdown(void) +int git_libgit2_shutdown(void) { + int ret; + /* Shut down any subsystems that have global state */ - if (0 == git_atomic_dec(&git__n_inits)) + if (ret = git_atomic_dec(&git__n_inits)) git__shutdown(); + + return ret; } git_global_st *git__global_state(void) |
