Core: Thread threadid support

git-svn-id: svn://ultimatepp.org/upp/trunk@2993 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2011-01-12 19:53:24 +00:00
parent d1a6d4f8b0
commit 4495aebbdd
2 changed files with 53 additions and 44 deletions

View file

@ -23,9 +23,11 @@ Thread::Thread()
sMutexLock();
#ifdef PLATFORM_WIN32
handle = 0;
thread_id = 0;
#endif
#ifdef PLATFORM_POSIX
handle = 0;
thread_id = 0;
#endif
}
@ -35,11 +37,13 @@ void Thread::Detach()
if(handle) {
CloseHandle(handle);
handle = 0;
thread_id = 0;
}
#elif defined(PLATFORM_POSIX)
if(handle) {
CHECK(!pthread_detach(handle));
handle = 0;
thread_id = 0;
}
#endif
}
@ -85,6 +89,42 @@ Mutex vm; //a common access synchronizer
//otherwise no child threads could run. they are created by main.
//now each thread, having any Thread instace can start a first Run()
bool Thread::Run(Callback _cb)
{
AtomicInc(sThreadCount);
if(!threadr)
#ifndef CPU_BLACKFIN
threadr = sMain = true;
#else
{
threadr = true;
//the sMain replacement
#ifdef PLATFORM_POSIX
pthread_t thid = pthread_self();
vm.Enter();
if(threadsv.Find(thid) < 0){
//thread not yet present, mark present
threadsv.Add(thid);
}
else
RLOG("BUG: Multiple Add in Mt.cpp");
vm.Leave();
#endif
}
#endif
Detach();
Callback *cb = new Callback(_cb);
#ifdef PLATFORM_WIN32
handle = (HANDLE)_beginthreadex(0, 0, sThreadRoutine, cb, 0, ((unsigned int *)(&thread_id)));
#endif
#ifdef PLATFORM_POSIX
if(pthread_create(&handle, 0, sThreadRoutine, cb))
handle = 0;
#endif
return handle;
}
Thread::~Thread()
{
Detach();
@ -128,43 +168,6 @@ bool Thread::IsMain() //the calling thread is the Main Thread or the only one in
#endif
}
bool Thread::Run(Callback _cb)
{
AtomicInc(sThreadCount);
if(!threadr)
#ifndef CPU_BLACKFIN
threadr = sMain = true;
#else
{
threadr = true;
//the sMain replacement
#ifdef PLATFORM_POSIX
pthread_t thid = pthread_self();
vm.Enter();
if(threadsv.Find(thid) < 0)
{
//thread not yet present, mark present
threadsv.Add(thid);
}
else
RLOG("BUG: Multiple Add in Mt.cpp");
vm.Leave();
#endif
}
#endif
Detach();
Callback *cb = new Callback(_cb);
#ifdef PLATFORM_WIN32
unsigned thread_id;
handle = (HANDLE)_beginthreadex(0, 0, sThreadRoutine, cb, 0, &thread_id);
#endif
#ifdef PLATFORM_POSIX
if(pthread_create(&handle, 0, sThreadRoutine, cb))
handle = 0;
#endif
return handle;
}
int Thread::GetCount()
{
return ReadWithBarrier(sThreadCount);

View file

@ -44,11 +44,11 @@ class Callback;
class Thread : NoCopy {
#ifdef PLATFORM_WIN32
HANDLE handle;
DWORD thread_id;
#endif
#ifdef PLATFORM_POSIX
pthread_t handle;
#endif
public:
bool Run(Callback cb);
@ -56,17 +56,22 @@ public:
int Wait();
bool IsOpen() const { return handle; }
#ifdef PLATFORM_WIN32
typedef HANDLE Handle;
typedef DWORD Id;
Id GetId() const { return thread_id; }
#endif
#ifdef PLATFORM_POSIX
typedef pthread_t Handle;
typedef pthread_t Id;
Id GetId() const { return handle; }
#endif
Handle GetHandle() const { return handle; }
Handle GetHandle() const { return handle; }
void Priority(int percent); // 0 = lowest, 100 = normal
static void Start(Callback cb);
@ -78,12 +83,13 @@ public:
static int GetCount();
static void ShutdownThreads();
static bool IsShutdownThreads();
#ifdef PLATFORM_WIN32
static Handle GetCurrentHandle() { return GetCurrentThread(); }
static Handle GetCurrentHandle() { return GetCurrentThread(); }
static inline Id GetCurrentId() { return ::GetCurrentThreadId(); };
#endif
#ifdef PLATFORM_POSIX
static Handle GetCurrentHandle() { return pthread_self(); }
static Handle GetCurrentHandle() { return pthread_self(); }
static inline Id GetCurrentId() { return pthread_self(); };
#endif
Thread();