Skip to content

Commit

Permalink
Added {Thread, Fiber}.count.
Browse files Browse the repository at this point in the history
brixen committed Jun 23, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 6531af5 commit eb85896
Showing 10 changed files with 84 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/fiber.rb
Original file line number Diff line number Diff line change
@@ -34,6 +34,11 @@ def self.main
raise PrimitiveFailure, "Fiber.main primitive failed"
end

def self.count
Rubinius.primitive :fiber_s_count
raise PrimitiveFailure, "Fiber.count primitive failed"
end

def status
Rubinius.primitive :fiber_status
raise PrimitiveFailure, "Fiber#status primitive failed"
5 changes: 5 additions & 0 deletions core/thread.rb
Original file line number Diff line number Diff line change
@@ -79,6 +79,11 @@ def self.list
Kernel.raise PrimitiveFailure, "Thread.list primitive failed"
end

def self.count
Rubinius.primitive :thread_count
Kernel.raise PrimitiveFailure, "Thread.count primitive failed"
end

def self.stop
sleep
nil
4 changes: 4 additions & 0 deletions machine/builtin/fiber.cpp
Original file line number Diff line number Diff line change
@@ -412,6 +412,10 @@ namespace rubinius {
return state->vm()->thread()->fiber();
}

Fixnum* Fiber::s_count(STATE) {
return state->shared().vm_fibers_count(state);
}

void Fiber::finalize(STATE, Fiber* fib) {
if(state->shared().config.machine_fiber_log_finalizer.value) {
logger::write("fiber: finalizer: %s, %d",
3 changes: 3 additions & 0 deletions machine/builtin/fiber.hpp
Original file line number Diff line number Diff line change
@@ -93,6 +93,9 @@ namespace rubinius {
// Rubinius.primitive :fiber_s_main
static Fiber* s_main(STATE);

// Rubinius.primitive :fiber_s_count
static Fixnum* s_count(STATE);

bool root_p();

Status status() {
4 changes: 4 additions & 0 deletions machine/builtin/thread.cpp
Original file line number Diff line number Diff line change
@@ -432,6 +432,10 @@ namespace rubinius {
return state->shared().vm_threads(state);
}

Fixnum* Thread::count(STATE) {
return state->shared().vm_threads_count(state);
}

Object* Thread::set_priority(STATE, Fixnum* new_priority) {
priority(state, new_priority);
return new_priority;
3 changes: 3 additions & 0 deletions machine/builtin/thread.hpp
Original file line number Diff line number Diff line change
@@ -131,6 +131,9 @@ namespace rubinius {
// Rubinius.primitive :thread_list
static Array* list(STATE);

// Rubinius.primitive :thread_count
static Fixnum* count(STATE);

public: /* Instance primitives */

void fork(STATE);
43 changes: 43 additions & 0 deletions machine/shared_state.cpp
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
#include "signal.hpp"
#include "builtin/randomizer.hpp"
#include "builtin/array.hpp"
#include "builtin/fixnum.hpp"
#include "builtin/thread.hpp"
#include "builtin/native_method.hpp"
#include "builtin/system.hpp"
@@ -128,6 +129,27 @@ namespace rubinius {
return threads;
}

Fixnum* SharedState::vm_threads_count(STATE) {
std::lock_guard<std::mutex> guard(thread_nexus_->threads_mutex());

native_int count = 0;

for(ThreadList::iterator i = thread_nexus_->threads()->begin();
i != thread_nexus_->threads()->end();
++i)
{
if(VM* vm = (*i)->as_vm()) {
Thread *thread = vm->thread();
if(vm->kind() == memory::ManagedThread::eThread
&&!thread->nil_p() && CBOOL(thread->alive())) {
count++;
}
}
}

return Fixnum::from(count);
}

Array* SharedState::vm_fibers(STATE) {
std::lock_guard<std::mutex> guard(thread_nexus_->threads_mutex());

@@ -149,6 +171,27 @@ namespace rubinius {
return fibers;
}

Fixnum* SharedState::vm_fibers_count(STATE) {
std::lock_guard<std::mutex> guard(thread_nexus_->threads_mutex());

native_int count = 0;

for(ThreadList::iterator i = thread_nexus_->threads()->begin();
i != thread_nexus_->threads()->end();
++i)
{
if(VM* vm = (*i)->as_vm()) {
if(vm->kind() == memory::ManagedThread::eFiber
&& !vm->fiber()->nil_p()
&& vm->fiber()->status() != Fiber::eDead) {
count++;
}
}
}

return Fixnum::from(count);
}

Array* SharedState::vm_thread_fibers(STATE, Thread* thread) {
std::lock_guard<std::mutex> guard(thread_nexus_->threads_mutex());

3 changes: 3 additions & 0 deletions machine/shared_state.hpp
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@ namespace rubinius {
class ManagedThread;
}

class Fixnum;
class SignalThread;
class Memory;
class GlobalCache;
@@ -166,7 +167,9 @@ namespace rubinius {
}

Array* vm_threads(STATE);
Fixnum* vm_threads_count(STATE);
Array* vm_fibers(STATE);
Fixnum* vm_fibers_count(STATE);
Array* vm_thread_fibers(STATE, Thread* thread);

int global_serial() const {
7 changes: 7 additions & 0 deletions spec/ruby/core/fiber/count_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)

describe "Fiber.count" do
it "returns the number of Fibers that would be returned by .list" do
Fiber.count.should == Fiber.list.count
end
end
7 changes: 7 additions & 0 deletions spec/ruby/core/thread/count_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)

describe "Thread.count" do
it "returns the number of Threads that would be returned by .list" do
Thread.count.should == Thread.list.count
end
end

0 comments on commit eb85896

Please sign in to comment.