Skip to content

Commit 6c51014

Browse files
committed
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 parent a5cb2cc commit 6c51014

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/libgit2.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ int git_libgit2_init(void)
9090
return git_runtime_init(init_fns, ARRAY_SIZE(init_fns));
9191
}
9292

93+
int git_libgit2_init_count(void)
94+
{
95+
return git_runtime_init_count();
96+
}
97+
9398
int git_libgit2_shutdown(void)
9499
{
95100
return git_runtime_shutdown();

src/libgit2.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef INCLUDE_libgit2_h__
88
#define INCLUDE_libgit2_h__
99

10+
extern int git_libgit2_init_count(void);
11+
1012
extern const char *git_libgit2__user_agent(void);
1113
extern const char *git_libgit2__ssl_ciphers(void);
1214

src/runtime.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt)
127127
return ret;
128128
}
129129

130+
int git_runtime_init_count(void)
131+
{
132+
int ret;
133+
134+
if (init_lock() < 0)
135+
return -1;
136+
137+
ret = git_atomic32_get(&init_count);
138+
139+
if (init_unlock() < 0)
140+
return -1;
141+
142+
return ret;
143+
}
144+
130145
int git_runtime_shutdown(void)
131146
{
132147
int ret;

src/runtime.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ typedef void (*git_runtime_shutdown_fn)(void);
2828
*/
2929
int git_runtime_init(git_runtime_init_fn init_fns[], size_t cnt);
3030

31+
/*
32+
* Returns the number of initializations active (the number of calls to
33+
* `git_runtime_init` minus the number of calls sto `git_runtime_shutdown`).
34+
* If 0, the runtime is not currently initialized.
35+
*
36+
* @return The number of initializations performed or an error
37+
*/
38+
int git_runtime_init_count(void);
39+
3140
/**
3241
* Shut down the runtime. If this is the last shutdown call,
3342
* such that there are no remaining `init` calls, then any

0 commit comments

Comments
 (0)