-
-
Notifications
You must be signed in to change notification settings - Fork 707
osthread: suspend() and resume() code deduplication #23361
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
Open
denizzzka
wants to merge
6
commits into
dlang:master
Choose a base branch
from
denizzzka:osthread_code_dedup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+97
−76
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7b14dd8
osthread: gettid() alias added
denizzzka d70d1de
suspendThreadImpl added, implemented for Darwin, Solaris, Posix and W…
denizzzka 1be2cd1
WASI: gettid and suspendThreadImpl stubs added
denizzzka 4da9cc7
osthread: suspend() is unified for all OSes, loadStackAndRegInfo impl…
denizzzka eb3b45b
osthread: resumeThreadImpl implemented for all platforms
denizzzka ad9e418
osthread: resume() code deduplication
denizzzka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1129,6 +1129,33 @@ private extern(D) void* getStackBottom() nothrow @nogc | |
| static assert(false, "Platform not supported."); | ||
| } | ||
|
|
||
|
|
||
| // Returns true on success | ||
| // TODO: move to posix_impl module | ||
| version (Posix) | ||
| package bool suspendThreadImpl(Thread t) @nogc nothrow | ||
| { | ||
| version (Darwin) | ||
| return thread_suspend(t.m_tmach) == KERN_SUCCESS; | ||
| else version (Solaris) | ||
| return thr_suspend(t.m_addr) == 0; | ||
| else | ||
| return pthread_kill(t.m_addr, suspendSignalNumber) == 0; | ||
| } | ||
|
|
||
| // Returns true on success | ||
| // TODO: move to posix_impl module | ||
| version (Posix) | ||
| package bool resumeThreadImpl(Thread t) @nogc nothrow | ||
| { | ||
| version (Darwin) | ||
| return thread_resume(t.m_tmach) == KERN_SUCCESS; | ||
| else version (Solaris) | ||
| return thr_continue(t.m_addr) == 0; | ||
| else | ||
| return pthread_kill(t.m_addr, resumeSignalNumber) == 0; | ||
| } | ||
|
|
||
| /** | ||
| * Suspend the specified thread and load stack and register information for | ||
| * use by thread_scanAll. If the supplied thread is the calling thread, | ||
|
|
@@ -1153,18 +1180,31 @@ private extern (D) bool suspend( Thread t ) nothrow @nogc | |
| return false; | ||
| } | ||
|
|
||
| version (Windows) | ||
| const sameThread = t.m_addr == gettid(); | ||
|
|
||
| if (!sameThread) | ||
| { | ||
| if ( t.m_addr != GetCurrentThreadId() && SuspendThread( t.m_hndl ) == 0xFFFFFFFF ) | ||
| if (!suspendThreadImpl(t)) | ||
| { | ||
| if ( !t.isRunning ) | ||
| if (t.isRunning) | ||
|
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. I inverted condition here to match the functions description in the DDoc comment. |
||
| onThreadError( "Unable to suspend thread" ); | ||
| else | ||
| { | ||
| Thread.remove( t ); | ||
| return false; | ||
| } | ||
| onThreadError( "Unable to suspend thread" ); | ||
| } | ||
| } | ||
|
|
||
| loadStackAndRegInfo(t, sameThread); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private void loadStackAndRegInfo(Thread t, const bool sameThread) nothrow @nogc | ||
| { | ||
| version (Windows) | ||
| { | ||
| CONTEXT context = void; | ||
| context.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL; | ||
|
|
||
|
|
@@ -1214,16 +1254,6 @@ private extern (D) bool suspend( Thread t ) nothrow @nogc | |
| } | ||
| else version (Darwin) | ||
| { | ||
| if ( t.m_addr != pthread_self() && thread_suspend( t.m_tmach ) != KERN_SUCCESS ) | ||
| { | ||
| if ( !t.isRunning ) | ||
| { | ||
| Thread.remove( t ); | ||
| return false; | ||
| } | ||
| onThreadError( "Unable to suspend thread" ); | ||
| } | ||
|
|
||
| version (X86) | ||
| { | ||
| x86_thread_state32_t state = void; | ||
|
|
@@ -1339,18 +1369,8 @@ private extern (D) bool suspend( Thread t ) nothrow @nogc | |
| } | ||
| else version (Solaris) | ||
| { | ||
| if (t.m_addr != pthread_self()) | ||
| if (!sameThread) | ||
| { | ||
| if (thr_suspend(t.m_addr) != 0) | ||
| { | ||
| if (!t.isRunning) | ||
| { | ||
| Thread.remove(t); | ||
| return false; | ||
| } | ||
| onThreadError("Unable to suspend thread"); | ||
| } | ||
|
|
||
| static int getLwpStatus(ulong lwpid, out lwpstatus_t status) | ||
| { | ||
| import core.sys.posix.fcntl : open, O_RDONLY; | ||
|
|
@@ -1476,19 +1496,7 @@ private extern (D) bool suspend( Thread t ) nothrow @nogc | |
| } | ||
| else version (Posix) | ||
| { | ||
| if ( t.m_addr != pthread_self() ) | ||
| { | ||
| if ( pthread_kill( t.m_addr, suspendSignalNumber ) != 0 ) | ||
| { | ||
| if ( !t.isRunning ) | ||
| { | ||
| Thread.remove( t ); | ||
| return false; | ||
| } | ||
| onThreadError( "Unable to suspend thread" ); | ||
| } | ||
| } | ||
| else if ( !t.m_lock ) | ||
| if (sameThread && !t.m_lock) | ||
| { | ||
| t.m_curr.tstack = getStackTop(); | ||
| } | ||
|
|
@@ -1499,7 +1507,6 @@ private extern (D) bool suspend( Thread t ) nothrow @nogc | |
| } | ||
| else | ||
| static assert(0, "unsupported os"); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1615,70 +1622,48 @@ extern (C) void thread_suspendAll() nothrow | |
| private extern (D) void resume(ThreadBase _t) nothrow @nogc | ||
| { | ||
| Thread t = _t.toThread; | ||
| const sameThread = t.m_addr == gettid(); | ||
|
|
||
| version (Windows) | ||
| if (!sameThread) | ||
| { | ||
| if ( t.m_addr != GetCurrentThreadId() && ResumeThread( t.m_hndl ) == 0xFFFFFFFF ) | ||
| if (!resumeThreadImpl(t)) | ||
| { | ||
| if ( !t.isRunning ) | ||
| if (t.isRunning) | ||
| onThreadError( "Unable to resume thread" ); | ||
| else | ||
| { | ||
| Thread.remove( t ); | ||
| return; | ||
| } | ||
| onThreadError( "Unable to resume thread" ); | ||
| } | ||
| } | ||
|
|
||
| storeStackAndRegInfo(t, sameThread); | ||
| } | ||
|
|
||
| private void storeStackAndRegInfo(Thread t, const bool sameThread) nothrow @nogc | ||
| { | ||
| version (Windows) | ||
| { | ||
| if ( !t.m_lock ) | ||
| t.m_curr.tstack = t.m_curr.bstack; | ||
| t.m_reg[0 .. $] = 0; | ||
| } | ||
| else version (Darwin) | ||
| { | ||
| if ( t.m_addr != pthread_self() && thread_resume( t.m_tmach ) != KERN_SUCCESS ) | ||
| { | ||
| if ( !t.isRunning ) | ||
| { | ||
| Thread.remove( t ); | ||
| return; | ||
| } | ||
| onThreadError( "Unable to resume thread" ); | ||
| } | ||
|
|
||
| if ( !t.m_lock ) | ||
| t.m_curr.tstack = t.m_curr.bstack; | ||
| t.m_reg[0 .. $] = 0; | ||
| } | ||
| else version (Solaris) | ||
| { | ||
| if (t.m_addr != pthread_self() && thr_continue(t.m_addr) != 0) | ||
| { | ||
| if (!t.isRunning) | ||
| { | ||
| Thread.remove(t); | ||
| return; | ||
| } | ||
| onThreadError("Unable to resume thread"); | ||
| } | ||
|
|
||
| if (!t.m_lock) | ||
| t.m_curr.tstack = t.m_curr.bstack; | ||
| t.m_reg[0 .. $] = 0; | ||
| } | ||
| else version (Posix) | ||
| { | ||
| if ( t.m_addr != pthread_self() ) | ||
| { | ||
| if ( pthread_kill( t.m_addr, resumeSignalNumber ) != 0 ) | ||
| { | ||
| if ( !t.isRunning ) | ||
| { | ||
| Thread.remove( t ); | ||
| return; | ||
| } | ||
| onThreadError( "Unable to resume thread" ); | ||
| } | ||
| } | ||
| else if ( !t.m_lock ) | ||
| if (sameThread && !t.m_lock) | ||
| { | ||
| t.m_curr.tstack = t.m_curr.bstack; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
denizzzka marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
At the moment it is difficult to do this: it clings to other symbols that have not yet been transferred to posix_impl