Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions 26 src/runtime/pythonengine.cs
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.