Skip to content

Instantly share code, notes, and snippets.

@funny-falcon
Created May 28, 2012 12:21
Show Gist options
  • Save funny-falcon/2818868 to your computer and use it in GitHub Desktop.
Save funny-falcon/2818868 to your computer and use it in GitHub Desktop.
EM finer grained deffered callbacks
def EM.run_deferred_callbacks
until (@resultqueue ||= []).empty?
result,cback = @resultqueue.pop
cback.call result if cback
end
# Capture the size at the start of this tick...
time = Time.now
size = @next_tick_queue.size
cur_ticks = @next_tick_mutex.synchronize{ @next_tick_queue.slice!(0, size) }
size.times do |i|
callback = cur_ticks[i]
cur_ticks[i] = nil
begin
callback.call
ensure
EM.next_tick {} if $!
end
if i & 15 == 15 && Time.now - time > 0.005
@next_tick_mutex.synchronize{
@next_tick_queue[0,0] = cur_ticks.slice(i+1, size-i-1)
}
break
end
end
signal_loopbreak if @next_tick_queue.size > 0
end
def EM.run_deferred_callbacks
until (@resultqueue ||= []).empty?
result,cback = @resultqueue.pop
cback.call result if cback
end
cur_ticks = @next_tick_mutex.synchronize {
cur, @next_tick_queue = @next_tick_queue, []
cur
}
size = cur_ticks.size
next_to_work = 0
begin
while next_to_work < size
callback = cur_ticks[next_to_work]
cur_ticks[next_to_work] = nil
next_to_work += 1
callback.call
end
ensure
if next_to_work < size
@next_tick_mutex.synchronize do
@next_tick_queue[0,0] = cur_ticks.slice(next_to_work, size)
end
end
end
rescue
signal_loopbreak
raise
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment