Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
fix Issue 15270 - use TLS to store Thread.getThis
Browse files Browse the repository at this point in the history
- as signals are no longer send during thread startup we can use
  a TLS variable to store Thread.getThis()
- make sure to set the variable (and probably trigger lazy TLS
  allocation) before adding the Thread to the global list
  • Loading branch information
MartinNowak committed Oct 31, 2015
1 parent 7ec6520 commit e226ff2
Showing 1 changed file with 5 additions and 54 deletions.
59 changes: 5 additions & 54 deletions src/core/thread.d
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ else version( Posix )
obj.m_tlsgcdata = rt_tlsgc_init();

atomicStore!(MemoryOrder.raw)(obj.m_isRunning, true);
Thread.setThis(obj);
Thread.add(obj);
Thread.setThis(obj); // allocates lazy TLS (see Issue 11981)
Thread.add(obj); // can only receive signals from here on
scope (exit)
{
Thread.remove(obj);
Expand Down Expand Up @@ -1130,19 +1130,7 @@ 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.
version( OSX )
{
return sm_this;
}
else version( Posix )
{
auto t = cast(Thread) pthread_getspecific( sm_this );
return t;
}
else
{
return sm_this;
}
return sm_this;
}


Expand Down Expand Up @@ -1401,22 +1389,7 @@ private:
//
// Local storage
//
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;
}
static Thread sm_this;


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


Expand Down Expand Up @@ -1998,9 +1960,6 @@ 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 );
}
Thread.sm_main = thread_attachThis();
}
Expand All @@ -2020,14 +1979,6 @@ extern (C) void thread_term()
Thread.pAboutToStart = null;
}
Thread.termLocks();

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


Expand Down

0 comments on commit e226ff2

Please sign in to comment.