Skip to content

Commit

Permalink
Reworked running finalizers.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Jun 4, 2016
1 parent 949b25e commit 2310fd3
Show file tree
Hide file tree
Showing 25 changed files with 419 additions and 561 deletions.
4 changes: 0 additions & 4 deletions core/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,6 @@ def epilogue

run_at_exits

@stage = "running object finalizers"
::GC.start
ObjectSpace.run_finalizers

flush_stdio

rescue Object => e
Expand Down
11 changes: 8 additions & 3 deletions core/object_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def self.each_object(what=nil, &block)
def self.define_finalizer(obj, prc=nil, &block)
prc ||= block

if obj.kind_of? ImmediateValue or obj.kind_of? Float
raise ArgumentError, "can't define finalizer for #{obj.class}"
end

if obj.frozen?
raise RuntimeError, "can't modify frozen #{obj.class}"
end

if obj.equal? prc
# This is allowed. This is the Rubinius specific API that calls
# __finalize__ when the object is finalized.
Expand All @@ -74,9 +82,6 @@ def self.undefine_finalizer(obj)
return obj
end

def self.run_finalizers
end

def self.garbage_collect
GC.start
end
Expand Down
2 changes: 0 additions & 2 deletions core/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ def self.fork
end
end

ObjectSpace.run_finalizers

# Do not use Kernel.exit. This raises a SystemExit exception, which
# will run ensure blocks. This is not what MRI does and causes bugs
# in programs. See issue http://github.com/rubinius/rubinius/issues#issue/289 for
Expand Down
2 changes: 1 addition & 1 deletion machine/builtin/call_site.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ namespace rubinius {
cache->name(name);
cache->ip(ip);

state->memory()->needs_finalization(state, cache,
state->memory()->native_finalizer(state, cache,
(memory::FinalizerFunction)&CallSite::finalize);

state->vm()->metrics().machine.call_site_count++;
Expand Down
4 changes: 2 additions & 2 deletions machine/builtin/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace rubinius {
data->internal(rdata);

if(mark || free) {
state->memory()->needs_finalization(state, data,
state->memory()->extension_finalizer(state, data,
(memory::FinalizerFunction)&Data::finalize);
}

Expand Down Expand Up @@ -73,7 +73,7 @@ namespace rubinius {
data->internal(rdata);

if(type->function.dmark || type->function.dfree) {
state->memory()->needs_finalization(state, data,
state->memory()->extension_finalizer(state, data,
(memory::FinalizerFunction)&Data::finalize);
}

Expand Down
5 changes: 2 additions & 3 deletions machine/builtin/dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ namespace rubinius {
Dir* d = Dir::allocate(state, G(dir));
d->os(0);

state->memory()->needs_finalization(state, d,
(memory::FinalizerFunction)&Dir::finalize,
memory::FinalizeObject::eUnmanaged);
state->memory()->native_finalizer(state, d,
(memory::FinalizerFunction)&Dir::finalize);

return d;
}
Expand Down
5 changes: 2 additions & 3 deletions machine/builtin/encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,8 @@ namespace rubinius {

c->converter(NULL);

state->memory()->needs_finalization(state, c,
(memory::FinalizerFunction)&Converter::finalize,
memory::FinalizeObject::eUnmanaged);
state->memory()->native_finalizer(state, c,
(memory::FinalizerFunction)&Converter::finalize);

return c;
}
Expand Down
7 changes: 3 additions & 4 deletions machine/builtin/ffi_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,13 @@ namespace rubinius {

if(autorelease) {
if(!set_finalizer) {
state->memory()->needs_finalization(state, this,
(memory::FinalizerFunction)&Pointer::finalize,
memory::FinalizeObject::eUnmanaged);
state->memory()->native_finalizer(state, this,
(memory::FinalizerFunction)&Pointer::finalize);
set_finalizer = true;
}
} else {
if(set_finalizer) {
state->memory()->set_ruby_finalizer(this, cNil);
state->memory()->managed_finalizer(state, this, cNil);
set_finalizer = false;
}
}
Expand Down
10 changes: 4 additions & 6 deletions machine/builtin/fiber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ namespace rubinius {
fib->data(state->vm()->new_fiber_data(true, fib->stack_size()->to_native()));
fib->data()->set_call_frame(state->vm()->call_frame());

state->memory()->needs_finalization(state, fib,
(memory::FinalizerFunction)&Fiber::finalize,
memory::FinalizeObject::eUnmanaged);
state->memory()->native_finalizer(state, fib,
(memory::FinalizerFunction)&Fiber::finalize);

state->vm()->current_fiber.set(fib);
state->vm()->root_fiber.set(fib);
Expand Down Expand Up @@ -119,9 +118,8 @@ namespace rubinius {

state->vm()->metrics().system.fibers_created++;

state->memory()->needs_finalization(state, fib,
(memory::FinalizerFunction)&Fiber::finalize,
memory::FinalizeObject::eUnmanaged);
state->memory()->native_finalizer(state, fib,
(memory::FinalizerFunction)&Fiber::finalize);

return fib;
#else
Expand Down
10 changes: 4 additions & 6 deletions machine/builtin/fsevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ namespace rubinius {
if(fsevent->kq() < 0) {
logger::error("%s: unable to create kqueue", strerror(errno));
} else {
state->memory()->needs_finalization(state, fsevent,
(memory::FinalizerFunction)&FSEvent::finalize,
memory::FinalizeObject::eUnmanaged);
state->memory()->native_finalizer(state, fsevent,
(memory::FinalizerFunction)&FSEvent::finalize);
}

return fsevent;
Expand Down Expand Up @@ -84,9 +83,8 @@ namespace rubinius {
if(fsevent->in() < 0) {
logger::error("%s: unable to create inotify", strerror(errno));
} else {
state->memory()->needs_finalization(state, fsevent,
(memory::FinalizerFunction)&FSEvent::finalize,
memory::FinalizeObject::eUnmanaged);
state->memory()->native_finalizer(state, fsevent,
(memory::FinalizerFunction)&FSEvent::finalize);
}

return fsevent;
Expand Down
4 changes: 2 additions & 2 deletions machine/builtin/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace rubinius {

// Don't bother to add finalization for stdio
if(fd >= 3) {
state->memory()->needs_finalization(state, io,
state->memory()->extension_finalizer(state, io,
(memory::FinalizerFunction)&IO::finalize);
}

Expand All @@ -86,7 +86,7 @@ namespace rubinius {
IO* io = state->memory()->new_object<IO>(state, as<Class>(self));
io->ibuffer(state, IOBuffer::create(state));

state->memory()->needs_finalization(state, io,
state->memory()->extension_finalizer(state, io,
(memory::FinalizerFunction)&IO::finalize);

return io;
Expand Down
2 changes: 1 addition & 1 deletion machine/builtin/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@ namespace rubinius {

Object* System::vm_set_finalizer(STATE, Object* obj, Object* fin) {
if(!obj->reference_p()) return cFalse;
state->memory()->set_ruby_finalizer(obj, fin);
state->memory()->managed_finalizer(state, obj, fin);
return cTrue;
}

Expand Down
5 changes: 2 additions & 3 deletions machine/builtin/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ namespace rubinius {

thr->function(function);

state->memory()->needs_finalization(state, thr,
(memory::FinalizerFunction)&Thread::finalize,
memory::FinalizeObject::eUnmanaged);
state->memory()->native_finalizer(state, thr,
(memory::FinalizerFunction)&Thread::finalize);

state->vm()->metrics().system.threads_created++;

Expand Down
2 changes: 1 addition & 1 deletion machine/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ namespace rubinius {

shared->thread_nexus()->lock(state->vm());

shared->finalizer_handler()->finish(state);
shared->finalizer()->finish(state);

NativeMethod::cleanup_thread(state);

Expand Down
2 changes: 1 addition & 1 deletion machine/machine_threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace rubinius {

MachineThread::MachineThread(STATE, std::string name, StackSize stack_size)
: vm_(state->shared().thread_nexus()->new_vm(&state->shared(), name.c_str()))
, thread_running_(false)
, stack_size_(stack_size)
, thread_running_(false)
, thread_exit_(false)
{
state->shared().machine_threads()->register_thread(this);
Expand Down
5 changes: 3 additions & 2 deletions machine/machine_threads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "util/thread.hpp"

#include <atomic>
#include <string>
#include <list>

Expand All @@ -16,12 +17,12 @@ namespace rubinius {

class MachineThread {
VM* vm_;
bool thread_running_;
uint32_t stack_size_;

protected:

bool thread_exit_;
std::atomic<bool> thread_running_;
std::atomic<bool> thread_exit_;

public:

Expand Down
24 changes: 0 additions & 24 deletions machine/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,6 @@ namespace rubinius {

if(!collect_young_flag_ && !collect_full_flag_) return;

if(memory::FinalizerThread* finalizer = state->shared().finalizer_handler()) {
finalizer->start_collection(state);
}

if(cDebugThreading) {
std::cerr << std::endl << "[" << state
<< " WORLD beginning GC.]" << std::endl;
Expand Down Expand Up @@ -584,10 +580,6 @@ namespace rubinius {
#ifdef RBX_GC_DEBUG
young_->verify(data);
#endif
if(memory::FinalizerThread* finalizer = state->shared().finalizer_handler()) {
finalizer->finish_collection(state);
}
*/

collect_young_flag_ = false;
Expand Down Expand Up @@ -666,10 +658,6 @@ namespace rubinius {
metrics.gc.immix_count++;
metrics.gc.large_count++;

if(memory::FinalizerThread* finalizer = state->shared().finalizer_handler()) {
finalizer->finish_collection(state);
}

collect_full_flag_ = false;
interrupt_flag_ = false;

Expand Down Expand Up @@ -846,18 +834,6 @@ namespace rubinius {
}
}

void Memory::needs_finalization(STATE, Object* obj, memory::FinalizerFunction func,
memory::FinalizeObject::FinalizeKind kind)
{
if(memory::FinalizerThread* finalizer = shared_.finalizer_handler()) {
finalizer->record(state, obj, func, kind);
}
}

void Memory::set_ruby_finalizer(Object* obj, Object* finalizer) {
shared_.finalizer_handler()->set_ruby_finalizer(obj, finalizer);
}

capi::Handle* Memory::add_capi_handle(STATE, Object* obj) {
if(!obj->reference_p()) {
rubinius::bug("Trying to add a handle for a non reference");
Expand Down
24 changes: 19 additions & 5 deletions machine/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ namespace rubinius {
}
}

memory::FinalizerThread* finalizer_handler() const {
return shared_.finalizer_handler();
memory::FinalizerThread* finalizer() const {
return shared_.finalizer();
}

memory::InflatedHeaders* inflated_headers() const {
Expand Down Expand Up @@ -550,9 +550,23 @@ namespace rubinius {

void collect_maybe(STATE);

void needs_finalization(STATE, Object* obj, memory::FinalizerFunction func,
memory::FinalizeObject::FinalizeKind kind = memory::FinalizeObject::eManaged);
void set_ruby_finalizer(Object* obj, Object* finalizer);
void native_finalizer(STATE, Object* obj, memory::FinalizerFunction func) {
if(memory::FinalizerThread* f = this->finalizer()) {
f->native_finalizer(state, obj, func);
}
}

void extension_finalizer(STATE, Object* obj, memory::FinalizerFunction func) {
if(memory::FinalizerThread* f = this->finalizer()) {
f->extension_finalizer(state, obj, func);
}
}

void managed_finalizer(STATE, Object* obj, Object* finalizer) {
if(memory::FinalizerThread* f = this->finalizer()) {
f->managed_finalizer(state, obj, finalizer);
}
}

InflatedHeader* inflate_header(STATE, ObjectHeader* obj);
void inflate_for_id(STATE, ObjectHeader* obj, uint32_t id);
Expand Down
Loading

0 comments on commit 2310fd3

Please sign in to comment.