Skip to content

Commit cfbb3fe

Browse files
committed
NativeMethods: drop unnecessary libgit2 refcount
Remove the refcounting of libgit2 usage leftover from when `SafeHandleBase` ensured that all handles were finished before calling `git_libgit2_shutdown`. libgit2 itself does this refcounting now, we no longer need to. We only need to call `git_libgit2_init` before the first call to libgit2 and `git_libgit2_shutdown` at the end.
1 parent 2a8b1f0 commit cfbb3fe

File tree

1 file changed

+7
-30
lines changed

1 file changed

+7
-30
lines changed

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,28 @@ internal static class NativeMethods
1414
{
1515
public const uint GIT_PATH_MAX = 4096;
1616
private const string libgit2 = NativeDllName.Name;
17-
// This is here to keep the pointer alive
17+
18+
// An object tied to the lifecycle of the NativeMethods static class.
19+
// This will handle initialization and shutdown of the underlying
20+
// native library.
1821
#pragma warning disable 0414
1922
private static readonly LibraryLifetimeObject lifetimeObject;
2023
#pragma warning restore 0414
21-
private static int handlesCount;
2224

23-
/// <summary>
24-
/// Internal hack to ensure that the call to git_threads_shutdown is called after all handle finalizers
25-
/// have run to completion ensuring that no dangling git-related finalizer runs after git_threads_shutdown.
26-
/// There should never be more than one instance of this object per AppDomain.
27-
/// </summary>
2825
private sealed class LibraryLifetimeObject : CriticalFinalizerObject
2926
{
30-
// Ensure mono can JIT the .cctor and adjust the PATH before trying to load the native library.
31-
// See https://github.com/libgit2/libgit2sharp/pull/190
3227
[MethodImpl(MethodImplOptions.NoInlining)]
3328
public LibraryLifetimeObject()
3429
{
35-
int res = git_libgit2_init();
36-
Ensure.Int32Result(res);
37-
if (res == 1)
30+
// Configure the OpenSSL locking on the true initialization
31+
// of the library.
32+
if (git_libgit2_init() == 1)
3833
{
39-
// Ignore the error that this propagates. Call it in case openssl is being used.
4034
git_openssl_set_locking();
4135
}
42-
AddHandle();
4336
}
4437

4538
~LibraryLifetimeObject()
46-
{
47-
RemoveHandle();
48-
}
49-
}
50-
51-
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
52-
internal static void AddHandle()
53-
{
54-
Interlocked.Increment(ref handlesCount);
55-
}
56-
57-
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
58-
internal static void RemoveHandle()
59-
{
60-
int count = Interlocked.Decrement(ref handlesCount);
61-
if (count == 0)
6239
{
6340
git_libgit2_shutdown();
6441
}

0 commit comments

Comments
 (0)