Skip to content

Commit

Permalink
Don't discard MachineThread ThreadState on fork.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Feb 22, 2020
1 parent 11248ae commit 43a6850
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 24 deletions.
11 changes: 7 additions & 4 deletions machine/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

#include <zlib.h>

#include <chrono>
#include <sstream>
#include <thread>

namespace rubinius {
namespace logger {
Expand Down Expand Up @@ -377,7 +379,7 @@ namespace rubinius {
}

void FileLogger::SemaphoreLock::lock() {
uint64_t nanoseconds = 0;
int nanoseconds = 0;

while(sem_trywait(semaphore_) != 0) {
switch(errno) {
Expand All @@ -399,10 +401,11 @@ namespace rubinius {
472, 288, 131, 31, 435, 258, 221, 73, 537, 854
};
static int modulo = sizeof(delay) / sizeof(int);
static struct timespec ts = {0, 0};

nanoseconds += ts.tv_nsec = delay[i++ % modulo];
nanosleep(&ts, NULL);
int ns = delay[i++ % modulo];
std::this_thread::sleep_for(std::chrono::nanoseconds(ns));

nanoseconds += ns;

if(nanoseconds > cLockLimit * 2) {
logger::abort("logger: possible invalid semaphore state detected, unable to reset");
Expand Down
2 changes: 1 addition & 1 deletion machine/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace rubinius {
class SemaphoreLock {
sem_t* semaphore_;

const static uint64_t cLockLimit = 1000000000;
const static uint64_t cLockLimit = 100000;

public:
SemaphoreLock(sem_t* semaphore)
Expand Down
17 changes: 11 additions & 6 deletions machine/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,7 @@ namespace rubinius {

void Machine::halt_thread_nexus(STATE) {
if(_thread_nexus_) {
// TODO: remove restriction on deleting ThreadNexus
// delete _thread_nexus_;
delete _thread_nexus_;
_thread_nexus_ = nullptr;
}
}
Expand Down Expand Up @@ -519,6 +518,15 @@ namespace rubinius {
}
}

void Machine::halt_machine(STATE) {
NativeMethod::cleanup_thread(state);

new(&_waiting_mutex_) std::mutex;
new(&_waiting_condition_) std::condition_variable;

state->discard();
}

int Machine::halt(STATE, Object* exit_code) {
int code = -1;

Expand Down Expand Up @@ -567,10 +575,7 @@ namespace rubinius {
halt_environment(state);
halt_logger(state);
halt_machine_state(state);

NativeMethod::cleanup_thread(state);

state->discard();
halt_machine(state);

exit(exit_code);

Expand Down
1 change: 1 addition & 0 deletions machine/machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ namespace rubinius {
void halt_environment(STATE);
void halt_logger(STATE);
void halt_machine_state(STATE);
void halt_machine(STATE);
};
}
#endif
6 changes: 1 addition & 5 deletions machine/machine_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ namespace rubinius {
pthread_attr_t attrs;
pthread_attr_init(&attrs);
pthread_attr_setstacksize(&attrs, stack_size_);
pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);

if(int error = pthread_create(&thread_state_->os_thread(), &attrs,
MachineThread::run, (void*)this)) {
Expand All @@ -88,10 +89,6 @@ namespace rubinius {
UnmanagedPhase unmanaged(state);

wakeup(state);

void* return_value;
pthread_t os = thread_state_->os_thread();
pthread_join(os, &return_value);
}

void MachineThread::stop(STATE) {
Expand All @@ -100,7 +97,6 @@ namespace rubinius {
}

void MachineThread::after_fork_child(STATE) {
thread_state_ = state->thread_nexus()->create_thread_state(state->machine());
start(state);
}
}
2 changes: 1 addition & 1 deletion machine/signals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace rubinius {
public:

SignalThread(STATE, std::mutex& lock, std::condition_variable& condition);
~SignalThread() { }
virtual ~SignalThread() { }

void initialize(STATE);
void stop(STATE);
Expand Down
13 changes: 6 additions & 7 deletions machine/thread_nexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,20 @@ namespace rubinius {
threads_mutex_.try_lock();
threads_mutex_.unlock();

while(!threads_.empty()) {
ThreadState* thread_state = threads_.back();
threads_.pop_back();
for(auto i = threads_.begin(); i != threads_.end();) {
ThreadState* thread_state = *i;

switch(thread_state->kind()) {
case ThreadState::eThread: {
if(Thread* thread = thread_state->thread()) {
if(!thread->nil_p()) {
if(thread_state == state) {
thread->current_fiber(state, thread->fiber());
++i;
continue;
} else {
thread_state->set_thread_dead();
i = threads_.erase(i);
}
}
}
Expand All @@ -157,17 +158,15 @@ namespace rubinius {
}

thread_state->set_thread_dead();
i = threads_.erase(i);

break;
}
case ThreadState::eSystem:
thread_state->set_thread_dead();
thread_state->discard();
++i;
break;
}
}

threads_.push_back(state);
}

static const char* phase_name(ThreadState* thread_state) {
Expand Down

0 comments on commit 43a6850

Please sign in to comment.