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

Commit

Permalink
core.thread: Add Thread.id property, ThreadID type
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberShadow committed Sep 17, 2015
1 parent 9575940 commit 615193b
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions src/core/thread.d
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,25 @@ class Thread
///////////////////////////////////////////////////////////////////////////


/**
* Gets the OS identifier for this thread.
*
* Returns:
* If the thread hasn't been started yet, returns $(LREF ThreadID)$(D.init).
* Otherwise, returns the result of $(D GetCurrentThreadId) on Windows,
* and $(D pthread_self) on POSIX.
*
* The value is unique for the current process.
*/
final @property ThreadID id()
{
synchronized( this )
{
return m_addr;
}
}


/**
* Gets the user-readable label for this thread.
*
Expand Down Expand Up @@ -1344,12 +1363,10 @@ private:
version( Windows )
{
alias uint TLSKey;
alias uint ThreadAddr;
}
else version( Posix )
{
alias pthread_key_t TLSKey;
alias pthread_t ThreadAddr;
}


Expand Down Expand Up @@ -1397,7 +1414,7 @@ private:
{
mach_port_t m_tmach;
}
ThreadAddr m_addr;
ThreadID m_addr;
Call m_call;
string m_name;
union
Expand Down Expand Up @@ -2067,14 +2084,14 @@ version( Windows )
// that only does the TLS lookup without the fancy fallback stuff.

/// ditto
extern (C) Thread thread_attachByAddr( Thread.ThreadAddr addr )
extern (C) Thread thread_attachByAddr( ThreadID addr )
{
return thread_attachByAddrB( addr, getThreadStackBottom( addr ) );
}


/// ditto
extern (C) Thread thread_attachByAddrB( Thread.ThreadAddr addr, void* bstack )
extern (C) Thread thread_attachByAddrB( ThreadID addr, void* bstack )
{
GC.disable(); scope(exit) GC.enable();

Expand Down Expand Up @@ -2145,7 +2162,7 @@ extern (C) void thread_detachThis() nothrow
*
* $(D extern(C) void rt_moduleTlsDtor();)
*/
extern (C) void thread_detachByAddr( Thread.ThreadAddr addr )
extern (C) void thread_detachByAddr( ThreadID addr )
{
if( auto t = thread_findByAddr( addr ) )
Thread.remove( t );
Expand Down Expand Up @@ -2186,7 +2203,7 @@ unittest
* Returns:
* The thread object associated with the thread identifier, null if not found.
*/
static Thread thread_findByAddr( Thread.ThreadAddr addr )
static Thread thread_findByAddr( ThreadID addr )
{
Thread.slock.lock_nothrow();
scope(exit) Thread.slock.unlock_nothrow();
Expand Down Expand Up @@ -5229,3 +5246,13 @@ unittest
auto thr = new Thread(function{}, 4096 + 1).start();
thr.join();
}

/**
* Represents the ID of a thread, as returned by $(D Thread.)$(LREF id).
* The exact type varies from platform to platform.
*/
version (Windows)
alias ThreadID = uint;
else
version (Posix)
alias ThreadID = pthread_t;

0 comments on commit 615193b

Please sign in to comment.