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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
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;