Blame src/global.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "global.h"
Packit Service 20376f
#include "hash.h"
Packit Service 20376f
#include "sysdir.h"
Packit Service 20376f
#include "filter.h"
Packit Service 20376f
#include "merge_driver.h"
Packit Service 20376f
#include "curl_stream.h"
Packit Service 20376f
#include "openssl_stream.h"
Packit Service 20376f
#include "thread-utils.h"
Packit Service 20376f
#include "git2/global.h"
Packit Service 20376f
#include "transports/ssh.h"
Packit Service 20376f
Packit Service 20376f
#if defined(GIT_MSVC_CRTDBG)
Packit Service 20376f
#include "win32/w32_stack.h"
Packit Service 20376f
#include "win32/w32_crtdbg_stacktrace.h"
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
git_mutex git__mwindow_mutex;
Packit Service 20376f
Packit Service 20376f
#define MAX_SHUTDOWN_CB 10
Packit Service 20376f
Packit Service 20376f
static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
Packit Service 20376f
static git_atomic git__n_shutdown_callbacks;
Packit Service 20376f
static git_atomic git__n_inits;
Packit Service 20376f
char *git__user_agent;
Packit Service 20376f
char *git__ssl_ciphers;
Packit Service 20376f
Packit Service 20376f
void git__on_shutdown(git_global_shutdown_fn callback)
Packit Service 20376f
{
Packit Service 20376f
	int count = git_atomic_inc(&git__n_shutdown_callbacks);
Packit Service 20376f
	assert(count <= MAX_SHUTDOWN_CB && count > 0);
Packit Service 20376f
	git__shutdown_callbacks[count - 1] = callback;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void git__global_state_cleanup(git_global_st *st)
Packit Service 20376f
{
Packit Service 20376f
	if (!st)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git__free(st->error_t.message);
Packit Service 20376f
	st->error_t.message = NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int init_common(void)
Packit Service 20376f
{
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	/* Initialize the CRT debug allocator first, before our first malloc */
Packit Service 20376f
#if defined(GIT_MSVC_CRTDBG)
Packit Service 20376f
	git_win32__crtdbg_stacktrace_init();
Packit Service 20376f
	git_win32__stack_init();
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	/* Initialize any other subsystems that have global state */
Packit Service 20376f
	if ((ret = git_hash_global_init()) == 0 &&
Packit Service 20376f
		(ret = git_sysdir_global_init()) == 0 &&
Packit Service 20376f
		(ret = git_filter_global_init()) == 0 &&
Packit Service 20376f
		(ret = git_merge_driver_global_init()) == 0 &&
Packit Service 20376f
		(ret = git_transport_ssh_global_init()) == 0 &&
Packit Service 20376f
		(ret = git_openssl_stream_global_init()) == 0 &&
Packit Service 20376f
		(ret = git_curl_stream_global_init()) == 0)
Packit Service 20376f
		ret = git_mwindow_global_init();
Packit Service 20376f
Packit Service 20376f
	GIT_MEMORY_BARRIER;
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void shutdown_common(void)
Packit Service 20376f
{
Packit Service 20376f
	int pos;
Packit Service 20376f
Packit Service 20376f
	/* Shutdown subsystems that have registered */
Packit Service 20376f
	for (pos = git_atomic_get(&git__n_shutdown_callbacks);
Packit Service 20376f
		pos > 0;
Packit Service 20376f
		pos = git_atomic_dec(&git__n_shutdown_callbacks)) {
Packit Service 20376f
Packit Service 20376f
		git_global_shutdown_fn cb = git__swap(
Packit Service 20376f
			git__shutdown_callbacks[pos - 1], NULL);
Packit Service 20376f
Packit Service 20376f
		if (cb != NULL)
Packit Service 20376f
			cb();
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git__free(git__user_agent);
Packit Service 20376f
	git__free(git__ssl_ciphers);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Handle the global state with TLS
Packit Service 20376f
 *
Packit Service 20376f
 * If libgit2 is built with GIT_THREADS enabled,
Packit Service 20376f
 * the `git_libgit2_init()` function must be called
Packit Service 20376f
 * before calling any other function of the library.
Packit Service 20376f
 *
Packit Service 20376f
 * This function allocates a TLS index (using pthreads
Packit Service 20376f
 * or the native Win32 API) to store the global state
Packit Service 20376f
 * on a per-thread basis.
Packit Service 20376f
 *
Packit Service 20376f
 * Any internal method that requires global state will
Packit Service 20376f
 * then call `git__global_state()` which returns a pointer
Packit Service 20376f
 * to the global state structure; this pointer is lazily
Packit Service 20376f
 * allocated on each thread.
Packit Service 20376f
 *
Packit Service 20376f
 * Before shutting down the library, the
Packit Service 20376f
 * `git_libgit2_shutdown` method must be called to free
Packit Service 20376f
 * the previously reserved TLS index.
Packit Service 20376f
 *
Packit Service 20376f
 * If libgit2 is built without threading support, the
Packit Service 20376f
 * `git__global_statestate()` call returns a pointer to a single,
Packit Service 20376f
 * statically allocated global state. The `git_thread_`
Packit Service 20376f
 * functions are not available in that case.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * `git_libgit2_init()` allows subsystems to perform global setup,
Packit Service 20376f
 * which may take place in the global scope.  An explicit memory
Packit Service 20376f
 * fence exists at the exit of `git_libgit2_init()`.  Without this,
Packit Service 20376f
 * CPU cores are free to reorder cache invalidation of `_tls_init`
Packit Service 20376f
 * before cache invalidation of the subsystems' newly written global
Packit Service 20376f
 * state.
Packit Service 20376f
 */
Packit Service 20376f
#if defined(GIT_THREADS) && defined(GIT_WIN32)
Packit Service 20376f
Packit Service 20376f
static DWORD _tls_index;
Packit Service 20376f
static volatile LONG _mutex = 0;
Packit Service 20376f
Packit Service 20376f
static int synchronized_threads_init(void)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	_tls_index = TlsAlloc();
Packit Service 20376f
Packit Service 20376f
	git_threads_init();
Packit Service 20376f
Packit Service 20376f
	if (git_mutex_init(&git__mwindow_mutex))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	error = init_common();
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_libgit2_init(void)
Packit Service 20376f
{
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	/* Enter the lock */
Packit Service 20376f
	while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
Packit Service 20376f
Packit Service 20376f
	/* Only do work on a 0 -> 1 transition of the refcount */
Packit Service 20376f
	if ((ret = git_atomic_inc(&git__n_inits)) == 1) {
Packit Service 20376f
		if (synchronized_threads_init() < 0)
Packit Service 20376f
			ret = -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Exit the lock */
Packit Service 20376f
	InterlockedExchange(&_mutex, 0);
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_libgit2_shutdown(void)
Packit Service 20376f
{
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	/* Enter the lock */
Packit Service 20376f
	while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
Packit Service 20376f
Packit Service 20376f
	/* Only do work on a 1 -> 0 transition of the refcount */
Packit Service 20376f
	if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
Packit Service 20376f
		shutdown_common();
Packit Service 20376f
Packit Service 20376f
		git__free_tls_data();
Packit Service 20376f
Packit Service 20376f
		TlsFree(_tls_index);
Packit Service 20376f
		git_mutex_free(&git__mwindow_mutex);
Packit Service 20376f
Packit Service 20376f
#if defined(GIT_MSVC_CRTDBG)
Packit Service 20376f
		git_win32__crtdbg_stacktrace_cleanup();
Packit Service 20376f
		git_win32__stack_cleanup();
Packit Service 20376f
#endif
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Exit the lock */
Packit Service 20376f
	InterlockedExchange(&_mutex, 0);
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
git_global_st *git__global_state(void)
Packit Service 20376f
{
Packit Service 20376f
	git_global_st *ptr;
Packit Service 20376f
Packit Service 20376f
	assert(git_atomic_get(&git__n_inits) > 0);
Packit Service 20376f
Packit Service 20376f
	if ((ptr = TlsGetValue(_tls_index)) != NULL)
Packit Service 20376f
		return ptr;
Packit Service 20376f
Packit Service 20376f
	ptr = git__calloc(1, sizeof(git_global_st));
Packit Service 20376f
	if (!ptr)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	git_buf_init(&ptr->error_buf, 0);
Packit Service 20376f
Packit Service 20376f
	TlsSetValue(_tls_index, ptr);
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Free the TLS data associated with this thread.
Packit Service 20376f
 * This should only be used by the thread as it
Packit Service 20376f
 * is exiting.
Packit Service 20376f
 */
Packit Service 20376f
void git__free_tls_data(void)
Packit Service 20376f
{
Packit Service 20376f
	void *ptr = TlsGetValue(_tls_index);
Packit Service 20376f
	if (!ptr)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git__global_state_cleanup(ptr);
Packit Service 20376f
	git__free(ptr);
Packit Service 20376f
	TlsSetValue(_tls_index, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(hInstDll);
Packit Service 20376f
	GIT_UNUSED(lpvReserved);
Packit Service 20376f
Packit Service 20376f
	/* This is how Windows lets us know our thread is being shut down */
Packit Service 20376f
	if (fdwReason == DLL_THREAD_DETACH) {
Packit Service 20376f
		git__free_tls_data();
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * Windows pays attention to this during library loading. We don't do anything
Packit Service 20376f
	 * so we trivially succeed.
Packit Service 20376f
	 */
Packit Service 20376f
	return TRUE;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
Packit Service 20376f
Packit Service 20376f
static pthread_key_t _tls_key;
Packit Service 20376f
static pthread_mutex_t _init_mutex = PTHREAD_MUTEX_INITIALIZER;
Packit Service 20376f
static pthread_once_t _once_init = PTHREAD_ONCE_INIT;
Packit Service 20376f
int init_error = 0;
Packit Service 20376f
Packit Service 20376f
static void cb__free_status(void *st)
Packit Service 20376f
{
Packit Service 20376f
	git__global_state_cleanup(st);
Packit Service 20376f
	git__free(st);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void init_once(void)
Packit Service 20376f
{
Packit Service 20376f
	if ((init_error = git_mutex_init(&git__mwindow_mutex)) != 0)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	pthread_key_create(&_tls_key, &cb__free_status);
Packit Service 20376f
Packit Service 20376f
	init_error = init_common();
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_libgit2_init(void)
Packit Service 20376f
{
Packit Service 20376f
	int ret, err;
Packit Service 20376f
Packit Service 20376f
	ret = git_atomic_inc(&git__n_inits);
Packit Service 20376f
Packit Service 20376f
	if ((err = pthread_mutex_lock(&_init_mutex)) != 0)
Packit Service 20376f
		return err;
Packit Service 20376f
	err = pthread_once(&_once_init, init_once);
Packit Service 20376f
	err |= pthread_mutex_unlock(&_init_mutex);
Packit Service 20376f
Packit Service 20376f
	if (err || init_error)
Packit Service 20376f
		return err | init_error;
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_libgit2_shutdown(void)
Packit Service 20376f
{
Packit Service 20376f
	void *ptr = NULL;
Packit Service 20376f
	pthread_once_t new_once = PTHREAD_ONCE_INIT;
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	if ((ret = git_atomic_dec(&git__n_inits)) != 0)
Packit Service 20376f
		return ret;
Packit Service 20376f
Packit Service 20376f
	if ((ret = pthread_mutex_lock(&_init_mutex)) != 0)
Packit Service 20376f
		return ret;
Packit Service 20376f
Packit Service 20376f
	/* Shut down any subsystems that have global state */
Packit Service 20376f
	shutdown_common();
Packit Service 20376f
Packit Service 20376f
	ptr = pthread_getspecific(_tls_key);
Packit Service 20376f
	pthread_setspecific(_tls_key, NULL);
Packit Service 20376f
Packit Service 20376f
	git__global_state_cleanup(ptr);
Packit Service 20376f
	git__free(ptr);
Packit Service 20376f
Packit Service 20376f
	pthread_key_delete(_tls_key);
Packit Service 20376f
	git_mutex_free(&git__mwindow_mutex);
Packit Service 20376f
	_once_init = new_once;
Packit Service 20376f
Packit Service 20376f
	if ((ret = pthread_mutex_unlock(&_init_mutex)) != 0)
Packit Service 20376f
		return ret;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
git_global_st *git__global_state(void)
Packit Service 20376f
{
Packit Service 20376f
	git_global_st *ptr;
Packit Service 20376f
Packit Service 20376f
	assert(git_atomic_get(&git__n_inits) > 0);
Packit Service 20376f
Packit Service 20376f
	if ((ptr = pthread_getspecific(_tls_key)) != NULL)
Packit Service 20376f
		return ptr;
Packit Service 20376f
Packit Service 20376f
	ptr = git__calloc(1, sizeof(git_global_st));
Packit Service 20376f
	if (!ptr)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	git_buf_init(&ptr->error_buf, 0);
Packit Service 20376f
	pthread_setspecific(_tls_key, ptr);
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#else
Packit Service 20376f
Packit Service 20376f
static git_global_st __state;
Packit Service 20376f
Packit Service 20376f
int git_libgit2_init(void)
Packit Service 20376f
{
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	/* Only init subsystems the first time */
Packit Service 20376f
	if ((ret = git_atomic_inc(&git__n_inits)) != 1)
Packit Service 20376f
		return ret;
Packit Service 20376f
Packit Service 20376f
	if ((ret = init_common()) < 0)
Packit Service 20376f
		return ret;
Packit Service 20376f
Packit Service 20376f
	return 1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_libgit2_shutdown(void)
Packit Service 20376f
{
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	/* Shut down any subsystems that have global state */
Packit Service 20376f
	if ((ret = git_atomic_dec(&git__n_inits)) == 0) {
Packit Service 20376f
		shutdown_common();
Packit Service 20376f
		git__global_state_cleanup(&__state);
Packit Service 20376f
		memset(&__state, 0, sizeof(__state));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
git_global_st *git__global_state(void)
Packit Service 20376f
{
Packit Service 20376f
	return &__state;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#endif /* GIT_THREADS */