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

Commit

Permalink
Merge pull request #460 from dawgfoto/useTLS
Browse files Browse the repository at this point in the history
make Thread.sm_this a plain TLS value
  • Loading branch information
alexrp committed Mar 21, 2013
2 parents 135fc32 + d3bb881 commit 1f1b0d3
Showing 1 changed file with 9 additions and 47 deletions.
56 changes: 9 additions & 47 deletions src/core/thread.d
Expand Up @@ -998,30 +998,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( Windows )
{
auto t = cast(Thread) TlsGetValue( sm_this );

// NOTE: If this thread was attached via thread_attachByAddr then
// this TLS lookup won't initially be set, so when the TLS
// lookup fails, try an exhaustive search.
if( t is null )
{
t = thread_findByAddr( GetCurrentThreadId() );
setThis( t );
}
return t;
}
else version( Posix )
{
auto t = cast(Thread) pthread_getspecific( sm_this );

// NOTE: See the comment near thread_findByAddr() for why the
// secondary thread_findByAddr lookup can't be done on
// Posix. However, because thread_attachByAddr() is for
// Windows only, the secondary lookup is pointless anyway.
return t;
}
return sm_this;
}


Expand Down Expand Up @@ -1177,7 +1154,7 @@ private:
//
// Local storage
//
__gshared TLSKey sm_this;
static Thread sm_this;


//
Expand Down Expand Up @@ -1226,14 +1203,7 @@ private:
//
static void setThis( Thread t )
{
version( Windows )
{
TlsSetValue( sm_this, cast(void*) t );
}
else version( Posix )
{
pthread_setspecific( sm_this, cast(void*) t );
}
sm_this = t;
}


Expand Down Expand Up @@ -1658,17 +1628,8 @@ extern (C) void thread_init()
// exist to be scanned at this point, it is sufficient for these
// functions to detect the condition and return immediately.

version( Windows )
{
Thread.sm_this = TlsAlloc();
assert( Thread.sm_this != TLS_OUT_OF_INDEXES );
}
else version( OSX )
version( OSX )
{
int status;

status = pthread_key_create( &Thread.sm_this, null );
assert( status == 0 );
}
else version( Posix )
{
Expand Down Expand Up @@ -1712,9 +1673,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 Down Expand Up @@ -1823,7 +1781,11 @@ version( Windows )
else
{
thisThread.m_hndl = OpenThreadHandle( addr );
impersonate_thread(addr, { thisThread.m_tlsgcdata = rt.tlsgc.init(); });
impersonate_thread(addr,
{
thisThread.m_tlsgcdata = rt.tlsgc.init();
Thread.setThis( thisThread );
});
}

Thread.add( thisThread );
Expand Down

0 comments on commit 1f1b0d3

Please sign in to comment.