Skip to content

Commit 2779e03

Browse files
committed
Initial support for LongRunningCSharpRegion.cs
1 parent dd0d03d commit 2779e03

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
using System;
2-
using Python.Runtime;
1+
using System.Runtime.CompilerServices;
32

43
namespace PythonNETExtensions.Core
54
{
6-
public readonly struct LongRunningCSharpRegion: IDisposable
5+
public readonly ref struct LongRunningCSharpRegion
76
{
8-
private readonly nint Handle;
7+
private readonly ref PythonHandle Handle;
98

9+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1010
public LongRunningCSharpRegion()
1111
{
12-
Handle = PythonEngine.BeginAllowThreads();
12+
Handle.Dispose();
1313
}
1414

15+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1516
public void Dispose()
1617
{
17-
PythonEngine.EndAllowThreads(Handle);
18+
Handle = new PythonHandle();
1819
}
1920
}
2021
}

PythonNETExtensions/Core/PythonHandle.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ static PythonHandle()
4242
.GetValue(null)!;
4343
}
4444

45+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4546
public PythonHandle()
4647
{
4748
GILState = PyGILState_Ensure();
4849
}
4950

51+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5052
public void Dispose()
5153
{
5254
PyGILState_Release(GILState);

0 commit comments

Comments
 (0)