Skip to content

Commit

Permalink
Make currentThreadId multiplatform
Browse files Browse the repository at this point in the history
  • Loading branch information
Reflejo committed Feb 21, 2017
1 parent 8641558 commit baec009
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion source/common/common/thread.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#include "assert.h"
#include "thread.h"

#ifdef linux
#include <sys/syscall.h>
#elif defined(__FreeBSD__)
#include <pthread_np.h>
#elif defined(__APPLE__)
#include <mach/mach.h>
#endif

namespace Thread {

Expand All @@ -14,7 +20,17 @@ Thread::Thread(std::function<void()> thread_routine) : thread_routine_(thread_ro
UNREFERENCED_PARAMETER(rc);
}

int32_t Thread::currentThreadId() { return syscall(SYS_gettid); }
int32_t Thread::currentThreadId() {
#ifdef linux
return syscall(SYS_gettid);
#elif defined(__APPLE__)
int ret = mach_thread_self();
mach_port_deallocate(mach_task_self(), ret);
return ret;
#elif defined(__FreeBSD__)
return pthread_getthreadid_np();
#endif
}

void Thread::join() {
int rc = pthread_join(thread_id_, nullptr);
Expand Down

0 comments on commit baec009

Please sign in to comment.