Skip to content

Commit

Permalink
Undo most of the GIL changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tonybaloney committed Aug 12, 2024
1 parent 5a7aa98 commit a9f23e6
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/CSnakes.Runtime/Python/GIL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ namespace CSnakes.Runtime.Python;

public static class GIL
{
// TODO: Handle environments without the GIL (although GIL state ensure becomes a noop)
internal class PyGilState : IDisposable
{
private readonly IntPtr _state;
private readonly nint gilState;

public PyGilState()
{
Debug.Assert(CPythonAPI.IsInitialized);
_state = CPythonAPI.PyGILState_Ensure();
#if DEBUG
if (CPythonAPI.PyGILState_Check() == 1)
{
Debug.WriteLine($"GIL already acquired for thread {Environment.CurrentManagedThreadId}");
}
#endif
gilState = CPythonAPI.PyGILState_Ensure();
Debug.WriteLine($"GIL acquired for thread {Environment.CurrentManagedThreadId} ({CPythonAPI.GetNativeThreadId()})");
}

public void Dispose()
{
CPythonAPI.PyGILState_Release(_state);
GC.SuppressFinalize(this);
Debug.WriteLine($"Releasing GIL for thread {Thread.CurrentThread.ManagedThreadId}");
CPythonAPI.Py_MakePendingCalls();
CPythonAPI.PyGILState_Release(gilState);
}
}

Expand Down

0 comments on commit a9f23e6

Please sign in to comment.