Skip to content

Commit

Permalink
Platform-specific gettid. Closes #3404, #3405.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed May 20, 2015
1 parent a0f2fa5 commit 05613d2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions configure
Expand Up @@ -1220,6 +1220,10 @@ int main() { return tgetnum(""); }
@defines << "HAVE_INOTIFY"
end

if has_function("gettid", ["unistd.d", "sys/types.h"])
@defines << "HAVE_GETTID"
end

# glibc has useless lchmod() so we don't try to use lchmod() on linux
if !@linux and has_function("lchmod", ["sys/stat.h", "unistd.h"])
@have_lchmod = true
Expand Down
8 changes: 2 additions & 6 deletions vm/builtin/thread.cpp
Expand Up @@ -20,7 +20,7 @@
#include "metrics.hpp"
#include "util/logger.hpp"

#include <sys/syscall.h>
#include "missing/gettid.h"

/* HACK: returns a value that should identify a native thread
* for debugging threading issues. The winpthreads library
Expand Down Expand Up @@ -291,11 +291,7 @@ namespace rubinius {

NativeMethod::init_thread(state);

#ifdef __APPLE__
vm->thread->pid(state, Fixnum::from(syscall(SYS_thread_selfid)));
#else
vm->thread->pid(state, Fixnum::from(syscall(SYS_gettid)));
#endif
vm->thread->pid(state, Fixnum::from(gettid()));

// Lock the thread object and unlock it at __run__ in the ruby land.
vm->thread->alive(state, cTrue);
Expand Down
19 changes: 19 additions & 0 deletions vm/missing/gettid.cpp
@@ -0,0 +1,19 @@
#include "vm/missing/gettid.h"

#ifndef HAVE_GETTID
#include <sys/syscall.h>

pid_t gettid(void) {
#if defined(__APPLE__)
return (pid_t)syscall(SYS_thread_selfid);
#elif defined(__FreeBSD__)
long tid;
syscall(SYS_thr_self, &tid);
return (pid_t)(tid);
#elif defined(SYS_gettid)
return (pid_t)syscall(SYS_gettid);
#else
return 0;
#endif
}
#endif
11 changes: 11 additions & 0 deletions vm/missing/gettid.h
@@ -0,0 +1,11 @@
#include "vm/config.h"

#include <unistd.h>

#ifdef HAVE_GETTID
#include <sys/types.h>
#else
extern "C" {
pid_t gettid(void);
}
#endif

0 comments on commit 05613d2

Please sign in to comment.