Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions src/core/thread.d
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,10 @@ class Thread
*/
~this() nothrow @nogc
{
if ( m_addr == m_addr.init )
bool no_context = m_addr == m_addr.init;
bool not_registered = !next && !prev && (sm_tbeg !is this);

if (no_context || not_registered)
{
return;
}
Expand Down Expand Up @@ -1856,8 +1859,9 @@ private:
do
{
// Thread was already removed earlier, might happen b/c of thread_detachInstance
if (!t.next && !t.prev)
if (!t.next && !t.prev && (sm_tbeg !is t))
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems that this was used previously to see if the thread was detached. You can either replace this
with if (t.m_detached == false) or use this in the destructor directly.


slock.lock_nothrow();
{
// NOTE: When a thread is removed from the global thread list its
Expand Down
38 changes: 33 additions & 5 deletions test/thread/src/external_threads.d
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
import core.sys.posix.pthread;
import core.memory;
import core.thread;

extern (C) void rt_moduleTlsCtor();
extern (C) void rt_moduleTlsDtor();

extern(C)
void* entry_point(void*)
void* entry_point1(void*)
{
// try collecting - GC must ignore this call because this thread
// is not registered in runtime
GC.collect();
return null;
}

extern(C)
void* entry_point2(void*)
{
// This thread gets registered in druntime, does some work and gets
// unregistered to be cleaned up manually
thread_attachThis();
rt_moduleTlsCtor();

auto x = new int[10];

rt_moduleTlsDtor();
thread_detachThis();
return null;
}

void main()
{
// allocate some garbage
auto x = new int[1000];

pthread_t thread;
auto status = pthread_create(&thread, null, &entry_point, null);
assert(status == 0);
pthread_join(thread, null);
{
pthread_t thread;
auto status = pthread_create(&thread, null, &entry_point1, null);
assert(status == 0);
pthread_join(thread, null);
}

{
pthread_t thread;
auto status = pthread_create(&thread, null, &entry_point2, null);
assert(status == 0);
pthread_join(thread, null);
}
}