move hash library func ptrs to global global
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
diff --git a/include/git2/threads.h b/include/git2/threads.h
index 567a104..f448f6a 100644
--- a/include/git2/threads.h
+++ b/include/git2/threads.h
@@ -27,8 +27,10 @@ GIT_BEGIN_DECL
*
* If libgit2 has been built without GIT_THREADS
* support, this function is a no-op.
+ *
+ * @return 0 or an error code
*/
-GIT_EXTERN(void) git_threads_init(void);
+GIT_EXTERN(int) git_threads_init(void);
/**
* Shutdown the threading system.
diff --git a/src/global.c b/src/global.c
index 22127fa..de7e42d 100644
--- a/src/global.c
+++ b/src/global.c
@@ -6,6 +6,7 @@
*/
#include "common.h"
#include "global.h"
+#include "hash.h"
#include "git2/threads.h"
#include "thread-utils.h"
@@ -38,19 +39,39 @@ git_mutex git__mwindow_mutex;
* functions are not available in that case.
*/
+/*
+ * `git_threads_init()` allows subsystems to perform global setup,
+ * which may take place in the global scope. An explicit memory
+ * fence exists at the exit of `git_threads_init()`. Without this,
+ * CPU cores are free to reorder cache invalidation of `_tls_init`
+ * before cache invalidation of the subsystems' newly written global
+ * state.
+ */
#if defined(GIT_THREADS) && defined(GIT_WIN32)
static DWORD _tls_index;
static int _tls_init = 0;
-void git_threads_init(void)
+int git_threads_init(void)
{
+ int error;
+
if (_tls_init)
- return;
+ return 0;
_tls_index = TlsAlloc();
- _tls_init = 1;
git_mutex_init(&git__mwindow_mutex);
+
+ /* Initialize any other subsystems that have global state */
+ if ((error = git_hash_global_init()) >= 0)
+ _tls_init = 1;
+
+ if (error == 0)
+ _tls_init = 1;
+
+ GIT_MEMORY_BARRIER;
+
+ return error;
}
void git_threads_shutdown(void)
@@ -88,13 +109,22 @@ static void cb__free_status(void *st)
git__free(st);
}
-void git_threads_init(void)
+int git_threads_init(void)
{
+ int error = 0;
+
if (_tls_init)
- return;
+ return 0;
pthread_key_create(&_tls_key, &cb__free_status);
- _tls_init = 1;
+
+ /* Initialize any other subsystems that have global state */
+ if ((error = git_hash_global_init()) >= 0)
+ _tls_init = 1;
+
+ GIT_MEMORY_BARRIER;
+
+ return error;
}
void git_threads_shutdown(void)
@@ -125,9 +155,10 @@ git_global_st *git__global_state(void)
static git_global_st __state;
-void git_threads_init(void)
+int git_threads_init(void)
{
/* noop */
+ return 0;
}
void git_threads_shutdown(void)
diff --git a/src/global.h b/src/global.h
index b9f8b67..1025cf7 100644
--- a/src/global.h
+++ b/src/global.h
@@ -10,14 +10,15 @@
#include "mwindow.h"
#include "hash.h"
+#if defined(GIT_THREADS) && defined(_MSC_VER)
+# define GIT_MEMORY_BARRIER MemoryBarrier()
+#elif defined(GIT_THREADS)
+# define GIT_MEMORY_BARRIER __sync_synchronize()
+#endif
+
typedef struct {
git_error *last_error;
git_error error_t;
-
-#ifdef WIN32_SHA1
- git_hash_prov hash_prov;
-#endif
-
} git_global_st;
git_global_st *git__global_state(void);
diff --git a/src/hash.h b/src/hash.h
index e3f1f3f..0e543ed 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -12,6 +12,8 @@
typedef struct git_hash_prov git_hash_prov;
typedef struct git_hash_ctx git_hash_ctx;
+int git_hash_global_init(void);
+
int git_hash_ctx_init(git_hash_ctx *ctx);
void git_hash_ctx_cleanup(git_hash_ctx *ctx);
diff --git a/src/hash/hash_generic.h b/src/hash/hash_generic.h
index c5891c1..92c9cb9 100644
--- a/src/hash/hash_generic.h
+++ b/src/hash/hash_generic.h
@@ -16,6 +16,7 @@ struct git_hash_ctx {
unsigned int W[16];
};
+#define git_hash_global_init() 0
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)
diff --git a/src/hash/hash_openssl.h b/src/hash/hash_openssl.h
index 0d37f13..39c47ae 100644
--- a/src/hash/hash_openssl.h
+++ b/src/hash/hash_openssl.h
@@ -16,6 +16,7 @@ struct git_hash_ctx {
SHA_CTX c;
};
+#define git_hash_global_init() 0
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)
diff --git a/src/hash/hash_ppc.h b/src/hash/hash_ppc.h
index df78d91..df6570e 100644
--- a/src/hash/hash_ppc.h
+++ b/src/hash/hash_ppc.h
@@ -20,6 +20,7 @@ struct git_hash_ctx {
} buf;
};
+#define git_hash_global_init() 0
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)
diff --git a/src/hash/hash_win32.c b/src/hash/hash_win32.c
index 1fac452..c490942 100644
--- a/src/hash/hash_win32.c
+++ b/src/hash/hash_win32.c
@@ -13,8 +13,12 @@
#include <wincrypt.h>
#include <strsafe.h>
+static struct git_hash_prov hash_prov = {0};
+
+/* Hash initialization */
+
/* Initialize CNG, if available */
-GIT_INLINE(int) hash_cng_prov_init(git_hash_prov *prov)
+GIT_INLINE(int) hash_cng_prov_init(void)
{
OSVERSIONINFOEX version_test = {0};
DWORD version_test_mask;
@@ -43,67 +47,67 @@ GIT_INLINE(int) hash_cng_prov_init(git_hash_prov *prov)
if ((dll_path_len = GetSystemDirectory(dll_path, MAX_PATH)) == 0 || dll_path_len > MAX_PATH ||
StringCchCat(dll_path, MAX_PATH, "\\") < 0 ||
StringCchCat(dll_path, MAX_PATH, GIT_HASH_CNG_DLL_NAME) < 0 ||
- (prov->prov.cng.dll = LoadLibrary(dll_path)) == NULL)
+ (hash_prov.prov.cng.dll = LoadLibrary(dll_path)) == NULL)
return -1;
/* Load the function addresses */
- if ((prov->prov.cng.open_algorithm_provider = (hash_win32_cng_open_algorithm_provider_fn)GetProcAddress(prov->prov.cng.dll, "BCryptOpenAlgorithmProvider")) == NULL ||
- (prov->prov.cng.get_property = (hash_win32_cng_get_property_fn)GetProcAddress(prov->prov.cng.dll, "BCryptGetProperty")) == NULL ||
- (prov->prov.cng.create_hash = (hash_win32_cng_create_hash_fn)GetProcAddress(prov->prov.cng.dll, "BCryptCreateHash")) == NULL ||
- (prov->prov.cng.finish_hash = (hash_win32_cng_finish_hash_fn)GetProcAddress(prov->prov.cng.dll, "BCryptFinishHash")) == NULL ||
- (prov->prov.cng.hash_data = (hash_win32_cng_hash_data_fn)GetProcAddress(prov->prov.cng.dll, "BCryptHashData")) == NULL ||
- (prov->prov.cng.destroy_hash = (hash_win32_cng_destroy_hash_fn)GetProcAddress(prov->prov.cng.dll, "BCryptDestroyHash")) == NULL ||
- (prov->prov.cng.close_algorithm_provider = (hash_win32_cng_close_algorithm_provider_fn)GetProcAddress(prov->prov.cng.dll, "BCryptCloseAlgorithmProvider")) == NULL) {
- FreeLibrary(prov->prov.cng.dll);
+ if ((hash_prov.prov.cng.open_algorithm_provider = (hash_win32_cng_open_algorithm_provider_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptOpenAlgorithmProvider")) == NULL ||
+ (hash_prov.prov.cng.get_property = (hash_win32_cng_get_property_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptGetProperty")) == NULL ||
+ (hash_prov.prov.cng.create_hash = (hash_win32_cng_create_hash_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptCreateHash")) == NULL ||
+ (hash_prov.prov.cng.finish_hash = (hash_win32_cng_finish_hash_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptFinishHash")) == NULL ||
+ (hash_prov.prov.cng.hash_data = (hash_win32_cng_hash_data_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptHashData")) == NULL ||
+ (hash_prov.prov.cng.destroy_hash = (hash_win32_cng_destroy_hash_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptDestroyHash")) == NULL ||
+ (hash_prov.prov.cng.close_algorithm_provider = (hash_win32_cng_close_algorithm_provider_fn)GetProcAddress(hash_prov.prov.cng.dll, "BCryptCloseAlgorithmProvider")) == NULL) {
+ FreeLibrary(hash_prov.prov.cng.dll);
return -1;
}
/* Load the SHA1 algorithm */
- if (prov->prov.cng.open_algorithm_provider(&prov->prov.cng.handle, GIT_HASH_CNG_HASH_TYPE, NULL, GIT_HASH_CNG_HASH_REUSABLE) < 0) {
- FreeLibrary(prov->prov.cng.dll);
+ if (hash_prov.prov.cng.open_algorithm_provider(&hash_prov.prov.cng.handle, GIT_HASH_CNG_HASH_TYPE, NULL, GIT_HASH_CNG_HASH_REUSABLE) < 0) {
+ FreeLibrary(hash_prov.prov.cng.dll);
return -1;
}
/* Get storage space for the hash object */
- if (prov->prov.cng.get_property(prov->prov.cng.handle, GIT_HASH_CNG_HASH_OBJECT_LEN, (PBYTE)&prov->prov.cng.hash_object_size, sizeof(DWORD), &size_len, 0) < 0) {
- prov->prov.cng.close_algorithm_provider(prov->prov.cng.handle, 0);
- FreeLibrary(prov->prov.cng.dll);
+ if (hash_prov.prov.cng.get_property(hash_prov.prov.cng.handle, GIT_HASH_CNG_HASH_OBJECT_LEN, (PBYTE)&hash_prov.prov.cng.hash_object_size, sizeof(DWORD), &size_len, 0) < 0) {
+ hash_prov.prov.cng.close_algorithm_provider(hash_prov.prov.cng.handle, 0);
+ FreeLibrary(hash_prov.prov.cng.dll);
return -1;
}
- prov->type = CNG;
+ hash_prov.type = CNG;
return 0;
}
/* Initialize CryptoAPI */
-GIT_INLINE(int) hash_cryptoapi_prov_init(git_hash_prov *prov)
+GIT_INLINE(int) hash_cryptoapi_prov_init()
{
- if (!CryptAcquireContext(&prov->prov.cryptoapi.handle, NULL, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
+ if (!CryptAcquireContext(&hash_prov.prov.cryptoapi.handle, NULL, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
return -1;
- prov->type = CRYPTOAPI;
+ hash_prov.type = CRYPTOAPI;
return 0;
}
-static int hash_win32_prov_init(git_hash_prov *prov)
+int git_hash_global_init()
{
int error = 0;
- assert(prov->type == INVALID);
+ if (hash_prov.type != INVALID)
+ return 0;
- /* Try to load CNG */
- if ((error = hash_cng_prov_init(prov)) < 0)
- error = hash_cryptoapi_prov_init(prov);
+ if ((error = hash_cng_prov_init()) < 0)
+ error = hash_cryptoapi_prov_init();
- return error;
+ return error;
}
/* CryptoAPI: available in Windows XP and newer */
-GIT_INLINE(int) hash_ctx_cryptoapi_init(git_hash_ctx *ctx, git_hash_prov *prov)
+GIT_INLINE(int) hash_ctx_cryptoapi_init(git_hash_ctx *ctx)
{
ctx->type = CRYPTOAPI;
- ctx->prov = prov;
+ ctx->prov = &hash_prov;
return git_hash_init(ctx);
}
@@ -156,18 +160,18 @@ GIT_INLINE(void) hash_ctx_cryptoapi_cleanup(git_hash_ctx *ctx)
/* CNG: Available in Windows Server 2008 and newer */
-GIT_INLINE(int) hash_ctx_cng_init(git_hash_ctx *ctx, git_hash_prov *prov)
+GIT_INLINE(int) hash_ctx_cng_init(git_hash_ctx *ctx)
{
- if ((ctx->ctx.cng.hash_object = git__malloc(prov->prov.cng.hash_object_size)) == NULL)
+ if ((ctx->ctx.cng.hash_object = git__malloc(hash_prov.prov.cng.hash_object_size)) == NULL)
return -1;
- if (prov->prov.cng.create_hash(prov->prov.cng.handle, &ctx->ctx.cng.hash_handle, ctx->ctx.cng.hash_object, prov->prov.cng.hash_object_size, NULL, 0, 0) < 0) {
+ if (hash_prov.prov.cng.create_hash(hash_prov.prov.cng.handle, &ctx->ctx.cng.hash_handle, ctx->ctx.cng.hash_object, hash_prov.prov.cng.hash_object_size, NULL, 0, 0) < 0) {
git__free(ctx->ctx.cng.hash_object);
return -1;
}
ctx->type = CNG;
- ctx->prov = prov;
+ ctx->prov = &hash_prov;
return 0;
}
@@ -216,22 +220,21 @@ GIT_INLINE(void) hash_ctx_cng_cleanup(git_hash_ctx *ctx)
int git_hash_ctx_init(git_hash_ctx *ctx)
{
- git_global_st *global_state;
- git_hash_prov *hash_prov;
-
- assert(ctx);
-
- memset(ctx, 0x0, sizeof(git_hash_ctx));
+ int error = 0;
- if ((global_state = git__global_state()) == NULL)
- return -1;
+ assert(ctx);
- hash_prov = &global_state->hash_prov;
+ /*
+ * When compiled with GIT_THREADS, the global hash_prov data is
+ * initialized with git_threads_init. Otherwise, it must be initialized
+ * at first use.
+ */
+ if (hash_prov.type == INVALID && (error = git_hash_global_init()) < 0)
+ return error;
- if (hash_prov->type == INVALID && hash_win32_prov_init(hash_prov) < 0)
- return -1;
+ memset(ctx, 0x0, sizeof(git_hash_ctx));
- return (hash_prov->type == CNG) ? hash_ctx_cng_init(ctx, hash_prov) : hash_ctx_cryptoapi_init(ctx, hash_prov);
+ return (hash_prov.type == CNG) ? hash_ctx_cng_init(ctx) : hash_ctx_cryptoapi_init(ctx);
}
int git_hash_init(git_hash_ctx *ctx)