FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Allow GIL state debugging; require GILState to be properly disposed by lostmsu · Pull Request #1397 · pythonnet/pythonnet · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 23 additions & 3 deletions src/runtime/pythonengine.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;

namespace Python.Runtime
{
Expand Down Expand Up @@ -51,6 +52,9 @@ public static bool IsInitialized
get { return initialized; }
}

/// <summary>Set to <c>true</c> to enable GIL debugging assistance.</summary>
public static bool DebugGIL { get; set; } = false;

internal static DelegateManager DelegateManager
{
get
Expand Down Expand Up @@ -668,7 +672,7 @@ public static GILState GIL()
PythonEngine.Initialize();
}

return new GILState();
return PythonEngine.DebugGIL ? new DebugGILState() : new GILState();
}

public static PyScope CreateScope()
Expand All @@ -693,7 +697,7 @@ internal GILState()
state = PythonEngine.AcquireLock();
}

public void Dispose()
public virtual void Dispose()
{
if (this.isDisposed) return;

Expand All @@ -704,7 +708,23 @@ public void Dispose()

~GILState()
{
Dispose();
throw new InvalidOperationException("GIL must always be released, and it must be released from the same thread that acquired it.");
}
}

public class DebugGILState : GILState
{
readonly Thread owner;
internal DebugGILState() : base()
{
this.owner = Thread.CurrentThread;
}
public override void Dispose()
{
if (this.owner != Thread.CurrentThread)
throw new InvalidOperationException("GIL must always be released from the same thread, that acquired it");

base.Dispose();
}
}

Expand Down

Back | FazBrowse Home | New Git URL