-
Notifications
You must be signed in to change notification settings - Fork 762
Add GetPythonThreadID and Interrupt methods in PythonEngine #1337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8b848b3
d07cd05
f077caf
8e7b7c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…ic methods and assert nativeThreadID value
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we keep this, it should be renamed to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
@@ -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) | ||
gpetrou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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> | ||
|
There was a problem hiding this comment.
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