Commit 8aa69f887afaced1389d1ae965d5f313b10da66b

Edward Thomson 2020-05-13T10:48:13

mwindow: localize mutex Move the mwindow mutex into the mwindow code itself, initializing it in the mwindow global initialization function instead of in the global initializer.

diff --git a/src/futils.c b/src/futils.c
index 8c0f008..552a3fb 100644
--- a/src/futils.c
+++ b/src/futils.c
@@ -9,6 +9,7 @@
 
 #include "global.h"
 #include "strmap.h"
+#include "hash.h"
 #include <ctype.h>
 #if GIT_WIN32
 #include "win32/findfile.h"
diff --git a/src/global.c b/src/global.c
index 0642977..8b16166 100644
--- a/src/global.c
+++ b/src/global.c
@@ -12,6 +12,7 @@
 #include "sysdir.h"
 #include "filter.h"
 #include "settings.h"
+#include "mwindow.h"
 #include "merge_driver.h"
 #include "pool.h"
 #include "streams/registry.h"
@@ -22,8 +23,6 @@
 #include "transports/ssh.h"
 #include "win32/w32_stack.h"
 
-git_mutex git__mwindow_mutex;
-
 typedef int (*git_global_init_fn)(void);
 
 static git_global_init_fn git__init_callbacks[] = {
@@ -147,9 +146,6 @@ static int synchronized_threads_init(void)
 	if ((_fls_index = FlsAlloc(fls_free)) == FLS_OUT_OF_INDEXES)
 		return -1;
 
-	if (git_mutex_init(&git__mwindow_mutex))
-		return -1;
-
 	error = init_common();
 
 	return error;
@@ -186,7 +182,6 @@ int git_libgit2_shutdown(void)
 		shutdown_common();
 
 		FlsFree(_fls_index);
-		git_mutex_free(&git__mwindow_mutex);
 	}
 
 	/* Exit the lock */
@@ -229,11 +224,7 @@ static void cb__free_status(void *st)
 
 static void init_once(void)
 {
-	if ((init_error = git_mutex_init(&git__mwindow_mutex)) != 0)
-		return;
-
 	pthread_key_create(&_tls_key, &cb__free_status);
-
 	init_error = init_common();
 }
 
@@ -276,7 +267,6 @@ int git_libgit2_shutdown(void)
 	git__free(ptr);
 
 	pthread_key_delete(_tls_key);
-	git_mutex_free(&git__mwindow_mutex);
 	_once_init = new_once;
 
 out:
diff --git a/src/global.h b/src/global.h
index 2a7c727..f4e55eb 100644
--- a/src/global.h
+++ b/src/global.h
@@ -9,9 +9,6 @@
 
 #include "common.h"
 
-#include "mwindow.h"
-#include "hash.h"
-
 typedef struct {
 	git_error *last_error;
 	git_error error_t;
@@ -27,8 +24,6 @@ typedef struct {
 
 git_global_st *git__global_state(void);
 
-extern git_mutex git__mwindow_mutex;
-
 #define GIT_GLOBAL (git__global_state())
 
 typedef void (*git_global_shutdown_fn)(void);
diff --git a/src/mwindow.c b/src/mwindow.c
index ac26452..78ac411 100644
--- a/src/mwindow.c
+++ b/src/mwindow.c
@@ -29,16 +29,21 @@ size_t git_mwindow__window_size = DEFAULT_WINDOW_SIZE;
 size_t git_mwindow__mapped_limit = DEFAULT_MAPPED_LIMIT;
 size_t git_mwindow__file_limit = DEFAULT_FILE_LIMIT;
 
+/* Mutex to control access */
+git_mutex git__mwindow_mutex;
+
 /* Whenever you want to read or modify this, grab git__mwindow_mutex */
 git_mwindow_ctl git_mwindow__mem_ctl;
 
 /* Global list of mwindow files, to open packs once across repos */
 git_strmap *git__pack_cache = NULL;
 
-static void git_mwindow_files_free(void)
+static void git_mwindow_global_shutdown(void)
 {
 	git_strmap *tmp = git__pack_cache;
 
+	git_mutex_free(&git__mwindow_mutex);
+
 	git__pack_cache = NULL;
 	git_strmap_free(tmp);
 }
@@ -47,7 +52,11 @@ int git_mwindow_global_init(void)
 {
 	assert(!git__pack_cache);
 
-	git__on_shutdown(git_mwindow_files_free);
+	git__on_shutdown(git_mwindow_global_shutdown);
+
+	if (git_mutex_init(&git__mwindow_mutex) != 0)
+		return -1;
+
 	return git_strmap_new(&git__pack_cache);
 }
 
diff --git a/src/mwindow.h b/src/mwindow.h
index 1a391b0..7519fc3 100644
--- a/src/mwindow.h
+++ b/src/mwindow.h
@@ -13,6 +13,8 @@
 #include "map.h"
 #include "vector.h"
 
+extern git_mutex git__mwindow_mutex;
+
 typedef struct git_mwindow {
 	struct git_mwindow *next;
 	git_map window_map;