libgit2: provide init_count of the library A function to provide the initialization count of the library; this is subject to race conditions but is useful for a naive determination as to whether the library has been initialized or not.
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
diff --git a/src/libgit2.c b/src/libgit2.c
index e41988c..f27c999 100644
--- a/src/libgit2.c
+++ b/src/libgit2.c
@@ -90,6 +90,11 @@ int git_libgit2_init(void)
return git_runtime_init(init_fns, ARRAY_SIZE(init_fns));
}
+int git_libgit2_init_count(void)
+{
+ return git_runtime_init_count();
+}
+
int git_libgit2_shutdown(void)
{
return git_runtime_shutdown();
diff --git a/src/libgit2.h b/src/libgit2.h
index 6f92a83..a898367 100644
--- a/src/libgit2.h
+++ b/src/libgit2.h
@@ -7,6 +7,8 @@
#ifndef INCLUDE_libgit2_h__
#define INCLUDE_libgit2_h__
+extern int git_libgit2_init_count(void);
+
extern const char *git_libgit2__user_agent(void);
extern const char *git_libgit2__ssl_ciphers(void);
diff --git a/src/runtime.c b/src/runtime.c
index b61c3c4..c05dee8 100644
--- a/src/runtime.c
+++ b/src/runtime.c
@@ -127,6 +127,21 @@ int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt)
return ret;
}
+int git_runtime_init_count(void)
+{
+ int ret;
+
+ if (init_lock() < 0)
+ return -1;
+
+ ret = git_atomic32_get(&init_count);
+
+ if (init_unlock() < 0)
+ return -1;
+
+ return ret;
+}
+
int git_runtime_shutdown(void)
{
int ret;
diff --git a/src/runtime.h b/src/runtime.h
index be2e37a..2cbcafe 100644
--- a/src/runtime.h
+++ b/src/runtime.h
@@ -28,6 +28,15 @@ typedef void (*git_runtime_shutdown_fn)(void);
*/
int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt);
+/*
+ * Returns the number of initializations active (the number of calls to
+ * `git_runtime_init` minus the number of calls sto `git_runtime_shutdown`).
+ * If 0, the runtime is not currently initialized.
+ *
+ * @return The number of initializations performed or an error
+ */
+int git_runtime_init_count(void);
+
/**
* Shut down the runtime. If this is the last shutdown call,
* such that there are no remaining `init` calls, then any