Skip to content

Commit b6bac28

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 b6bac28

1 file changed

Lines changed: 8 additions & 32 deletions

File tree

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,27 @@ 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;
20-
#pragma warning restore 0414
21-
private static int handlesCount;
22-
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>
23+
2824
private sealed class LibraryLifetimeObject : CriticalFinalizerObject
2925
{
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
3226
[MethodImpl(MethodImplOptions.NoInlining)]
3327
public LibraryLifetimeObject()
3428
{
35-
int res = git_libgit2_init();
36-
Ensure.Int32Result(res);
37-
if (res == 1)
29+
// Configure the OpenSSL locking on the true initialization
30+
// of the library.
31+
if (git_libgit2_init() == 1)
3832
{
39-
// Ignore the error that this propagates. Call it in case openssl is being used.
4033
git_openssl_set_locking();
4134
}
42-
AddHandle();
4335
}
4436

4537
~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)
6238
{
6339
git_libgit2_shutdown();
6440
}

0 commit comments

Comments
 (0)