41 #ifndef _btsthread_cpp
42 #define _btsthread_cpp
77 #if defined(_MEZZ_THREAD_POSIX_)
96 #if defined(_MEZZ_THREAD_POSIX_)
102 static ThreadId _pthread_t_to_ID(
const pthread_t &aHandle)
104 static Mutex idMapLock;
105 static std::map<pthread_t, unsigned long int> idMap;
106 static unsigned long int idCount(1);
108 lock_guard<Mutex> guard(idMapLock);
109 if(idMap.find(aHandle) == idMap.end())
110 idMap[aHandle] = idCount ++;
111 return ThreadId(idMap[aHandle]);
113 #endif // _MEZZ_POSIX_
120 struct _thread_start_info {
121 void (*mFunction)(
void *);
127 #if defined(_MEZZ_THREAD_WIN32_)
128 unsigned WINAPI Thread::wrapper_function(
void * aArg)
129 #elif defined(_MEZZ_THREAD_POSIX_)
130 void * Thread::wrapper_function(
void * aArg)
134 _thread_start_info * ti = (_thread_start_info *) aArg;
139 ti->mFunction(ti->mArg);
149 lock_guard<Mutex> guard(ti->mThread->mDataMutex);
150 ti->mThread->mNotAThread =
true;
162 lock_guard<Mutex> guard(mDataMutex);
166 _thread_start_info * ti =
new _thread_start_info;
167 ti->mFunction = aFunction;
175 #if defined(_MEZZ_THREAD_WIN32_)
176 mHandle = (HANDLE) _beginthreadex(0, 0, wrapper_function, (
void *) ti, 0, &mWin32ThreadID);
177 #elif defined(_MEZZ_THREAD_POSIX_)
178 if(pthread_create(&mHandle, NULL, wrapper_function, (
void *) ti) != 0)
193 lock_guard<Mutex> guard(mDataMutex);
197 _thread_start_info * ti =
new _thread_start_info;
198 ti->mFunction = aFunction;
206 #if defined(_MEZZ_THREAD_WIN32_)
207 mHandle = (HANDLE) _beginthreadex(0, 0, wrapper_function, (
void *) ti, 0, &mWin32ThreadID);
208 #elif defined(_MEZZ_THREAD_POSIX_)
209 if(pthread_create(&mHandle, NULL, wrapper_function, (
void *) ti) != 0)
231 #if defined(_MEZZ_THREAD_WIN32_)
232 WaitForSingleObject(mHandle, INFINITE);
233 CloseHandle(mHandle);
234 #elif defined(_MEZZ_THREAD_POSIX_)
235 pthread_join(mHandle, NULL);
243 bool result = !mNotAThread;
253 #if defined(_MEZZ_THREAD_WIN32_)
254 CloseHandle(mHandle);
255 #elif defined(_TTHREAD_POSIX_)
256 pthread_detach(mHandle);
267 #if defined(_MEZZ_THREAD_WIN32_)
268 return ThreadId((
unsigned long int) mWin32ThreadID);
269 #elif defined(_MEZZ_THREAD_POSIX_)
270 return _pthread_t_to_ID(mHandle);
276 #if defined(_MEZZ_THREAD_WIN32_)
279 return (
int) si.dwNumberOfProcessors;
280 #elif defined(_SC_NPROCESSORS_ONLN)
281 return (
int) sysconf(_SC_NPROCESSORS_ONLN);
282 #elif defined(_SC_NPROC_ONLN)
283 return (
int) sysconf(_SC_NPROC_ONLN);
298 #if defined(_MEZZ_THREAD_WIN32_)
299 return ThreadId((
unsigned long int) GetCurrentThreadId());
300 #elif defined(_MEZZ_THREAD_POSIX_)
301 return _pthread_t_to_ID(pthread_self());
307 #if defined(_MEZZ_THREAD_WIN32_)
316 #if defined(_MEZZ_THREAD_WIN32_)
317 Sleep(MicroSeconds/1000);
319 usleep(MicroSeconds);
void Lock()
Lock the mutex.
ThreadId MEZZ_LIB get_id()
Return the thread ID of the calling thread.
ThreadId get_id() const
Return the thread ID of a thread object.
void detach()
Detach from the thread.
bool joinable() const
Check if the thread is joinable.
This file defines a minimalistic cross-platform thread that the scheduler uses to schedule tasks...
void MEZZ_LIB yield()
Yield execution to another thread.
static unsigned hardware_concurrency()
Determine the number of threads which can possibly execute concurrently.
Declares a tool for automatically unlocking a mutex in an exception safe way.
uint32_t UInt32
An 32-bit unsigned integer.
void MEZZ_LIB sleep_for(UInt32 MicroSeconds)
Blocks the calling thread for a period of time.
Thread()
Default constructor.
The bulk of the engine components go in this namspace.
void Unlock()
Unlock the mutex.
void join()
Wait for the thread to finish (join execution flows).