Commit e316b0d3d64eb8f65f4109c1565d929b29e1d33a

Edward Thomson 2020-05-15T11:47:09

runtime: move init/shutdown into the "runtime" Provide a mechanism for system components to register for initialization and shutdown of the libgit2 runtime.

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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
diff --git a/src/alloc.c b/src/alloc.c
index 7bfbbda..6972e7b 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -6,6 +6,7 @@
  */
 
 #include "alloc.h"
+#include "runtime.h"
 
 #include "allocators/stdalloc.h"
 #include "allocators/win32_crtdbg.h"
@@ -40,7 +41,8 @@ int git_allocator_global_init(void)
 	git_win32__crtdbg_stacktrace_init();
 	git_win32__stack_init();
 
-	git__on_shutdown(allocator_global_shutdown);
+	if (git_runtime_shutdown_register(allocator_global_shutdown) < 0)
+		return -1;
 #endif
 
 	/*
diff --git a/src/filter.c b/src/filter.c
index 09b57dc..86db5a5 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -11,7 +11,7 @@
 #include "futils.h"
 #include "hash.h"
 #include "repository.h"
-#include "global.h"
+#include "runtime.h"
 #include "git2/sys/filter.h"
 #include "git2/config.h"
 #include "blob.h"
@@ -206,7 +206,7 @@ int git_filter_global_init(void)
 			GIT_FILTER_IDENT, ident, GIT_FILTER_IDENT_PRIORITY) < 0)
 		error = -1;
 
-	git__on_shutdown(git_filter_global_shutdown);
+	error = git_runtime_shutdown_register(git_filter_global_shutdown);
 
 done:
 	if (error) {
diff --git a/src/futils.c b/src/futils.c
index 552a3fb..2ad8a1b 100644
--- a/src/futils.c
+++ b/src/futils.c
@@ -7,7 +7,7 @@
 
 #include "futils.h"
 
-#include "global.h"
+#include "runtime.h"
 #include "strmap.h"
 #include "hash.h"
 #include <ctype.h>
diff --git a/src/global.c b/src/global.c
index c4e925b..e6b7f46 100644
--- a/src/global.c
+++ b/src/global.c
@@ -5,7 +5,7 @@
  * a Linking Exception. For full terms see the included COPYING file.
  */
 
-#include "global.h"
+#include "runtime.h"
 
 #include "alloc.h"
 #include "threadstate.h"
@@ -24,159 +24,29 @@
 #include "transports/ssh.h"
 #include "win32/w32_stack.h"
 
-typedef int (*git_global_init_fn)(void);
-
-static git_global_init_fn git__init_callbacks[] = {
-	git_allocator_global_init,
-	git_threadstate_global_init,
-	git_threads_global_init,
-	git_hash_global_init,
-	git_sysdir_global_init,
-	git_filter_global_init,
-	git_merge_driver_global_init,
-	git_transport_ssh_global_init,
-	git_stream_registry_global_init,
-	git_openssl_stream_global_init,
-	git_mbedtls_stream_global_init,
-	git_mwindow_global_init,
-	git_pool_global_init,
-	git_settings_global_init
-};
-
-static git_global_shutdown_fn git__shutdown_callbacks[ARRAY_SIZE(git__init_callbacks)];
-
-static git_atomic git__n_shutdown_callbacks;
-static git_atomic git__n_inits;
-
-void git__on_shutdown(git_global_shutdown_fn callback)
-{
-	int count = git_atomic_inc(&git__n_shutdown_callbacks);
-	assert(count <= (int) ARRAY_SIZE(git__shutdown_callbacks) && count > 0);
-	git__shutdown_callbacks[count - 1] = callback;
-}
-
-static int init_common(void)
-{
-	size_t i;
-	int ret;
-
-	/* Initialize subsystems that have global state */
-	for (i = 0; i < ARRAY_SIZE(git__init_callbacks); i++)
-		if ((ret = git__init_callbacks[i]()) != 0)
-			break;
-
-	GIT_MEMORY_BARRIER;
-
-	return ret;
-}
-
-static void shutdown_common(void)
-{
-	int pos;
-
-	/* Shutdown subsystems that have registered */
-	for (pos = git_atomic_get(&git__n_shutdown_callbacks);
-		pos > 0;
-		pos = git_atomic_dec(&git__n_shutdown_callbacks)) {
-
-		git_global_shutdown_fn cb = git__swap(
-			git__shutdown_callbacks[pos - 1], NULL);
-
-		if (cb != NULL)
-			cb();
-	}
-}
-
-/*
- * `git_libgit2_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_libgit2_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)
-
-/*
- * On Win32, we use a spinlock to provide locking semantics.  This is
- * lighter-weight than a proper critical section.
- */
-static volatile LONG init_spinlock = 0;
-
-GIT_INLINE(int) init_lock(void)
-{
-	while (InterlockedCompareExchange(&init_spinlock, 1, 0)) { Sleep(0); }
-	return 0;
-}
-
-GIT_INLINE(int) init_unlock(void)
-{
-	InterlockedExchange(&init_spinlock, 0);
-	return 0;
-}
-
-#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
-
-/*
- * On POSIX, we need to use a proper mutex for locking.  We might prefer
- * a spinlock here, too, but there's no static initializer for a
- * pthread_spinlock_t.
- */
-static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-
-GIT_INLINE(int) init_lock(void)
-{
-	return pthread_mutex_lock(&mutex) == 0 ? 0 : -1;
-}
-
-GIT_INLINE(int) init_unlock(void)
-{
-	return pthread_mutex_unlock(&mutex) == 0 ? 0 : -1;
-}
-
-#elif defined(GIT_THREADS)
-# error unknown threading model
-#else
-
-# define init_lock() 0
-# define init_unlock() 0
-
-#endif
-
 int git_libgit2_init(void)
 {
-	int ret;
-
-	if (init_lock() < 0)
-		return -1;
-
-	/* Only do work on a 0 -> 1 transition of the refcount */
-	if ((ret = git_atomic_inc(&git__n_inits)) == 1) {
-		if (init_common() < 0)
-			ret = -1;
-	}
-
-	if (init_unlock() < 0)
-		return -1;
-
-	return ret;
+	static git_runtime_init_fn init_fns[] = {
+		git_allocator_global_init,
+		git_threadstate_global_init,
+		git_threads_global_init,
+		git_hash_global_init,
+		git_sysdir_global_init,
+		git_filter_global_init,
+		git_merge_driver_global_init,
+		git_transport_ssh_global_init,
+		git_stream_registry_global_init,
+		git_openssl_stream_global_init,
+		git_mbedtls_stream_global_init,
+		git_mwindow_global_init,
+		git_pool_global_init,
+		git_settings_global_init
+	};
+
+	return git_runtime_init(init_fns, ARRAY_SIZE(init_fns));
 }
 
 int git_libgit2_shutdown(void)
 {
-	int ret;
-
-	/* Enter the lock */
-	if (init_lock() < 0)
-		return -1;
-
-	/* Only do work on a 1 -> 0 transition of the refcount */
-	if ((ret = git_atomic_dec(&git__n_inits)) == 0)
-		shutdown_common();
-
-	/* Exit the lock */
-	if (init_unlock() < 0)
-		return -1;
-
-	return ret;
+	return git_runtime_shutdown();
 }
diff --git a/src/global.h b/src/global.h
deleted file mode 100644
index 0364cab..0000000
--- a/src/global.h
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- * Copyright (C) the libgit2 contributors. All rights reserved.
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-#ifndef INCLUDE_global_h__
-#define INCLUDE_global_h__
-
-#include "common.h"
-
-typedef void (*git_global_shutdown_fn)(void);
-
-extern void git__on_shutdown(git_global_shutdown_fn callback);
-
-#endif
diff --git a/src/hash/sha1/win32.c b/src/hash/sha1/win32.c
index c736956..b1266cc 100644
--- a/src/hash/sha1/win32.c
+++ b/src/hash/sha1/win32.c
@@ -7,7 +7,7 @@
 
 #include "win32.h"
 
-#include "global.h"
+#include "runtime.h"
 
 #include <wincrypt.h>
 #include <strsafe.h>
@@ -129,7 +129,8 @@ int git_hash_sha1_global_init(void)
 	if ((error = hash_cng_prov_init()) < 0)
 		error = hash_cryptoapi_prov_init();
 
-	git__on_shutdown(sha1_shutdown);
+	if (!error)
+		error = git_runtime_shutdown_register(sha1_shutdown);
 
 	return error;
 }
diff --git a/src/merge_driver.c b/src/merge_driver.c
index 666349b..b1f5748 100644
--- a/src/merge_driver.c
+++ b/src/merge_driver.c
@@ -8,7 +8,7 @@
 #include "merge_driver.h"
 
 #include "vector.h"
-#include "global.h"
+#include "runtime.h"
 #include "merge.h"
 #include "git2/merge.h"
 #include "git2/sys/merge.h"
@@ -209,7 +209,7 @@ int git_merge_driver_global_init(void)
 			merge_driver_name__binary, &git_merge_driver__binary)) < 0)
 		goto done;
 
-	git__on_shutdown(git_merge_driver_global_shutdown);
+	error = git_runtime_shutdown_register(git_merge_driver_global_shutdown);
 
 done:
 	if (error < 0)
diff --git a/src/mwindow.c b/src/mwindow.c
index 78ac411..a852d6b 100644
--- a/src/mwindow.c
+++ b/src/mwindow.c
@@ -10,7 +10,7 @@
 #include "vector.h"
 #include "futils.h"
 #include "map.h"
-#include "global.h"
+#include "runtime.h"
 #include "strmap.h"
 #include "pack.h"
 
@@ -50,14 +50,15 @@ static void git_mwindow_global_shutdown(void)
 
 int git_mwindow_global_init(void)
 {
-	assert(!git__pack_cache);
+	int error;
 
-	git__on_shutdown(git_mwindow_global_shutdown);
+	assert(!git__pack_cache);
 
-	if (git_mutex_init(&git__mwindow_mutex) != 0)
-		return -1;
+	if ((error = git_mutex_init(&git__mwindow_mutex)) < 0 ||
+	    (error = git_strmap_new(&git__pack_cache)) < 0)
+	    return error;
 
-	return git_strmap_new(&git__pack_cache);
+	return git_runtime_shutdown_register(git_mwindow_global_shutdown);
 }
 
 int git_mwindow_get_pack(struct git_pack_file **out, const char *path)
diff --git a/src/net.c b/src/net.c
index dbde626..ac1bc42 100644
--- a/src/net.c
+++ b/src/net.c
@@ -14,7 +14,7 @@
 #include "posix.h"
 #include "buffer.h"
 #include "http_parser.h"
-#include "global.h"
+#include "runtime.h"
 
 #define DEFAULT_PORT_HTTP  "80"
 #define DEFAULT_PORT_HTTPS "443"
diff --git a/src/netops.c b/src/netops.c
index 04ae824..1ef2302 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -13,7 +13,7 @@
 #include "posix.h"
 #include "buffer.h"
 #include "http_parser.h"
-#include "global.h"
+#include "runtime.h"
 
 int gitno_recv(gitno_buffer *buf)
 {
diff --git a/src/runtime.c b/src/runtime.c
new file mode 100644
index 0000000..56110c4
--- /dev/null
+++ b/src/runtime.c
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "common.h"
+#include "runtime.h"
+
+static git_runtime_shutdown_fn shutdown_callback[32];
+static git_atomic shutdown_callback_count;
+
+static git_atomic init_count;
+
+static int init_common(git_runtime_init_fn init_fns[], size_t cnt)
+{
+	size_t i;
+	int ret;
+
+	/* Initialize subsystems that have global state */
+	for (i = 0; i < cnt; i++) {
+		if ((ret = init_fns[i]()) != 0)
+			break;
+	}
+
+	GIT_MEMORY_BARRIER;
+
+	return ret;
+}
+
+static void shutdown_common(void)
+{
+	git_runtime_shutdown_fn cb;
+	int pos;
+
+	for (pos = git_atomic_get(&shutdown_callback_count);
+	     pos > 0;
+	     pos = git_atomic_dec(&shutdown_callback_count)) {
+		cb = git__swap(shutdown_callback[pos - 1], NULL);
+
+		if (cb != NULL)
+			cb();
+	}
+}
+
+int git_runtime_shutdown_register(git_runtime_shutdown_fn callback)
+{
+	int count = git_atomic_inc(&shutdown_callback_count);
+
+	if (count > (int)ARRAY_SIZE(shutdown_callback) || count == 0) {
+		git_error_set(GIT_ERROR_INVALID,
+		              "too many shutdown callbacks registered");
+		git_atomic_dec(&shutdown_callback_count);
+		return -1;
+	}
+
+	shutdown_callback[count - 1] = callback;
+
+	return 0;
+}
+
+#if defined(GIT_THREADS) && defined(GIT_WIN32)
+
+/*
+ * On Win32, we use a spinlock to provide locking semantics.  This is
+ * lighter-weight than a proper critical section.
+ */
+static volatile LONG init_spinlock = 0;
+
+GIT_INLINE(int) init_lock(void)
+{
+	while (InterlockedCompareExchange(&init_spinlock, 1, 0)) { Sleep(0); }
+	return 0;
+}
+
+GIT_INLINE(int) init_unlock(void)
+{
+	InterlockedExchange(&init_spinlock, 0);
+	return 0;
+}
+
+#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
+
+/*
+ * On POSIX, we need to use a proper mutex for locking.  We might prefer
+ * a spinlock here, too, but there's no static initializer for a
+ * pthread_spinlock_t.
+ */
+static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+GIT_INLINE(int) init_lock(void)
+{
+	return pthread_mutex_lock(&init_mutex) == 0 ? 0 : -1;
+}
+
+GIT_INLINE(int) init_unlock(void)
+{
+	return pthread_mutex_unlock(&init_mutex) == 0 ? 0 : -1;
+}
+
+#elif defined(GIT_THREADS)
+# error unknown threading model
+#else
+
+# define mutex_lock() 0
+# define mutex_unlock() 0
+
+#endif
+
+int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt)
+{
+	int ret;
+
+	if (init_lock() < 0)
+		return -1;
+
+	/* Only do work on a 0 -> 1 transition of the refcount */
+	if ((ret = git_atomic_inc(&init_count)) == 1) {
+		if (init_common(init_fns, cnt) < 0)
+			ret = -1;
+	}
+
+	if (init_unlock() < 0)
+		return -1;
+
+	return ret;
+}
+
+int git_runtime_shutdown(void)
+{
+	int ret;
+
+	/* Enter the lock */
+	if (init_lock() < 0)
+		return -1;
+
+	/* Only do work on a 1 -> 0 transition of the refcount */
+	if ((ret = git_atomic_dec(&init_count)) == 0)
+		shutdown_common();
+
+	/* Exit the lock */
+	if (init_unlock() < 0)
+		return -1;
+
+	return ret;
+}
diff --git a/src/runtime.h b/src/runtime.h
new file mode 100644
index 0000000..be2e37a
--- /dev/null
+++ b/src/runtime.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_runtime_h__
+#define INCLUDE_runtime_h__
+
+#include "common.h"
+
+typedef int (*git_runtime_init_fn)(void);
+typedef void (*git_runtime_shutdown_fn)(void);
+
+/**
+ * Start up a new runtime.  If this is the first time that this
+ * function is called within the context of the current library
+ * or executable, then the given `init_fns` will be invoked.  If
+ * it is not the first time, they will be ignored.
+ *
+ * The given initialization functions _may_ register shutdown
+ * handlers using `git_runtime_shutdown_register` to be notified
+ * when the runtime is shutdown.
+ *
+ * @param init_fns The list of initialization functions to call
+ * @param cnt The number of init_fns
+ * @return The number of initializations performed (including this one) or an error
+ */
+int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt);
+
+/**
+ * Shut down the runtime.  If this is the last shutdown call,
+ * such that there are no remaining `init` calls, then any
+ * shutdown hooks that have been registered will be invoked.
+ *
+ * The number of oustanding initializations will be returned.
+ * If this number is 0, then the runtime is shutdown.
+ *
+ * @return The number of outstanding initializations (after this one) or an error
+ */
+int git_runtime_shutdown(void);
+
+/**
+ * Register a shutdown handler for this runtime.  This should be done
+ * by a function invoked by `git_runtime_init` to ensure that the
+ * appropriate locks are taken.
+ *
+ * @param callback The shutdown handler callback
+ * @return 0 or an error code
+ */
+int git_runtime_shutdown_register(git_runtime_shutdown_fn callback);
+
+#endif
diff --git a/src/settings.c b/src/settings.c
index 0426093..4f69da3 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -19,7 +19,7 @@
 #include "alloc.h"
 #include "sysdir.h"
 #include "cache.h"
-#include "global.h"
+#include "runtime.h"
 #include "object.h"
 #include "odb.h"
 #include "refs.h"
@@ -47,8 +47,7 @@ static void git_settings_global_shutdown(void)
 
 int git_settings_global_init(void)
 {
-	git__on_shutdown(git_settings_global_shutdown);
-	return 0;
+	return git_runtime_shutdown_register(git_settings_global_shutdown);
 }
 
 int git_libgit2_version(int *major, int *minor, int *rev)
diff --git a/src/streams/mbedtls.c b/src/streams/mbedtls.c
index cbe2f68..00daa55 100644
--- a/src/streams/mbedtls.c
+++ b/src/streams/mbedtls.c
@@ -11,7 +11,7 @@
 
 #include <ctype.h>
 
-#include "global.h"
+#include "runtime.h"
 #include "stream.h"
 #include "streams/socket.h"
 #include "netops.h"
@@ -152,9 +152,7 @@ int git_mbedtls_stream_global_init(void)
 	if (!loaded && crtpath != NULL && stat(crtpath, &statbuf) == 0 && S_ISDIR(statbuf.st_mode))
 		loaded = (git_mbedtls__set_cert_location(crtpath, 1) == 0);
 
-	git__on_shutdown(shutdown_ssl);
-
-	return 0;
+	return git_runtime_shutdown_register(shutdown_ssl);
 
 cleanup:
 	mbedtls_ctr_drbg_free(ctr_drbg);
diff --git a/src/streams/openssl.c b/src/streams/openssl.c
index 58265c1..0a0c2c5 100644
--- a/src/streams/openssl.c
+++ b/src/streams/openssl.c
@@ -11,7 +11,7 @@
 
 #include <ctype.h>
 
-#include "global.h"
+#include "runtime.h"
 #include "settings.h"
 #include "posix.h"
 #include "stream.h"
@@ -286,9 +286,7 @@ int git_openssl_stream_global_init(void)
 	if (init_bio_method() < 0)
 		goto error;
 
-	git__on_shutdown(shutdown_ssl);
-
-	return 0;
+	return git_runtime_shutdown_register(shutdown_ssl);
 
 error:
 	git_error_set(GIT_ERROR_NET, "could not initialize openssl: %s",
@@ -325,8 +323,8 @@ int git_openssl_set_locking(void)
 	}
 
 	CRYPTO_set_locking_callback(openssl_locking_function);
-	git__on_shutdown(shutdown_ssl_locking);
-	return 0;
+	return git_runtime_shutdown_register(shutdown_ssl_locking);
+
 #elif !defined(OPENSSL_LEGACY_API)
 	return 0;
 #else
diff --git a/src/streams/registry.c b/src/streams/registry.c
index 2844312..b3bf17a 100644
--- a/src/streams/registry.c
+++ b/src/streams/registry.c
@@ -9,7 +9,7 @@
 
 #include "streams/registry.h"
 
-#include "global.h"
+#include "runtime.h"
 #include "streams/tls.h"
 #include "streams/mbedtls.h"
 #include "streams/openssl.h"
@@ -33,8 +33,7 @@ int git_stream_registry_global_init(void)
 	if (git_rwlock_init(&stream_registry.lock) < 0)
 		return -1;
 
-	git__on_shutdown(shutdown_stream_registry);
-	return 0;
+	return git_runtime_shutdown_register(shutdown_stream_registry);
 }
 
 GIT_INLINE(void) stream_registration_cpy(
diff --git a/src/streams/tls.c b/src/streams/tls.c
index 6a25171..255a4a0 100644
--- a/src/streams/tls.c
+++ b/src/streams/tls.c
@@ -8,7 +8,6 @@
 #include "git2/errors.h"
 
 #include "common.h"
-#include "global.h"
 #include "streams/registry.h"
 #include "streams/tls.h"
 #include "streams/mbedtls.h"
diff --git a/src/sysdir.c b/src/sysdir.c
index 6dc78c8..401b4a5 100644
--- a/src/sysdir.c
+++ b/src/sysdir.c
@@ -7,7 +7,7 @@
 
 #include "sysdir.h"
 
-#include "global.h"
+#include "runtime.h"
 #include "buffer.h"
 #include "path.h"
 #include <ctype.h>
@@ -189,9 +189,7 @@ int git_sysdir_global_init(void)
 	for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++)
 		error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf);
 
-	git__on_shutdown(git_sysdir_global_shutdown);
-
-	return error;
+	return git_runtime_shutdown_register(git_sysdir_global_shutdown);
 }
 
 static int git_sysdir_check_selector(git_sysdir_t which)
diff --git a/src/threadstate.c b/src/threadstate.c
index 4aa1c03..8e17124 100644
--- a/src/threadstate.c
+++ b/src/threadstate.c
@@ -6,7 +6,7 @@
  */
 
 #include "threadstate.h"
-#include "global.h"
+#include "runtime.h"
 
 static void threadstate_dispose(git_threadstate *threadstate);
 
@@ -57,9 +57,7 @@ int git_threadstate_global_init(void)
 	if ((fls_index = FlsAlloc(fls_free)) == FLS_OUT_OF_INDEXES)
 		return -1;
 
-	git__on_shutdown(git_threadstate_global_shutdown);
-
-	return 0;
+	return git_runtime_shutdown_register(git_threadstate_global_shutdown);
 }
 
 git_threadstate *git_threadstate_get(void)
@@ -105,9 +103,7 @@ int git_threadstate_global_init(void)
 	if (pthread_key_create(&tls_key, &tls_free) != 0)
 		return -1;
 
-	git__on_shutdown(git_threadstate_global_shutdown);
-
-	return 0;
+	return git_runtime_shutdown_register(git_threadstate_global_shutdown);
 }
 
 git_threadstate *git_threadstate_get(void)
@@ -134,14 +130,12 @@ static git_threadstate threadstate;
 static void git_threadstate_global_shutdown(void)
 {
 	threadstate_dispose(&threadstate);
-	memset(&threadstate, 0, sizeof(git_threadstate);
+	memset(&threadstate, 0, sizeof(git_threadstate));
 }
 
 int git_threadstate_global_init(void)
 {
-	git__on_shutdown(git_threadstate_global_shutdown);
-
-	return 0;
+	return git_runtime_shutdown_register(git_tlsdata_global_shutdown);
 }
 
 git_threadstate *git_threadstate_get(void)
diff --git a/src/trace.c b/src/trace.c
index ec6a90a..3acb643 100644
--- a/src/trace.c
+++ b/src/trace.c
@@ -8,7 +8,7 @@
 #include "trace.h"
 
 #include "buffer.h"
-#include "global.h"
+#include "runtime.h"
 #include "git2/trace.h"
 
 #ifdef GIT_TRACE
diff --git a/src/transports/http.c b/src/transports/http.c
index 66731b0..fb1740c 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -14,7 +14,6 @@
 #include "buffer.h"
 #include "net.h"
 #include "netops.h"
-#include "global.h"
 #include "remote.h"
 #include "git2/sys/credential.h"
 #include "smart.h"
diff --git a/src/transports/httpclient.c b/src/transports/httpclient.c
index ee936c8..6ec22f8 100644
--- a/src/transports/httpclient.c
+++ b/src/transports/httpclient.c
@@ -10,7 +10,6 @@
 #include "http_parser.h"
 #include "vector.h"
 #include "trace.h"
-#include "global.h"
 #include "httpclient.h"
 #include "http.h"
 #include "auth.h"
diff --git a/src/transports/ssh.c b/src/transports/ssh.c
index 68b3cbe..f4ed05b 100644
--- a/src/transports/ssh.c
+++ b/src/transports/ssh.c
@@ -11,7 +11,7 @@
 #include <libssh2.h>
 #endif
 
-#include "global.h"
+#include "runtime.h"
 #include "git2.h"
 #include "buffer.h"
 #include "net.h"
@@ -934,8 +934,7 @@ int git_transport_ssh_global_init(void)
 		return -1;
 	}
 
-	git__on_shutdown(shutdown_ssh);
-	return 0;
+	return git_runtime_shutdown_register(shutdown_ssh);
 
 #else
 
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index f9736cd..2a1f6f2 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -17,7 +17,6 @@
 #include "smart.h"
 #include "remote.h"
 #include "repository.h"
-#include "global.h"
 #include "http.h"
 #include "git2/sys/credential.h"
 
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index cacf986..5a5e921 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -14,7 +14,6 @@
 #include "utf-conv.h"
 #include "repository.h"
 #include "reparse.h"
-#include "global.h"
 #include "buffer.h"
 #include <errno.h>
 #include <io.h>
diff --git a/src/win32/thread.c b/src/win32/thread.c
index dad32ef..51b005b 100644
--- a/src/win32/thread.c
+++ b/src/win32/thread.c
@@ -6,6 +6,7 @@
  */
 
 #include "thread.h"
+#include "runtime.h"
 
 #define CLEAN_THREAD_EXIT 0x6F012842
 
@@ -60,9 +61,7 @@ int git_threads_global_init(void)
 	if ((fls_index = FlsAlloc(NULL)) == FLS_OUT_OF_INDEXES)
 		return -1;
 
-	git__on_shutdown(git_threads_global_shutdown);
-
-	return 0;
+	return git_runtime_shutdown_register(git_threads_global_shutdown);
 }
 
 int git_thread_create(
diff --git a/tests/pack/filelimit.c b/tests/pack/filelimit.c
index 044679f..b39ab73 100644
--- a/tests/pack/filelimit.c
+++ b/tests/pack/filelimit.c
@@ -1,6 +1,5 @@
 #include "clar_libgit2.h"
 #include "mwindow.h"
-#include "global.h"
 
 #include <git2.h>
 #include "git2/sys/commit.h"