8000 NativeMethods: drop unnecessary libgit2 refcount · libgit2/libgit2sharp@57662ac · GitHub
[go: up one dir, main page]

Skip to content

Commit 57662ac

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 c72b740 commit 57662ac

File tree

1 file changed

+11
-41
lines changed

1 file changed

+11
-41
lines changed

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,28 @@ internal static partial class NativeMethods
1717
{
1818
public const uint GIT_PATH_MAX = 4096;
1919
private const string libgit2 = NativeDllName.Name;
20-
// This is here to keep the pointer alive
21-
#pragma warning disable 0414
20+
21+
// An object tied to the lifecycle of the NativeMethods static class.
22+
// This will handle initialization and shutdown of the underlying
23+
// native library.
24+
#pragma warning disable 0414
2225
private static readonly LibraryLifetimeObject lifetimeObject;
23-
#pragma warning restore 0414
24-
private static int handlesCount;
25-
26-
/// <summary>
27-
/// Internal hack to ensure that the call to git_threads_shutdown is called after all handle finalizers
28-
/// have run to completion ensuring that no dangling git-related finalizer runs after git_threads_shutdown.
29-
/// There should never be more than one instance of this object per AppDomain.
30-
/// </summary>
31-
private sealed class LibraryLifetimeObject
32-
#if DESKTOP
33-
: CriticalFinalizerObject
34-
#endif
26+
#pragma warning restore 0414
27+
28+
private sealed class LibraryLifetimeObject : CriticalFinalizerObject
3529
{
36-
// Ensure mono can JIT the .cctor and adjust the PATH before trying to load the native library.
37-
// See https://github.com/libgit2/libgit2sharp/pull/190
3830
[MethodImpl(MethodImplOptions.NoInlining)]
3931
public LibraryLifetimeObject()
4032
{
41-
int res = git_libgit2_init();
42-
Ensure.Int32Result(res);
43-
if (res == 1)
33+
// Configure the OpenSSL locking on the true initialization
34+
// of the library.
35+
if (git_libgit2_init() == 1)
4436
{
45-
// Ignore the error that this propagates. Call it in case openssl is being used.
4637
git_openssl_set_locking();
4738
}
48-
AddHandle();
4939
}
5040

5141
~LibraryLifetimeObject()
52-
{
53-
RemoveHandle();
54-
}
55-
}
56-
57-
#if DESKTOP
58-
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
59-
#endif
60-
internal static void AddHandle()
61-
{
62-
Interlocked.Increment(ref handlesCount);
63-
}
64-
65-
#if DESKTOP
66-
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
67-
#endif
68-
internal static void RemoveHandle()
69-
{
70-
int count = Interlocked.Decrement(ref handlesCount);
71-
if (count == 0)
7242
{
7343
git_libgit2_shutdown();
7444
}

0 commit comments

Comments
 (0)
0