Skip to content

Commit

Permalink
Showing 21 changed files with 129 additions and 106 deletions.
17 changes: 5 additions & 12 deletions vm/builtin/thread.cpp
Original file line number Diff line number Diff line change
@@ -269,17 +269,10 @@ namespace rubinius {
SharedState& shared = vm->shared;
State state_obj(vm), *state = &state_obj;

std::string thread_name;
vm->set_current_thread();

{
std::ostringstream tn;
tn << "rbx.ruby." << vm->thread_id();
VM::set_current(vm, tn.str());
thread_name = tn.str();
}

RUBINIUS_THREAD_START(const_cast<RBX_DTRACE_CHAR_P>(thread_name.c_str()),
vm->thread_id(), 0);
RUBINIUS_THREAD_START(
const_cast<RBX_DTRACE_CHAR_P>(vm->name().c_str()), vm->thread_id(), 0);

if(cDebugThreading) {
utilities::logger::debug("Thread: start thread: id: %d, pthread: %d",
@@ -341,8 +334,8 @@ namespace rubinius {

vm->set_zombie();

RUBINIUS_THREAD_STOP(const_cast<RBX_DTRACE_CHAR_P>(thread_name.c_str()),
vm->thread_id(), 0);
RUBINIUS_THREAD_STOP(
const_cast<RBX_DTRACE_CHAR_P>(vm->name().c_str()), vm->thread_id(), 0);

return 0;
}
7 changes: 3 additions & 4 deletions vm/console.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef RBX_CONSOLE_HPP
#define RBX_CONSOLE_HPP

#include "lock.hpp"
#include "internal_threads.hpp"

#include "gc/root.hpp"
@@ -66,7 +65,7 @@ namespace rubinius {
Class* server_class(STATE);
};

class Listener : public InternalThread, public Lockable {
class Listener : public InternalThread {
Console* console_;

TypedRoot<FSEvent*> fsevent_;
@@ -88,7 +87,7 @@ namespace rubinius {

typedef std::list<char*> RequestList;

class Response : public InternalThread, public Lockable {
class Response : public InternalThread {
Console* console_;

TypedRoot<Channel*> inbox_;
@@ -122,7 +121,7 @@ namespace rubinius {
void write_response(STATE, const char* response, native_int size);
};

class Request : public InternalThread, public Lockable {
class Request : public InternalThread {
Console* console_;
Response* response_;

2 changes: 1 addition & 1 deletion vm/environment.cpp
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ namespace rubinius {

check_io_descriptors();

root_vm = shared->new_vm();
root_vm = shared->new_vm("rbx.ruby.main");
root_vm->metrics().init(metrics::eRubyMetrics);
state = new State(root_vm);

3 changes: 1 addition & 2 deletions vm/gc/finalize.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef RBX_GC_FINALIZE_HPP
#define RBX_GC_FINALIZE_HPP

#include "lock.hpp"
#include "internal_threads.hpp"

#include "gc/finalize.hpp"
@@ -59,7 +58,7 @@ namespace rubinius {
typedef std::list<FinalizeObject> FinalizeObjects;
typedef std::list<FinalizeObjects*> FinalizeObjectsList;

class FinalizerThread : public InternalThread, public Lockable {
class FinalizerThread : public InternalThread {
public:
class iterator {
FinalizerThread* handler_;
3 changes: 1 addition & 2 deletions vm/gc/immix_marker.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef RBX_GC_IMMIX_MARKER_HPP
#define RBX_GC_IMMIX_MARKER_HPP

#include "lock.hpp"
#include "internal_threads.hpp"

#include "gc/root.hpp"
@@ -13,7 +12,7 @@ namespace rubinius {
class ImmixGC;
class GCData;

class ImmixMarker : public InternalThread, public Lockable {
class ImmixMarker : public InternalThread {
ImmixGC* immix_;
GCData* data_;

22 changes: 17 additions & 5 deletions vm/gc/managed.cpp
Original file line number Diff line number Diff line change
@@ -5,16 +5,28 @@
#include "shared_state.hpp"
#include "metrics.hpp"

#include <sstream>

namespace rubinius {
utilities::thread::ThreadData<ManagedThread*> _current_thread;

ManagedThread::ManagedThread(uint32_t id, SharedState& ss, ManagedThread::Kind kind)
: shared_(ss)
, name_(kind == eRuby ? "<ruby>" : "<system>")
ManagedThread::ManagedThread(uint32_t id, SharedState& ss,
ManagedThread::Kind kind, const char* name)
: Lockable(true)
, shared_(ss)
, run_state_(eIndependent)
, kind_(kind)
, os_thread_(pthread_self())
, id_(id)
{
if(name) {
name_ = std::string(name);
} else {
std::ostringstream thread_name;
thread_name << "rbx.ruby." << id_;
name_ = thread_name.str();
}

metrics_.init(metrics::eNone);
}

@@ -28,9 +40,9 @@ namespace rubinius {
return _current_thread.get();
}

void ManagedThread::set_current(ManagedThread* th, std::string name) {
void ManagedThread::set_current_thread(ManagedThread* th) {
utilities::thread::Thread::set_os_name(th->name().c_str());
th->os_thread_ = pthread_self();
th->set_name(name);
_current_thread.set(th);
}
}
21 changes: 8 additions & 13 deletions vm/gc/managed.hpp
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@

#include <algorithm>
#include <vector>
#include <string>

namespace rubinius {
class SharedState;
@@ -24,7 +25,8 @@ namespace rubinius {
friend class WorldState;

enum Kind {
eRuby, eSystem
eRuby,
eSystem
};

enum RunState {
@@ -51,9 +53,12 @@ namespace rubinius {
uint32_t id_;

public:
ManagedThread(uint32_t id, SharedState& ss, Kind kind);
ManagedThread(uint32_t id, SharedState& ss, Kind kind, const char* name);
~ManagedThread();

static void set_current_thread(ManagedThread* vm);
static ManagedThread* current();

Roots& roots() {
return roots_;
}
@@ -99,19 +104,13 @@ namespace rubinius {
}

VM* as_vm() {
if(kind_ == eRuby) return reinterpret_cast<VM*>(this);
return 0;
return reinterpret_cast<VM*>(this);
}

std::string name() const {
return name_;
}

void set_name(std::string name) {
name_ = name;
utilities::thread::Thread::set_os_name(name.c_str());
}

uint32_t thread_id() const {
return id_;
}
@@ -131,10 +130,6 @@ namespace rubinius {
metrics::MetricsData& metrics() {
return metrics_;
}

public:
static ManagedThread* current();
static void set_current(ManagedThread* vm, std::string name);
};
}

4 changes: 3 additions & 1 deletion vm/global_cache.hpp
Original file line number Diff line number Diff line change
@@ -44,7 +44,9 @@ namespace rubinius {
static bool resolve(STATE, Symbol* name, Dispatch& msg, LookupData& lookup);
bool resolve_i(STATE, Symbol* name, Dispatch& msg, LookupData& lookup);

GlobalCache() {
GlobalCache()
: Lockable(true)
{
reset();
}

24 changes: 9 additions & 15 deletions vm/internal_threads.cpp
Original file line number Diff line number Diff line change
@@ -12,8 +12,7 @@ namespace rubinius {
using namespace utilities;

InternalThread::InternalThread(STATE, std::string name, StackSize stack_size)
: vm_(state->shared().new_vm())
, name_(name)
: vm_(state->shared().new_vm(name.c_str()))
, thread_running_(false)
, stack_size_(stack_size)
, metrics_(vm_->metrics())
@@ -29,14 +28,11 @@ namespace rubinius {
SharedState& shared = vm->shared;
State state_obj(vm), *state = &state_obj;

state->vm()->set_run_state(ManagedThread::eIndependent);
vm->set_current_thread();
vm->set_run_state(ManagedThread::eIndependent);

RBX_DTRACE_CHAR_P thread_name =
const_cast<RBX_DTRACE_CHAR_P>(thread->name_.c_str());
vm->set_name(thread_name);

RUBINIUS_THREAD_START(const_cast<RBX_DTRACE_CHAR_P>(thread_name),
vm->thread_id(), 1);
RUBINIUS_THREAD_START(
const_cast<RBX_DTRACE_CHAR_P>(vm->name().c_str()), vm->thread_id(), 1);

NativeMethod::init_thread(state);

@@ -48,12 +44,10 @@ namespace rubinius {

NativeMethod::cleanup_thread(state);

RUBINIUS_THREAD_STOP(const_cast<RBX_DTRACE_CHAR_P>(thread_name),
vm->thread_id(), 1);
RUBINIUS_THREAD_STOP(
const_cast<RBX_DTRACE_CHAR_P>(vm->name().c_str()), vm->thread_id(), 1);

if(state->vm()->run_state() != ManagedThread::eIndependent) {
shared.gc_independent();
}
shared.gc_independent();

return 0;
}
@@ -75,7 +69,7 @@ namespace rubinius {

if(int error = pthread_create(&vm_->os_thread(), &attrs,
InternalThread::run, (void*)this)) {
logger::fatal("%s: %s: create thread failed", strerror(error), name_.c_str());
logger::fatal("%s: %s: create thread failed", strerror(error), vm_->name().c_str());
::abort();
}

3 changes: 2 additions & 1 deletion vm/internal_threads.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef RBX_AUXILIARY_THREADS_H
#define RBX_AUXILIARY_THREADS_H

#include "lock.hpp"

#include "util/thread.hpp"

#include <string>
@@ -15,7 +17,6 @@ namespace rubinius {

class InternalThread {
VM* vm_;
std::string name_;
bool thread_running_;
uint32_t stack_size_;

3 changes: 1 addition & 2 deletions vm/llvm/state.hpp
Original file line number Diff line number Diff line change
@@ -42,7 +42,6 @@
#include "gc/managed.hpp"
#include "internal_threads.hpp"
#include "configuration.hpp"
#include "lock.hpp"
#include "metrics.hpp"
#include "util/thread.hpp"

@@ -71,7 +70,7 @@ namespace rubinius {
cMachineCode = 4
};

class LLVMState : public InternalThread, public Lockable {
class LLVMState : public InternalThread {
jit::RubiniusJITMemoryManager* memory_;
llvm::JITEventListener* jit_event_listener_;

10 changes: 8 additions & 2 deletions vm/lock.hpp
Original file line number Diff line number Diff line change
@@ -51,6 +51,10 @@ namespace rubinius {

class Mutex : public Lock, public utilities::thread::Mutex {
public:
Mutex(bool recursive)
: utilities::thread::Mutex(recursive)
{ }

bool mutex_p() {
return true;
}
@@ -153,6 +157,10 @@ namespace rubinius {
Mutex mutex_;

public:
Lockable(bool recursive)
: mutex_(recursive)
{ }

Mutex& mutex() {
return mutex_;
}
@@ -225,8 +233,6 @@ namespace rubinius {


#define SYNC(__state) LockableScopedLock __lsl_guard__(__state, this, __FILE__, __LINE__)
#define SYNC_TL LockableScopedLock __lsl_guard__(ManagedThread::current(), this, __FILE__, __LINE__)

#define UNSYNC __lsl_guard__.unlock()
#define RESYNC __lsl_guard__.relock()
}
3 changes: 1 addition & 2 deletions vm/metrics.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef RBX_METRICS_HPP
#define RBX_METRICS_HPP

#include "lock.hpp"
#include "internal_threads.hpp"

#include "gc/root.hpp"
@@ -375,7 +374,7 @@ namespace rubinius {
void reinit();
};

class Metrics : public InternalThread, public Lockable {
class Metrics : public InternalThread {
bool enabled_;

TypedRoot<Tuple*> values_;
13 changes: 6 additions & 7 deletions vm/object_memory.cpp
Original file line number Diff line number Diff line change
@@ -54,7 +54,8 @@ namespace rubinius {

/* ObjectMemory methods */
ObjectMemory::ObjectMemory(VM* state, Configuration& config)
: young_(new BakerGC(this, config.gc_young_initial_bytes * 2))
: Lockable(true)
, young_(new BakerGC(this, config.gc_young_initial_bytes * 2))
, mark_sweep_(new MarkSweepGC(this, config))
, immix_(new ImmixGC(this))
, immix_marker_(NULL)
@@ -452,15 +453,13 @@ namespace rubinius {
}
}

// TODO: Fix API to support proper testing.
void ObjectMemory::set_young_lifetime(size_t age) {
SYNC_TL;

young_->set_lifetime(age);
}

// TODO: Fix API to support proper testing.
void ObjectMemory::debug_marksweep(bool val) {
SYNC_TL;

if(val) {
mark_sweep_->free_entries = false;
} else {
@@ -825,7 +824,7 @@ namespace rubinius {
}

void ObjectMemory::add_type_info(TypeInfo* ti) {
SYNC_TL;
utilities::thread::SpinLock::LockGuard guard(shared_.type_info_lock());

if(TypeInfo* current = type_info[ti->type]) {
delete current;
@@ -1014,7 +1013,7 @@ namespace rubinius {
}

void ObjectMemory::add_code_resource(CodeResource* cr) {
SYNC_TL;
utilities::thread::SpinLock::LockGuard guard(shared_.code_resource_lock());

code_manager_.add_resource(cr, &collect_mature_now);
}
42 changes: 27 additions & 15 deletions vm/shared_state.cpp
Original file line number Diff line number Diff line change
@@ -31,7 +31,8 @@
namespace rubinius {

SharedState::SharedState(Environment* env, Configuration& config, ConfigParser& cp)
: internal_threads_(0)
: Lockable(true)
, internal_threads_(0)
, signals_(0)
, finalizer_thread_(0)
, console_(0)
@@ -48,6 +49,16 @@ namespace rubinius {
, root_vm_(0)
, env_(env)
, tool_broker_(new tooling::ToolBroker)
, ruby_critical_lock_()
, fork_exec_lock_()
, capi_ds_lock_()
, capi_locks_lock_()
, capi_constant_lock_()
, llvm_state_lock_()
, vm_lock_()
, wait_lock_()
, type_info_lock_()
, code_resource_lock_()
, use_capi_lock_(false)
, om(0)
, global_cache(new GlobalCache)
@@ -103,31 +114,32 @@ namespace rubinius {
return atomic::fetch_and_add(&thread_ids_, 1);
}

VM* SharedState::new_vm() {
uint32_t id = new_thread_id();

SYNC_TL;
VM* SharedState::new_vm(const char* name) {
utilities::thread::SpinLock::LockGuard guard(vm_lock_);

// TODO calculate the thread id by finding holes in the
// field of ids, so we reuse ids.
uint32_t id = new_thread_id();

VM* vm = new VM(id, *this);
VM* vm = new VM(id, *this, name);
threads_.push_back(vm);

// If there is no root vm, then the first one created becomes it.
if(!root_vm_) root_vm_ = vm;

return vm;
}

void SharedState::remove_vm(VM* vm) {
SYNC_TL;
utilities::thread::SpinLock::LockGuard guard(vm_lock_);

threads_.remove(vm);

// Don't delete ourself here, it's too problematic.
}

Array* SharedState::vm_threads(STATE) {
SYNC_TL;
utilities::thread::SpinLock::LockGuard guard(vm_lock_);

Array* threads = Array::create(state, 0);
for(ThreadList::iterator i = threads_.begin();
@@ -140,21 +152,18 @@ namespace rubinius {
}
}
}

return threads;
}

SignalThread* SharedState::start_signals(STATE) {
SYNC(state);

signals_ = new SignalThread(state);
signals_->start(state);

return signals_;
}

console::Console* SharedState::start_console(STATE) {
SYNC(state);

if(!console_) {
console_ = new console::Console(state);
console_->start(state);
@@ -164,8 +173,6 @@ namespace rubinius {
}

metrics::Metrics* SharedState::start_metrics(STATE) {
SYNC(state);

if(!metrics_) {
metrics_ = new metrics::Metrics(state);
metrics_->start(state);
@@ -227,6 +234,11 @@ namespace rubinius {
capi_ds_lock_.init();
capi_locks_lock_.init();
capi_constant_lock_.init();
llvm_state_lock_.init();
vm_lock_.init();
wait_lock_.init();
type_info_lock_.init();
code_resource_lock_.init();
internal_threads_->init();

om->after_fork_child(state);
@@ -343,7 +355,7 @@ namespace rubinius {
return 0;
}

Mutex* lock = new Mutex();
Mutex* lock = new Mutex(true);
capi_locks_.push_back(lock);

// We use a 1 offset index, so 0 can indicate no lock used
18 changes: 17 additions & 1 deletion vm/shared_state.hpp
Original file line number Diff line number Diff line change
@@ -123,6 +123,10 @@ namespace rubinius {
utilities::thread::SpinLock capi_locks_lock_;
utilities::thread::SpinLock capi_constant_lock_;
utilities::thread::SpinLock llvm_state_lock_;
utilities::thread::SpinLock vm_lock_;
utilities::thread::SpinLock wait_lock_;
utilities::thread::SpinLock type_info_lock_;
utilities::thread::SpinLock code_resource_lock_;

CApiBlackList capi_black_list_;
CApiLocks capi_locks_;
@@ -168,7 +172,7 @@ namespace rubinius {
finalizer_thread_ = thr;
}

VM* new_vm();
VM* new_vm(const char* name = NULL);
void remove_vm(VM*);

ThreadList* threads() {
@@ -295,6 +299,18 @@ namespace rubinius {
return llvm_state_lock_;
}

utilities::thread::SpinLock& wait_lock() {
return wait_lock_;
}

utilities::thread::SpinLock& type_info_lock() {
return type_info_lock_;
}

utilities::thread::SpinLock& code_resource_lock() {
return code_resource_lock_;
}

void scheduler_loop();

void after_fork_child(STATE, GCToken gct, CallFrame* call_frame);
3 changes: 1 addition & 2 deletions vm/signal.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef RBX_SIGNAL_HPP
#define RBX_SIGNAL_HPP

#include "lock.hpp"
#include "internal_threads.hpp"

#include "gc/root.hpp"
@@ -15,7 +14,7 @@ namespace rubinius {
class Configuration;
struct CallFrame;

class SignalThread : public InternalThread, public Lockable {
class SignalThread : public InternalThread {
const static int pending_signal_size_ = 256;
SharedState& shared_;

7 changes: 1 addition & 6 deletions vm/test/test.hpp
Original file line number Diff line number Diff line change
@@ -25,8 +25,8 @@ class VMTest {
config_parser = new ConfigParser;
shared = new SharedState(0, config, *config_parser);
VM* vm = shared->new_vm();
vm->metrics().init(metrics::eRubyMetrics);
vm->initialize_as_root();
vm->metrics().init(metrics::eRubyMetrics);
state = new State(vm);
}

@@ -49,11 +49,6 @@ class VMTest {
void tearDown() {
destroy();
}

VM* new_vm() {
return shared->new_vm();
}

};

#endif
2 changes: 1 addition & 1 deletion vm/test/test_thread.hpp
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ class TestThread : public CxxTest::TestSuite, public VMTest {
}

void test_create() {
Thread* thr = Thread::create(state, new_vm());
Thread* thr = Thread::create(state, shared->new_vm());

TS_ASSERT_DIFFERS(thr, Thread::current(state));
}
23 changes: 13 additions & 10 deletions vm/vm.cpp
Original file line number Diff line number Diff line change
@@ -66,8 +66,8 @@ namespace rubinius {
static rlim_t cMaxStack = (1024 * 1024 * 128);
#endif

VM::VM(uint32_t id, SharedState& shared)
: ManagedThread(id, shared, ManagedThread::eRuby)
VM::VM(uint32_t id, SharedState& shared, const char* name)
: ManagedThread(id, shared, ManagedThread::eRuby, name)
, saved_call_frame_(0)
, saved_call_site_information_(0)
, fiber_stacks_(this, shared)
@@ -119,6 +119,7 @@ namespace rubinius {
}

void VM::initialize_as_root() {
set_current_thread();

om = new ObjectMemory(this, shared.config);
shared.om = om;
@@ -144,8 +145,6 @@ namespace rubinius {
Thread::create(&state, this);
thread->alive(&state, cTrue);
thread->sleep(&state, cFalse);

VM::set_current(this, "rbx.ruby.main");
}

void VM::initialize_config() {
@@ -187,8 +186,8 @@ namespace rubinius {
/**
* Sets this VM instance as the current VM on this pthread.
*/
void VM::set_current(VM* vm, std::string name) {
ManagedThread::set_current(vm, name);
void VM::set_current_thread() {
ManagedThread::set_current_thread(this);
}

Object* VM::new_object_typed_dirty(Class* cls, size_t size, object_type type) {
@@ -416,7 +415,8 @@ namespace rubinius {
}

void VM::clear_waiter() {
SYNC_TL;
utilities::thread::SpinLock::LockGuard guard(shared.wait_lock());

vm_jit_.interrupt_with_signal_ = false;
waiting_channel_.set(nil<Channel>());
waiting_object_.set(cNil);
@@ -425,18 +425,21 @@ namespace rubinius {
}

void VM::wait_on_channel(Channel* chan) {
SYNC_TL;
utilities::thread::SpinLock::LockGuard guard(shared.wait_lock());

thread->sleep(this, cTrue);
waiting_channel_.set(chan);
}

void VM::wait_on_inflated_lock(Object* wait) {
SYNC_TL;
utilities::thread::SpinLock::LockGuard guard(shared.wait_lock());

waiting_object_.set(wait);
}

void VM::wait_on_custom_function(void (*func)(void*), void* data) {
SYNC_TL;
utilities::thread::SpinLock::LockGuard guard(shared.wait_lock());

custom_wakeup_ = func;
custom_wakeup_data_ = data;
}
5 changes: 3 additions & 2 deletions vm/vm.hpp
Original file line number Diff line number Diff line change
@@ -342,14 +342,13 @@ namespace rubinius {
static void init_stack_size();

static VM* current();
static void set_current(VM* vm, std::string name);

static void discard(STATE, VM*);

public:

/* Prototypes */
VM(uint32_t id, SharedState& shared);
VM(uint32_t id, SharedState& shared, const char* name = NULL);
~VM();

void initialize_as_root();
@@ -359,6 +358,8 @@ namespace rubinius {
void bootstrap_symbol(STATE);
void initialize_config();

void set_current_thread();

void setup_errno(STATE, int num, const char* name, Class* sce, Module* ern);
void bootstrap_exceptions(STATE);
void initialize_fundamental_constants(STATE);

0 comments on commit 4b5b02d

Please sign in to comment.