Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use get_ident and LLP64 and LP64 in DllImport names and ulong in publ…
…ic methods and assert nativeThreadID value
  • Loading branch information
gpetrou committed Jan 21, 2021
commit f077caf2d201cba1c0290b8745229fa584599ef5
7 changes: 0 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
### Added

- Ability to instantiate new .NET arrays using `Array[T](dim1, dim2, ...)` syntax
<<<<<<< HEAD
<<<<<<< HEAD
- Python operator method will call C# operator method for supported binary and unary operators ([#1324][p1324]).
=======
- Add Interrupt method in PythonEngine
>>>>>>> Add Interrupt method in PythonEngine
=======
- Add GetNativeThreadID and Interrupt methods in PythonEngine
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetNativeThreadID should be gone now

>>>>>>> Add GetNativeThreadID method in PythonEngine and different PyThreadState_SetAsyncExc calls for OS and Python version

### Changed
- Drop support for Python 2, 3.4, and 3.5
Expand Down
10 changes: 6 additions & 4 deletions src/embed_tests/TestInterrupt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public void Dispose()
public void InterruptTest()
{
int runSimpleStringReturnValue = int.MinValue;
ulong nativeThreadId = 0;
ulong nativeThreadID = ulong.MinValue;
Task.Factory.StartNew(() =>
{
using (Py.GIL())
{
nativeThreadId = PythonEngine.GetNativeThreadID();
nativeThreadID = PythonEngine.GetNativeThreadID();
runSimpleStringReturnValue = PythonEngine.RunSimpleString(@"
import time

Expand All @@ -47,13 +47,15 @@ import time

Thread.Sleep(200);

Assert.AreNotEqual(ulong.MinValue, nativeThreadID);

using (Py.GIL())
{
int interruptReturnValue = PythonEngine.Interrupt(nativeThreadId);
int interruptReturnValue = PythonEngine.Interrupt(nativeThreadID);
Assert.AreEqual(1, interruptReturnValue);
}

Thread.Sleep(500);
Thread.Sleep(300);

Assert.AreEqual(-1, runSimpleStringReturnValue);
}
Expand Down
47 changes: 4 additions & 43 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ namespace Python.Runtime
/// </summary>
public class PythonEngine : IDisposable
{
[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
private static extern uint GetCurrentThreadId();

[DllImport("libc", EntryPoint = "pthread_self")]
private static extern IntPtr pthread_selfLinux();

[DllImport("pthread", EntryPoint = "pthread_self", CallingConvention = CallingConvention.Cdecl)]
private static extern ulong pthread_selfOSX();

public static ShutdownMode ShutdownMode
{
get => Runtime.ShutdownMode;
Expand Down Expand Up @@ -582,28 +573,8 @@ public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = nu
/// <returns>The native thread ID.</returns>
public static ulong GetNativeThreadID()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we keep this, it should be renamed to GetPythonThreadId and doesn't this require the GIL in general?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain why we need GIL here and not in the Interrupt method? GIL is used for both in the tests already.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you are right, all of the other functions in here require the GIL to be taken explicitly as well. I'm not terribly happy about this, but this should be fine then. The dynamic here should not be required, just use InvokeMethod:

var threading = ...;
return threading.InvokeMethod("get_ident");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And renaming would still be nice, sorry for the back and forth, I misread the docs.

{
if (Runtime.PyVersion >= new Version(3, 8))
{
dynamic threading = Py.Import("threading");
return threading.get_native_id();
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return GetCurrentThreadId();
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return (ulong)pthread_selfLinux();
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return pthread_selfOSX();
}

return 0;
dynamic threading = Py.Import("threading");
return threading.get_ident();
}

/// <summary>
Expand All @@ -613,22 +584,12 @@ public static ulong GetNativeThreadID()
/// <returns>The number of thread states modified; this is normally one, but will be zero if the thread id isn’t found.</returns>
public static int Interrupt(ulong nativeThreadID)
{
if (Runtime.PyVersion >= new Version(3, 7))
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Runtime.PyThreadState_SetAsyncExc37Windows(nativeThreadID, Exceptions.KeyboardInterrupt);
}

return Runtime.PyThreadState_SetAsyncExc37NonWindows((UIntPtr)nativeThreadID, Exceptions.KeyboardInterrupt);
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Runtime.PyThreadState_SetAsyncExc36Windows((long)nativeThreadID, Exceptions.KeyboardInterrupt);
return Runtime.PyThreadState_SetAsyncExcLLP64((uint)nativeThreadID, Exceptions.KeyboardInterrupt);
}

return Runtime.PyThreadState_SetAsyncExc36NonWindows((IntPtr)nativeThreadID, Exceptions.KeyboardInterrupt);
return Runtime.PyThreadState_SetAsyncExcLP64(nativeThreadID, Exceptions.KeyboardInterrupt);
}

/// <summary>
Expand Down
10 changes: 2 additions & 8 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2144,16 +2144,10 @@ internal static void Py_CLEAR(ref IntPtr ob)
internal static extern int Py_AddPendingCall(IntPtr func, IntPtr arg);

[DllImport(_PythonDll, EntryPoint = "PyThreadState_SetAsyncExc", CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyThreadState_SetAsyncExc37Windows(ulong id, IntPtr exc);
internal static extern int PyThreadState_SetAsyncExcLLP64(uint id, IntPtr exc);

[DllImport(_PythonDll, EntryPoint = "PyThreadState_SetAsyncExc", CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyThreadState_SetAsyncExc36Windows(long id, IntPtr exc);

[DllImport(_PythonDll, EntryPoint = "PyThreadState_SetAsyncExc", CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyThreadState_SetAsyncExc37NonWindows(UIntPtr id, IntPtr exc);

[DllImport(_PythonDll, EntryPoint = "PyThreadState_SetAsyncExc", CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyThreadState_SetAsyncExc36NonWindows(IntPtr id, IntPtr exc);
internal static extern int PyThreadState_SetAsyncExcLP64(ulong id, IntPtr exc);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int Py_MakePendingCalls();
Expand Down