Commit bad2702c by Edward Thomson

global: refactor setup and cleanup

Move the common initialization and cleanup methods to reduce
unnecessary duplication.
parent 0f814deb
...@@ -33,6 +33,9 @@ static git_atomic git__n_shutdown_callbacks; ...@@ -33,6 +33,9 @@ static git_atomic git__n_shutdown_callbacks;
static git_atomic git__n_inits; static git_atomic git__n_inits;
char *git__user_agent; char *git__user_agent;
static int init_ssl(void);
static void shutdown_ssl(void);
void git__on_shutdown(git_global_shutdown_fn callback) void git__on_shutdown(git_global_shutdown_fn callback)
{ {
int count = git_atomic_inc(&git__n_shutdown_callbacks); int count = git_atomic_inc(&git__n_shutdown_callbacks);
...@@ -49,16 +52,51 @@ static void git__global_state_cleanup(git_global_st *st) ...@@ -49,16 +52,51 @@ static void git__global_state_cleanup(git_global_st *st)
st->error_t.message = NULL; st->error_t.message = NULL;
} }
static void git__shutdown(void) static int init_common(void)
{
int ret;
/* Initialize the CRT debug allocator first, before our first malloc */
#if defined(GIT_MSVC_CRTDBG)
git_win32__crtdbg_stacktrace_init();
git_win32__stack_init();
#endif
/* Initialize any other subsystems that have global state */
if ((ret = git_hash_global_init()) == 0 &&
(ret = git_sysdir_global_init()) == 0 &&
(ret = init_ssl()) == 0) {
}
GIT_MEMORY_BARRIER;
return ret;
}
static void shutdown_common(void)
{ {
int pos; int pos;
/* Shutdown subsystems that have registered */ /* Shutdown subsystems that have registered */
for (pos = git_atomic_get(&git__n_shutdown_callbacks); pos > 0; pos = git_atomic_dec(&git__n_shutdown_callbacks)) { for (pos = git_atomic_get(&git__n_shutdown_callbacks);
git_global_shutdown_fn cb = git__swap(git__shutdown_callbacks[pos - 1], NULL); pos > 0;
pos = git_atomic_dec(&git__n_shutdown_callbacks)) {
git_global_shutdown_fn cb = git__swap(
git__shutdown_callbacks[pos - 1], NULL);
if (cb != NULL) if (cb != NULL)
cb(); cb();
} }
shutdown_ssl();
git__free(git__user_agent);
#if defined(GIT_MSVC_CRTDBG)
git_win32__crtdbg_stacktrace_cleanup();
git_win32__stack_cleanup();
#endif
} }
#if defined(GIT_THREADS) && defined(GIT_OPENSSL) #if defined(GIT_THREADS) && defined(GIT_OPENSSL)
...@@ -91,7 +129,7 @@ static void shutdown_ssl_locking(void) ...@@ -91,7 +129,7 @@ static void shutdown_ssl_locking(void)
} }
#endif #endif
static void init_ssl(void) static int init_ssl(void)
{ {
#ifdef GIT_OPENSSL #ifdef GIT_OPENSSL
long ssl_opts = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3; long ssl_opts = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
...@@ -116,15 +154,18 @@ static void init_ssl(void) ...@@ -116,15 +154,18 @@ static void init_ssl(void)
if (!SSL_CTX_set_default_verify_paths(git__ssl_ctx)) { if (!SSL_CTX_set_default_verify_paths(git__ssl_ctx)) {
SSL_CTX_free(git__ssl_ctx); SSL_CTX_free(git__ssl_ctx);
git__ssl_ctx = NULL; git__ssl_ctx = NULL;
return -1;
} }
#endif #endif
return 0;
} }
/** /**
* This function aims to clean-up the SSL context which * This function aims to clean-up the SSL context which
* we allocated. * we allocated.
*/ */
static void uninit_ssl(void) static void shutdown_ssl(void)
{ {
#ifdef GIT_OPENSSL #ifdef GIT_OPENSSL
if (git__ssl_ctx) { if (git__ssl_ctx) {
...@@ -208,14 +249,13 @@ static int synchronized_threads_init(void) ...@@ -208,14 +249,13 @@ static int synchronized_threads_init(void)
int error; int error;
_tls_index = TlsAlloc(); _tls_index = TlsAlloc();
win32_pthread_initialize();
if (git_mutex_init(&git__mwindow_mutex)) if (git_mutex_init(&git__mwindow_mutex))
return -1; return -1;
/* Initialize any other subsystems that have global state */ error = init_common();
if ((error = git_hash_global_init()) >= 0)
error = git_sysdir_global_init();
win32_pthread_initialize();
return error; return error;
} }
...@@ -229,11 +269,6 @@ int git_libgit2_init(void) ...@@ -229,11 +269,6 @@ int git_libgit2_init(void)
/* Only do work on a 0 -> 1 transition of the refcount */ /* Only do work on a 0 -> 1 transition of the refcount */
if ((ret = git_atomic_inc(&git__n_inits)) == 1) { if ((ret = git_atomic_inc(&git__n_inits)) == 1) {
#if defined(GIT_MSVC_CRTDBG)
git_win32__crtdbg_stacktrace_init();
git_win32__stack_init();
#endif
if (synchronized_threads_init() < 0) if (synchronized_threads_init() < 0)
ret = -1; ret = -1;
} }
...@@ -244,17 +279,6 @@ int git_libgit2_init(void) ...@@ -244,17 +279,6 @@ int git_libgit2_init(void)
return ret; return ret;
} }
static void synchronized_threads_shutdown(void)
{
/* Shut down any subsystems that have global state */
git__shutdown();
git__free_tls_data();
TlsFree(_tls_index);
git_mutex_free(&git__mwindow_mutex);
}
int git_libgit2_shutdown(void) int git_libgit2_shutdown(void)
{ {
int ret; int ret;
...@@ -264,14 +288,12 @@ int git_libgit2_shutdown(void) ...@@ -264,14 +288,12 @@ int git_libgit2_shutdown(void)
/* Only do work on a 1 -> 0 transition of the refcount */ /* Only do work on a 1 -> 0 transition of the refcount */
if ((ret = git_atomic_dec(&git__n_inits)) == 0) { if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
synchronized_threads_shutdown(); shutdown_common();
#if defined(GIT_MSVC_CRTDBG) git__free_tls_data();
git_win32__crtdbg_stacktrace_cleanup();
git_win32__stack_cleanup();
#endif
git__free(git__user_agent); TlsFree(_tls_index);
git_mutex_free(&git__mwindow_mutex);
} }
/* Exit the lock */ /* Exit the lock */
...@@ -331,17 +353,10 @@ static void init_once(void) ...@@ -331,17 +353,10 @@ static void init_once(void)
{ {
if ((init_error = git_mutex_init(&git__mwindow_mutex)) != 0) if ((init_error = git_mutex_init(&git__mwindow_mutex)) != 0)
return; return;
pthread_key_create(&_tls_key, &cb__free_status);
/* Initialize any other subsystems that have global state */
if ((init_error = git_hash_global_init()) >= 0)
init_error = git_sysdir_global_init();
/* OpenSSL needs to be initialized from the main thread */ pthread_key_create(&_tls_key, &cb__free_status);
init_ssl();
GIT_MEMORY_BARRIER; init_error = init_common();
} }
int git_libgit2_init(void) int git_libgit2_init(void)
...@@ -364,15 +379,13 @@ int git_libgit2_shutdown(void) ...@@ -364,15 +379,13 @@ int git_libgit2_shutdown(void)
return ret; return ret;
/* Shut down any subsystems that have global state */ /* Shut down any subsystems that have global state */
git__shutdown(); shutdown_common();
uninit_ssl();
ptr = pthread_getspecific(_tls_key); ptr = pthread_getspecific(_tls_key);
pthread_setspecific(_tls_key, NULL); pthread_setspecific(_tls_key, NULL);
git__global_state_cleanup(ptr); git__global_state_cleanup(ptr);
git__free(ptr); git__free(ptr);
git__free(git__user_agent);
pthread_key_delete(_tls_key); pthread_key_delete(_tls_key);
git_mutex_free(&git__mwindow_mutex); git_mutex_free(&git__mwindow_mutex);
...@@ -405,15 +418,16 @@ static git_global_st __state; ...@@ -405,15 +418,16 @@ static git_global_st __state;
int git_libgit2_init(void) int git_libgit2_init(void)
{ {
static int ssl_inited = 0; int ret;
if (!ssl_inited) { /* Only init SSL the first time */
init_ssl(); if ((ret = git_atomic_inc(&git__n_inits)) != 1)
ssl_inited = 1; return ret;
}
if ((ret = init_common()) < 0)
return ret;
git_buf_init(&__state.error_buf, 0); return 1;
return git_atomic_inc(&git__n_inits);
} }
int git_libgit2_shutdown(void) int git_libgit2_shutdown(void)
...@@ -421,15 +435,12 @@ int git_libgit2_shutdown(void) ...@@ -421,15 +435,12 @@ int git_libgit2_shutdown(void)
int ret; int ret;
/* Shut down any subsystems that have global state */ /* Shut down any subsystems that have global state */
if ((ret = git_atomic_dec(&git__n_inits)) != 0) if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
return ret; shutdown_common();
git__global_state_cleanup(&__state);
git__shutdown(); }
git__global_state_cleanup(&__state);
uninit_ssl();
git__free(git__user_agent);
return 0; return ret;
} }
git_global_st *git__global_state(void) git_global_st *git__global_state(void)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment