-
Notifications
You must be signed in to change notification settings - Fork 773
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
…ate_SetAsyncExc calls for OS and Python version
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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() | ||
|
Member
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
Contributor
Author
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.
Member
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
Member
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(); | ||
|
Member
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. Should get the
Contributor
Author
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. Isn't the user supposed to do that when calling the method, as I did in the test?
Contributor
Author
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. 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; | ||
|
gpetrou marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /// <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) | ||
|
gpetrou marked this conversation as resolved.
Outdated
|
||
| { | ||
| 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> | ||
|
|
||
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