Created
May 28, 2012 12:21
-
-
Save funny-falcon/2818868 to your computer and use it in GitHub Desktop.
EM finer grained deffered callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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