Skip to content

Commit

Permalink
Merge branch 'close_schedule_fix'
Browse files Browse the repository at this point in the history
* close_schedule_fix:
  Expose EM.num_close_scheduled, and utilise in TimeTilNextEvent to avoid long waits for unbinds.
  • Loading branch information
raggi committed Sep 29, 2011
2 parents 86bb981 + 2cd0c19 commit 8cd81fb
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 2 deletions.
9 changes: 9 additions & 0 deletions ext/cmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ extern "C" int evma_is_paused (const unsigned long binding)
return 0;
}

/************************
evma_num_close_scheduled
************************/

extern "C" int evma_num_close_scheduled ()
{
return EventMachine->NumCloseScheduled;
}

/**********************
evma_create_tcp_server
**********************/
Expand Down
2 changes: 2 additions & 0 deletions ext/ed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ EventableDescriptor::~EventableDescriptor()
(*EventCallback)(ProxiedFrom->GetBinding(), EM_PROXY_TARGET_UNBOUND, NULL, 0);
ProxiedFrom->StopProxy();
}
MyEventMachine->NumCloseScheduled--;
StopProxy();
Close();
}
Expand Down Expand Up @@ -214,6 +215,7 @@ EventableDescriptor::ScheduleClose

void EventableDescriptor::ScheduleClose (bool after_writing)
{
MyEventMachine->NumCloseScheduled++;
// KEEP THIS SYNCHRONIZED WITH ::IsCloseScheduled.
if (after_writing)
bCloseAfterWriting = true;
Expand Down
5 changes: 3 additions & 2 deletions 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),
NumCloseScheduled (0),
bTerminateSignalReceived (false),
bEpoll (false),
epfd (-1),
Expand Down Expand Up @@ -689,10 +690,10 @@ timeval EventMachine_t::_TimeTilNextEvent()
if (!NewDescriptors.empty() || !ModifiedDescriptors.empty()) {
next_event = MyCurrentLoopTime;
}

timeval tv;

if (next_event == 0) {
if (next_event == 0 || NumCloseScheduled > 0) {
tv = Quantum;
} else {
if (next_event > MyCurrentLoopTime) {
Expand Down
1 change: 1 addition & 0 deletions ext/em.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class EventMachine_t
public:
void _ReadLoopBreaker();
void _ReadInotifyEvents();
int NumCloseScheduled;

private:
enum {
Expand Down
2 changes: 2 additions & 0 deletions ext/eventmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ extern "C" {
int evma_is_paused(const unsigned long binding);
int evma_resume(const unsigned long binding);

int evma_num_close_scheduled();

void evma_stop_tcp_server (const unsigned long signature);
const unsigned long evma_create_tcp_server (const char *address, int port);
const unsigned long evma_create_unix_domain_server (const char *filename);
Expand Down
10 changes: 10 additions & 0 deletions ext/rubymain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,15 @@ static VALUE t_paused_p (VALUE self, VALUE signature)
return evma_is_paused(NUM2ULONG (signature)) ? Qtrue : Qfalse;
}

/*********************
t_num_close_scheduled
*********************/

static VALUE t_num_close_scheduled (VALUE self)
{
return INT2FIX(evma_num_close_scheduled());
}

/*****************
t_open_udp_socket
*****************/
Expand Down Expand Up @@ -1180,6 +1189,7 @@ extern "C" void Init_rubyeventmachine()
rb_define_module_function (EmModule, "pause_connection", (VALUE (*)(...))t_pause, 1);
rb_define_module_function (EmModule, "resume_connection", (VALUE (*)(...))t_resume, 1);
rb_define_module_function (EmModule, "connection_paused?", (VALUE (*)(...))t_paused_p, 1);
rb_define_module_function (EmModule, "num_close_scheduled", (VALUE (*)(...))t_num_close_scheduled, 0);

rb_define_module_function (EmModule, "start_proxy", (VALUE (*)(...))t_start_proxy, 4);
rb_define_module_function (EmModule, "stop_proxy", (VALUE (*)(...))t_stop_proxy, 1);
Expand Down
21 changes: 21 additions & 0 deletions tests/test_basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,25 @@ def test_bubble_errors_from_initialize
end
end
end

def test_schedule_close
localhost, port = '127.0.0.1', 9000
timer_ran = false
num_close_scheduled = nil
EM.run do
assert_equal 0, EM.num_close_scheduled
EM.add_timer(1) { timer_ran = true; EM.stop }
EM.start_server localhost, port do |s|
s.close_connection
num_close_scheduled = EM.num_close_scheduled
end
EM.connect localhost, port do |c|
def c.unbind
EM.stop
end
end
end
assert !timer_ran
assert_equal 1, num_close_scheduled
end
end

0 comments on commit 8cd81fb

Please sign in to comment.