Commit 799e22ea0c3f20f1900011573a10053dc3ea9138

Carlos Martín Nieto 2014-10-23T17:34:41

Rename git_threads_ to git_libgit2_ This describes their purpose better, as we now initialize ssl and some other global stuff in there. Calling the init function is not something which has been optional for a while now.

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
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f268f6f..4b9d924 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -122,3 +122,7 @@ v0.21 + 1
 * The fetch behavior of remotes with autotag set to GIT_REMOTE_DOWNLOAD_TAGS_ALL
   has been changed to match git 1.9.0 and later. In this mode, libgit2 now
   fetches all tags in addition to whatever else needs to be fetched.
+
+* git_threads_init() and git_threads_shutdown() have been renamed to
+  git_libgit2_init() and git_libgit2_shutdown() to better explain what
+  their purpose is, as it's grown to be more than just about threads.
diff --git a/examples/add.c b/examples/add.c
index 0c6076e..0101ab9 100644
--- a/examples/add.c
+++ b/examples/add.c
@@ -40,7 +40,7 @@ int main (int argc, char** argv)
 	int options = 0, count = 0;
 	struct print_payload payload = {0};
 
-	git_threads_init();
+	git_libgit2_init();
 
 	parse_opts(&options, &count, argc, argv);
 
@@ -66,7 +66,7 @@ int main (int argc, char** argv)
 	git_index_free(index);
 	git_repository_free(repo);
 
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return 0;
 }
diff --git a/examples/blame.c b/examples/blame.c
index fda605b..9d38f25 100644
--- a/examples/blame.c
+++ b/examples/blame.c
@@ -48,7 +48,7 @@ int main(int argc, char *argv[])
 	git_blob *blob;
 	git_object *obj;
 
-	git_threads_init();
+	git_libgit2_init();
 
 	parse_opts(&o, argc, argv);
 	if (o.M) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES;
@@ -131,7 +131,7 @@ int main(int argc, char *argv[])
 	git_blame_free(blame);
 	git_repository_free(repo);
 
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return 0;
 }
diff --git a/examples/cat-file.c b/examples/cat-file.c
index 52399fa..f948740 100644
--- a/examples/cat-file.c
+++ b/examples/cat-file.c
@@ -127,7 +127,7 @@ int main(int argc, char *argv[])
 	git_object *obj = NULL;
 	char oidstr[GIT_OID_HEXSZ + 1];
 
-	git_threads_init();
+	git_libgit2_init();
 
 	parse_opts(&o, argc, argv);
 
@@ -190,7 +190,7 @@ int main(int argc, char *argv[])
 	git_object_free(obj);
 	git_repository_free(repo);
 
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return 0;
 }
diff --git a/examples/describe.c b/examples/describe.c
index cbf73b0..09a4fd0 100644
--- a/examples/describe.c
+++ b/examples/describe.c
@@ -145,7 +145,7 @@ int main(int argc, char **argv)
 	git_repository *repo;
 	describe_options opts;
 
-	git_threads_init();
+	git_libgit2_init();
 
 	check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
 			"Could not open repository", NULL);
@@ -156,7 +156,7 @@ int main(int argc, char **argv)
 	do_describe(repo, &opts);
 
 	git_repository_free(repo);
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return 0;
 }
diff --git a/examples/diff.c b/examples/diff.c
index 76ac2f3..b69cb22 100644
--- a/examples/diff.c
+++ b/examples/diff.c
@@ -77,7 +77,7 @@ int main(int argc, char *argv[])
 		-1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "."
 	};
 
-	git_threads_init();
+	git_libgit2_init();
 
 	parse_opts(&o, argc, argv);
 
@@ -163,7 +163,7 @@ int main(int argc, char *argv[])
 	git_tree_free(t2);
 	git_repository_free(repo);
 
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return 0;
 }
diff --git a/examples/general.c b/examples/general.c
index 051d693..8981cac 100644
--- a/examples/general.c
+++ b/examples/general.c
@@ -59,6 +59,10 @@ static void check_error(int error_code, const char *action)
 
 int main (int argc, char** argv)
 {
+  // Initialize the library, this will set up any global state which libgit2 needs
+  // including threading and crypto
+  git_libgit2_init();
+
   // ### Opening the Repository
 
   // There are a couple of methods for opening a repository, this being the
diff --git a/examples/init.c b/examples/init.c
index 0e823ab..fe7a672 100644
--- a/examples/init.c
+++ b/examples/init.c
@@ -46,7 +46,7 @@ int main(int argc, char *argv[])
 	git_repository *repo = NULL;
 	struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
 
-	git_threads_init();
+	git_libgit2_init();
 
 	parse_opts(&o, argc, argv);
 
@@ -116,7 +116,7 @@ int main(int argc, char *argv[])
 	}
 
 	git_repository_free(repo);
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return 0;
 }
diff --git a/examples/log.c b/examples/log.c
index d5f75a2..e54eed3 100644
--- a/examples/log.c
+++ b/examples/log.c
@@ -80,7 +80,7 @@ int main(int argc, char *argv[])
 	git_commit *commit = NULL;
 	git_pathspec *ps = NULL;
 
-	git_threads_init();
+	git_libgit2_init();
 
 	/** Parse arguments and set up revwalker. */
 
@@ -180,7 +180,7 @@ int main(int argc, char *argv[])
 	git_pathspec_free(ps);
 	git_revwalk_free(s.walker);
 	git_repository_free(s.repo);
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return 0;
 }
diff --git a/examples/network/git2.c b/examples/network/git2.c
index 5b32ac8..d44334b 100644
--- a/examples/network/git2.c
+++ b/examples/network/git2.c
@@ -54,7 +54,7 @@ int main(int argc, char **argv)
 		exit(EXIT_FAILURE);
 	}
 
-	git_threads_init();
+	git_libgit2_init();
 
 	for (i = 0; commands[i].name != NULL; ++i) {
 		if (!strcmp(argv[1], commands[i].name))
diff --git a/examples/rev-list.c b/examples/rev-list.c
index 940a011..ee9afc4 100644
--- a/examples/rev-list.c
+++ b/examples/rev-list.c
@@ -24,7 +24,7 @@ int main (int argc, char **argv)
 	git_oid oid;
 	char buf[GIT_OID_HEXSZ+1];
 
-	git_threads_init();
+	git_libgit2_init();
 
 	check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "opening repository", NULL);
 	check_lg2(git_revwalk_new(&walk, repo), "allocating revwalk", NULL);
@@ -36,7 +36,7 @@ int main (int argc, char **argv)
 		printf("%s\n", buf);
 	}
 
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 	return 0;
 }
 
diff --git a/examples/rev-parse.c b/examples/rev-parse.c
index a248337..483d6e0 100644
--- a/examples/rev-parse.c
+++ b/examples/rev-parse.c
@@ -29,13 +29,13 @@ int main(int argc, char *argv[])
 {
 	struct parse_state ps = {0};
 
-	git_threads_init();
+	git_libgit2_init();
 	parse_opts(&ps, argc, argv);
 
 	check_lg2(parse_revision(&ps), "Parsing", NULL);
 
 	git_repository_free(ps.repo);
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return 0;
 }
diff --git a/examples/showindex.c b/examples/showindex.c
index 604124f..43be5e2 100644
--- a/examples/showindex.c
+++ b/examples/showindex.c
@@ -23,7 +23,7 @@ int main (int argc, char** argv)
 	char out[GIT_OID_HEXSZ+1];
 	out[GIT_OID_HEXSZ] = '\0';
 
-	git_threads_init();
+	git_libgit2_init();
 
 	if (argc > 2)
 		fatal("usage: showindex [<repo-dir>]", NULL);
@@ -64,7 +64,7 @@ int main (int argc, char** argv)
 	}
 
 	git_index_free(index);
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return 0;
 }
diff --git a/examples/status.c b/examples/status.c
index a59f344..62cb5b2 100644
--- a/examples/status.c
+++ b/examples/status.c
@@ -73,7 +73,7 @@ int main(int argc, char *argv[])
 	git_status_list *status;
 	struct opts o = { GIT_STATUS_OPTIONS_INIT, "." };
 
-	git_threads_init();
+	git_libgit2_init();
 
 	o.statusopt.show  = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
 	o.statusopt.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
@@ -135,7 +135,7 @@ show_status:
 	}
 
 	git_repository_free(repo);
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return 0;
 }
diff --git a/examples/tag.c b/examples/tag.c
index 1d254ae..c6a70d9 100644
--- a/examples/tag.c
+++ b/examples/tag.c
@@ -300,7 +300,7 @@ int main(int argc, char **argv)
 	tag_action action;
 	tag_state state;
 
-	git_threads_init();
+	git_libgit2_init();
 
 	check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
 			"Could not open repository", NULL);
@@ -313,7 +313,7 @@ int main(int argc, char **argv)
 	action(&state);
 
 	git_repository_free(repo);
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return 0;
 }
diff --git a/include/git2.h b/include/git2.h
index 41adbba..388cd82 100644
--- a/include/git2.h
+++ b/include/git2.h
@@ -56,7 +56,7 @@
 #include "git2/status.h"
 #include "git2/submodule.h"
 #include "git2/tag.h"
-#include "git2/threads.h"
+#include "git2/global.h"
 #include "git2/transport.h"
 #include "git2/tree.h"
 #include "git2/types.h"
diff --git a/include/git2/global.h b/include/git2/global.h
new file mode 100644
index 0000000..4f90c4c
--- /dev/null
+++ b/include/git2/global.h
@@ -0,0 +1,38 @@
+/*
+ * 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_git_global_h__
+#define INCLUDE_git_global_h__
+
+#include "common.h"
+
+GIT_BEGIN_DECL
+
+/**
+ * Init the global state
+ *
+ * This function must the called before any other libgit2 function in
+ * order to set up global state and threading.
+ *
+ * This function may be called multiple times.
+ *
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_libgit2_init(void);
+
+/**
+ * Shutdown the global state
+ *
+ * Clean up the global state and threading context after calling it as
+ * many times as `git_libgit2_init()` was called.
+ * 
+ */
+GIT_EXTERN(void) git_libgit2_shutdown(void);
+
+/** @} */
+GIT_END_DECL
+#endif
+
diff --git a/include/git2/threads.h b/include/git2/threads.h
index 6b42870..c0c3898 100644
--- a/include/git2/threads.h
+++ b/include/git2/threads.h
@@ -19,32 +19,6 @@
 GIT_BEGIN_DECL
 
 /**
- * Init the threading system.
- *
- * If libgit2 has been built with GIT_THREADS
- * on, this function must be called once before
- * any other library functions.
- *
- * If libgit2 has been built without GIT_THREADS
- * support, this function is a no-op.
- *
- * @return 0 or an error code
- */
-GIT_EXTERN(int) git_threads_init(void);
-
-/**
- * Shutdown the threading system.
- *
- * If libgit2 has been built with GIT_THREADS
- * on, this function must be called before shutting
- * down the library.
- *
- * If libgit2 has been built without GIT_THREADS
- * support, this function is a no-op.
- */
-GIT_EXTERN(void) git_threads_shutdown(void);
-
-/**
  * Initialize the OpenSSL locks
  *
  * OpenSSL requires the application to determine how it performs
diff --git a/src/global.c b/src/global.c
index 3c91860..da903cb 100644
--- a/src/global.c
+++ b/src/global.c
@@ -9,6 +9,7 @@
 #include "hash.h"
 #include "sysdir.h"
 #include "git2/threads.h"
+#include "git2/global.h"
 #include "thread-utils.h"
 
 
@@ -133,7 +134,7 @@ int git_openssl_set_locking(void)
  * Handle the global state with TLS
  *
  * If libgit2 is built with GIT_THREADS enabled,
- * the `git_threads_init()` function must be called
+ * the `git_libgit2_init()` function must be called
  * before calling any other function of the library.
  *
  * This function allocates a TLS index (using pthreads
@@ -146,7 +147,7 @@ int git_openssl_set_locking(void)
  * allocated on each thread.
  *
  * Before shutting down the library, the
- * `git_threads_shutdown` method must be called to free
+ * `git_libgit2_shutdown` method must be called to free
  * the previously reserved TLS index.
  *
  * If libgit2 is built without threading support, the
@@ -156,9 +157,9 @@ int git_openssl_set_locking(void)
  */
 
 /*
- * `git_threads_init()` allows subsystems to perform global setup,
+ * `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_threads_init()`.  Without this,
+ * 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.
@@ -185,7 +186,7 @@ static int synchronized_threads_init(void)
 	return error;
 }
 
-int git_threads_init(void)
+int git_libgit2_init(void)
 {
 	int error = 0;
 
@@ -210,7 +211,7 @@ static void synchronized_threads_shutdown(void)
 	git_mutex_free(&git__mwindow_mutex);
 }
 
-void git_threads_shutdown(void)
+void git_libgit2_shutdown(void)
 {
 	/* Enter the lock */
 	while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
@@ -272,14 +273,14 @@ static void init_once(void)
 	GIT_MEMORY_BARRIER;
 }
 
-int git_threads_init(void)
+int git_libgit2_init(void)
 {
 	pthread_once(&_once_init, init_once);
 	git_atomic_inc(&git__n_inits);
 	return init_error;
 }
 
-void git_threads_shutdown(void)
+void git_libgit2_shutdown(void)
 {
 	void *ptr = NULL;
 	pthread_once_t new_once = PTHREAD_ONCE_INIT;
@@ -320,7 +321,7 @@ git_global_st *git__global_state(void)
 
 static git_global_st __state;
 
-int git_threads_init(void)
+int git_libgit2_init(void)
 {
 	static int ssl_inited = 0;
 
@@ -333,7 +334,7 @@ int git_threads_init(void)
 	return 0;
 }
 
-void git_threads_shutdown(void)
+void git_libgit2_shutdown(void)
 {
 	/* Shut down any subsystems that have global state */
 	if (0 == git_atomic_dec(&git__n_inits))
diff --git a/src/hash/hash_win32.c b/src/hash/hash_win32.c
index bb22313..6bae53e 100644
--- a/src/hash/hash_win32.c
+++ b/src/hash/hash_win32.c
@@ -236,7 +236,7 @@ int git_hash_ctx_init(git_hash_ctx *ctx)
 
 	/*
 	 * When compiled with GIT_THREADS, the global hash_prov data is
-	 * initialized with git_threads_init.  Otherwise, it must be initialized
+	 * initialized with git_libgit2_init.  Otherwise, it must be initialized
 	 * at first use.
 	 */
 	if (hash_prov.type == INVALID && (error = git_hash_global_init()) < 0)
diff --git a/tests/main.c b/tests/main.c
index 3de4f98..a092b8b 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -10,7 +10,7 @@ int main(int argc, char *argv[])
 
 	clar_test_init(argc, argv);
 
-	git_threads_init();
+	git_libgit2_init();
 	cl_sandbox_set_search_path_defaults();
 
 	/* Run the test suite */
@@ -19,7 +19,7 @@ int main(int argc, char *argv[])
 	clar_test_shutdown();
 
 	giterr_clear();
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 
 	return res;
 }
diff --git a/tests/threads/basic.c b/tests/threads/basic.c
index eb15293..9c342bc 100644
--- a/tests/threads/basic.c
+++ b/tests/threads/basic.c
@@ -27,11 +27,11 @@ void test_threads_basic__multiple_init(void)
 {
 	git_repository *nested_repo;
 
-	git_threads_init();
+	git_libgit2_init();
 	cl_git_pass(git_repository_open(&nested_repo, cl_fixture("testrepo.git")));
 	git_repository_free(nested_repo);
 
-	git_threads_shutdown();
+	git_libgit2_shutdown();
 	cl_git_pass(git_repository_open(&nested_repo, cl_fixture("testrepo.git")));
 	git_repository_free(nested_repo);
 }