Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
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
56 changes: 52 additions & 4 deletions src/core/thread.d
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,6 @@ class Thread
// Thread Accessors
///////////////////////////////////////////////////////////////////////////


/**
* Provides a reference to the calling thread.
*
Expand All @@ -1066,7 +1065,19 @@ class Thread
// NOTE: This function may not be called until thread_init has
// completed. See thread_suspendAll for more information
// on why this might occur.
return sm_this;
version( OSX )
{
return sm_this;
}
else version( Posix )
{
auto t = cast(Thread) pthread_getspecific( sm_this );
return t;
}
else
{
return sm_this;
}
}


Expand Down Expand Up @@ -1225,7 +1236,22 @@ private:
//
// Local storage
//
static Thread sm_this;
version( OSX )
{
static Thread sm_this;
}
else version( Posix )
{
// On Posix (excluding OSX), pthread_key_t is explicitly used to
// store and access thread reference. This is needed
// to avoid TLS access in signal handlers (malloc deadlock)
// when using shared libraries, see issue 11981.
__gshared pthread_key_t sm_this;
}
else
{
static Thread sm_this;
}


//
Expand Down Expand Up @@ -1274,7 +1300,18 @@ private:
//
static void setThis( Thread t )
{
sm_this = t;
version( OSX )
{
sm_this = t;
}
else version( Posix )
{
pthread_setspecific( sm_this, cast(void*) t );
}
else
{
sm_this = t;
}
}


Expand Down Expand Up @@ -1754,6 +1791,9 @@ extern (C) void thread_init()

status = sem_init( &suspendCount, 0, 0 );
assert( status == 0 );

status = pthread_key_create( &Thread.sm_this, null );
assert( status == 0 );
Copy link
Member

Choose a reason for hiding this comment

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

I would create the key in a shared static this() and destroy it in a shared static ~this().
You could define them close to getThis/setThis, then a single of those comments would suffice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm. Makes sense.

...Except we can't. create/delete should happen in thread_init() and thread_term().

To clarify: thread_init() is run before even shared static ctors, and main thread is initialized then as well. I.e. it's too late to create the key in shared static ctor.

Copy link
Member

Choose a reason for hiding this comment

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

Your right, I forgot about that.

}
Thread.sm_main = thread_attachThis();
}
Expand All @@ -1766,6 +1806,14 @@ extern (C) void thread_init()
extern (C) void thread_term()
{
Thread.termLocks();

version( OSX )
{
}
else version( Posix )
{
pthread_key_delete( Thread.sm_this );
}
}


Expand Down