#include "git2/global.h"
#include "git2/sys/openssl.h"
#include "thread-utils.h"
-
+#if defined(GIT_MSVC_CRTDBG)
+#include "win32/w32_stack.h"
+#include "win32/w32_crtdbg_stacktrace.h"
+#endif
git_mutex git__mwindow_mutex;
#define MAX_SHUTDOWN_CB 8
-#ifdef GIT_SSL
+#ifdef GIT_OPENSSL
# include <openssl/ssl.h>
SSL_CTX *git__ssl_ctx;
# ifdef GIT_THREADS
}
}
-#if defined(GIT_THREADS) && defined(GIT_SSL)
+#if defined(GIT_THREADS) && defined(GIT_OPENSSL)
void openssl_locking_function(int mode, int n, const char *file, int line)
{
int lock;
static void init_ssl(void)
{
-#ifdef GIT_SSL
+#ifdef GIT_OPENSSL
long ssl_opts = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
/* Older OpenSSL and MacOS OpenSSL doesn't have this */
#endif
}
+/**
+ * This function aims to clean-up the SSL context which
+ * we allocated.
+ */
+static void uninit_ssl(void)
+{
+#ifdef GIT_OPENSSL
+ if (git__ssl_ctx) {
+ SSL_CTX_free(git__ssl_ctx);
+ git__ssl_ctx = NULL;
+ }
+#endif
+}
+
int git_openssl_set_locking(void)
{
-#ifdef GIT_SSL
+#ifdef GIT_OPENSSL
# ifdef GIT_THREADS
int num_locks, i;
/* Only do work on a 0 -> 1 transition of the refcount */
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)
ret = -1;
}
static void synchronized_threads_shutdown(void)
{
- void *ptr;
-
/* Shut down any subsystems that have global state */
git__shutdown();
- ptr = TlsGetValue(_tls_index);
- git__global_state_cleanup(ptr);
+ git__free_tls_data();
TlsFree(_tls_index);
git_mutex_free(&git__mwindow_mutex);
while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
/* 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();
+#if defined(GIT_MSVC_CRTDBG)
+ git_win32__crtdbg_stacktrace_cleanup();
+ git_win32__stack_cleanup();
+#endif
+ }
+
/* Exit the lock */
InterlockedExchange(&_mutex, 0);
return ptr;
}
-void git__free_thread_global_state(void)
+/**
+ * Free the TLS data associated with this thread.
+ * This should only be used by the thread as it
+ * is exiting.
+ */
+void git__free_tls_data(void)
{
void *ptr = TlsGetValue(_tls_index);
if (!ptr)
TlsSetValue(_tls_index, NULL);
}
-BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
-{
- if (reason == DLL_THREAD_DETACH)
- git__free_thread_global_state();
-
- return TRUE;
-}
-
#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
static pthread_key_t _tls_key;
/* Shut down any subsystems that have global state */
git__shutdown();
+ uninit_ssl();
ptr = pthread_getspecific(_tls_key);
pthread_setspecific(_tls_key, NULL);
git__shutdown();
git__global_state_cleanup(&__state);
+ uninit_ssl();
return 0;
}