Thanks to visit codestin.com
Credit goes to github.com

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
Add GetNativeThreadID method in PythonEngine and different PyThreadSt…
…ate_SetAsyncExc calls for OS and Python version
  • Loading branch information
gpetrou committed Jan 21, 2021
commit d07cd05e9c738f4f62cf91d4e1fc83283c704730
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

- 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
26 changes: 2 additions & 24 deletions src/embed_tests/TestInterrupt.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -14,15 +13,6 @@ public class TestInterrupt
{
private IntPtr _threadState;

[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();

[OneTimeSetUp]
public void SetUp()
{
Expand All @@ -44,21 +34,9 @@ public void InterruptTest()
ulong nativeThreadId = 0;
Task.Factory.StartNew(() =>
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
nativeThreadId = GetCurrentThreadId();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
nativeThreadId = (ulong)pthread_selfLinux();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
nativeThreadId = pthread_selfOSX();
}

using (Py.GIL())
{
nativeThreadId = PythonEngine.GetNativeThreadID();
runSimpleStringReturnValue = PythonEngine.RunSimpleString(@"
import time

Expand All @@ -75,7 +53,7 @@ import time
Assert.AreEqual(1, interruptReturnValue);
}

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

Assert.AreEqual(-1, runSimpleStringReturnValue);
}
Expand Down
60 changes: 57 additions & 3 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ 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 @@ -567,14 +576,59 @@ public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = nu
}
}

/// <summary>
/// Gets the native thread ID.
/// </summary>
/// <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();
Copy link
Member

Choose a reason for hiding this comment

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

Should get the GIL, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't the user supposed to do that when calling the method, as I did in the test?

Copy link
Contributor Author

@gpetrou gpetrou Dec 27, 2020

Choose a reason for hiding this comment

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

This does not seem to be working on Ubuntu. Are you sure that this should work? Looking in CPython tests I see that get_ident is used. I don't see any benefit in adding the above lines. Should I just remove them?

}

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;
}

/// <summary>
/// Interrupts the execution of a thread.
/// </summary>
/// <param name="nativeThreadId">The native thread id.</param>
/// <param name="nativeThreadID">The native thread ID.</param>
/// <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)
public static int Interrupt(ulong nativeThreadID)
{
return Runtime.PyThreadState_SetAsyncExc(nativeThreadId, Exceptions.KeyboardInterrupt);
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_SetAsyncExc36NonWindows((IntPtr)nativeThreadID, Exceptions.KeyboardInterrupt);
}

/// <summary>
Expand Down
13 changes: 11 additions & 2 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2143,8 +2143,17 @@ internal static void Py_CLEAR(ref IntPtr ob)
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int Py_AddPendingCall(IntPtr func, IntPtr arg);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyThreadState_SetAsyncExc(ulong id, IntPtr exc);
[DllImport(_PythonDll, EntryPoint = "PyThreadState_SetAsyncExc", CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyThreadState_SetAsyncExc37Windows(ulong 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);

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