Skip to content

Commit cda7249

Browse files
committedMay 15, 2015
Added Thread#pid. Closes #3365.
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.
1 parent a91c53b commit cda7249

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed
 

Diff for: ‎kernel/common/thread.rb

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def self.exclusive
1212
end
1313

1414
attr_reader :recursive_objects
15+
attr_reader :pid
1516

1617
# Implementation note: ideally, the recursive_objects
1718
# lookup table would be different per method call.

Diff for: ‎vm/builtin/thread.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "metrics.hpp"
2121
#include "util/logger.hpp"
2222

23+
#include <sys/syscall.h>
24+
2325
/* HACK: returns a value that should identify a native thread
2426
* for debugging threading issues. The winpthreads library
2527
* defines pthread_t to be a structure not a pointer.
@@ -73,6 +75,7 @@ namespace rubinius {
7375
thr->joins(state, Array::create(state, 1));
7476
thr->killed(state, cFalse);
7577
thr->priority(state, Fixnum::from(0));
78+
thr->pid(state, Fixnum::from(0));
7679
thr->klass(state, klass);
7780

7881
vm->thread.set(thr);
@@ -289,6 +292,12 @@ namespace rubinius {
289292

290293
NativeMethod::init_thread(state);
291294

295+
#ifdef __APPLE__
296+
vm->thread->pid(state, Fixnum::from(syscall(SYS_thread_selfid)));
297+
#else
298+
vm->thread->pid(state, Fixnum::from(syscall(SYS_gettid)));
299+
#endif
300+
292301
// Lock the thread object and unlock it at __run__ in the ruby land.
293302
vm->thread->alive(state, cTrue);
294303
vm->thread->init_lock_.unlock();

Diff for: ‎vm/builtin/thread.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace rubinius {
4747
Array* joins_; // slot
4848
Object* killed_; // slot
4949
Fixnum* priority_; // slot
50+
Fixnum* pid_; // slot
5051

5152
utilities::thread::SpinLock init_lock_;
5253
utilities::thread::Mutex join_lock_;
@@ -88,6 +89,7 @@ namespace rubinius {
8889
attr_accessor(joins, Array);
8990
attr_accessor(killed, Object);
9091
attr_accessor(priority, Fixnum);
92+
attr_accessor(pid, Fixnum);
9193

9294
VM* vm() const {
9395
return vm_;

0 commit comments

Comments
 (0)
Please sign in to comment.