Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help Rubinius out with collecting garbage #202

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ext/cmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ extern "C" void evma_release_library()
evma_run_machine
****************/

extern "C" void evma_run_machine()
extern "C" int evma_run_machine()
{
ensure_eventmachine("evma_run_machine");
EventMachine->Run();
return EventMachine->Run();
}


Expand Down Expand Up @@ -887,3 +887,9 @@ extern "C" uint64_t evma_get_current_loop_time()
ensure_eventmachine("evma_get_current_loop_time");
return EventMachine->GetCurrentLoopTime();
}

extern "C" void evma_set_one_shot_only(int val)
{
ensure_eventmachine("evma_set_one_shot_only");
EventMachine->SetOneShotOnly(val == 1);
}
7 changes: 6 additions & 1 deletion ext/em.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ EventMachine_t::EventMachine_t (EMCallback event_callback):
NextHeartbeatTime (0),
LoopBreakerReader (-1),
LoopBreakerWriter (-1),
OneShotOnly(false),
NumCloseScheduled (0),
bTerminateSignalReceived (false),
bEpoll (false),
Expand Down Expand Up @@ -484,7 +485,7 @@ void EventMachine_t::ClearHeartbeat(uint64_t key, EventableDescriptor* ed)
EventMachine_t::Run
*******************/

void EventMachine_t::Run()
int EventMachine_t::Run()
{
#ifdef HAVE_EPOLL
if (bEpoll) {
Expand Down Expand Up @@ -537,7 +538,11 @@ void EventMachine_t::Run()
_RunOnce();
if (bTerminateSignalReceived)
break;
if (OneShotOnly)
return 0;
}

return 1;
}


Expand Down
8 changes: 7 additions & 1 deletion ext/em.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class EventMachine_t
EventMachine_t (EMCallback);
virtual ~EventMachine_t();

void Run();
int Run();
void ScheduleHalt();
void SignalLoopBreaker();
const unsigned long InstallOneshotTimer (int);
Expand Down Expand Up @@ -173,6 +173,10 @@ class EventMachine_t

uint64_t GetRealTime();

bool SetOneShotOnly(bool val) {
OneShotOnly = val;
}

private:
void _RunOnce();
void _RunTimers();
Expand Down Expand Up @@ -232,6 +236,8 @@ class EventMachine_t
unsigned LastTickCount;
#endif

bool OneShotOnly;

#ifdef OS_DARWIN
mach_timebase_info_data_t mach_timebase;
#endif
Expand Down
3 changes: 2 additions & 1 deletion ext/eventmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
};

void evma_initialize_library (EMCallback);
void evma_run_machine();
int evma_run_machine();
void evma_release_library();
const unsigned long evma_install_oneshot_timer (int seconds);
const unsigned long evma_connect_to_server (const char *bind_addr, int bind_port, const char *server, int port);
Expand Down Expand Up @@ -119,6 +119,7 @@ extern "C" {
void evma_set_kqueue (int use);

uint64_t evma_get_current_loop_time();
void evma_set_one_shot_only(int val);
#if __cplusplus
}
#endif
Expand Down
11 changes: 9 additions & 2 deletions ext/rubymain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ t_run_machine_without_threads

static VALUE t_run_machine_without_threads (VALUE self)
{
evma_run_machine();
return Qnil;
return evma_run_machine() ? Qtrue : Qfalse;
}


Expand Down Expand Up @@ -1002,6 +1001,12 @@ static VALUE t__kqueue_set (VALUE self, VALUE val)
return val;
}

static VALUE t_set_one_shot_only (VALUE self, VALUE val)
{
evma_set_one_shot_only(RTEST(val));
return val;
}


/********
t__ssl_p
Expand Down Expand Up @@ -1316,6 +1321,8 @@ extern "C" void Init_rubyeventmachine()
rb_define_module_function (EmModule, "kqueue=", (VALUE(*)(...))t__kqueue_set, 1);
rb_define_module_function (EmModule, "kqueue?", (VALUE(*)(...))t__kqueue_p, 0);

rb_define_module_function (EmModule, "one_shot_only=", (VALUE(*)(...))t_set_one_shot_only, 1);

rb_define_module_function (EmModule, "ssl?", (VALUE(*)(...))t__ssl_p, 0);

rb_define_method (EmConnection, "get_outbound_data_size", (VALUE(*)(...))conn_get_outbound_data_size, 0);
Expand Down
16 changes: 15 additions & 1 deletion lib/eventmachine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,26 @@ def self.run blk=nil, tail=nil, &block
@reactor_pid = Process.pid
@reactor_running = true
initialize_event_machine

# Only have +run_machine+ crank once and then return.
# This allows rubinius to deal with the objects accessed
# by +run_machine+ properly as garbage.
if defined?(RUBY_ENGINE) and RUBY_ENGINE == "rbx"
self.one_shot_only = true
end

(b = blk || block) and add_timer(0, b)
if @next_tick_queue && !@next_tick_queue.empty?
add_timer(0) { signal_loopbreak }
end
@reactor_thread = Thread.current
run_machine

# Crank the machine in a loop. +run_machine+ returns
# true if there is a legit reason to exit the loop, otherwise
# we just keep running it.
while true
break if run_machine
end
ensure
until @tails.empty?
@tails.pop.call
Expand Down