Skip to content

Commit

Permalink
Added Thread#pid. Closes #3365.
Browse files Browse the repository at this point in the history
Unfortunately, this works great on Linux and not-so-great on OS X. I was
unable to find any syscall that would provide the same behavior as Linux.

We also may end up needing more #ifdefs.
  • Loading branch information
brixen committed May 15, 2015
1 parent a91c53b commit cda7249
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions kernel/common/thread.rb
Expand Up @@ -12,6 +12,7 @@ def self.exclusive
end

attr_reader :recursive_objects
attr_reader :pid

# Implementation note: ideally, the recursive_objects
# lookup table would be different per method call.
Expand Down
9 changes: 9 additions & 0 deletions vm/builtin/thread.cpp
Expand Up @@ -20,6 +20,8 @@
#include "metrics.hpp"
#include "util/logger.hpp"

#include <sys/syscall.h>

/* HACK: returns a value that should identify a native thread
* for debugging threading issues. The winpthreads library
* defines pthread_t to be a structure not a pointer.
Expand Down Expand Up @@ -73,6 +75,7 @@ namespace rubinius {
thr->joins(state, Array::create(state, 1));
thr->killed(state, cFalse);
thr->priority(state, Fixnum::from(0));
thr->pid(state, Fixnum::from(0));
thr->klass(state, klass);

vm->thread.set(thr);
Expand Down Expand Up @@ -289,6 +292,12 @@ 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

// Lock the thread object and unlock it at __run__ in the ruby land.
vm->thread->alive(state, cTrue);
vm->thread->init_lock_.unlock();
Expand Down
2 changes: 2 additions & 0 deletions vm/builtin/thread.hpp
Expand Up @@ -47,6 +47,7 @@ namespace rubinius {
Array* joins_; // slot
Object* killed_; // slot
Fixnum* priority_; // slot
Fixnum* pid_; // slot

utilities::thread::SpinLock init_lock_;
utilities::thread::Mutex join_lock_;
Expand Down Expand Up @@ -88,6 +89,7 @@ namespace rubinius {
attr_accessor(joins, Array);
attr_accessor(killed, Object);
attr_accessor(priority, Fixnum);
attr_accessor(pid, Fixnum);

VM* vm() const {
return vm_;
Expand Down

0 comments on commit cda7249

Please sign in to comment.