forked from travis-ci/travis-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sven Fuchs
committed
Mar 20, 2011
1 parent
c39bcf3
commit 030560e
Showing
14 changed files
with
297 additions
and
109 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Hash | ||
def compact | ||
dup.compact! | ||
end | ||
|
||
def compact! | ||
keys.each do |key| | ||
delete(key) if self[key].nil? | ||
end | ||
self | ||
end | ||
end unless {}.respond_to?(:compact) |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module Travis | ||
class Synchronizer < Array | ||
class << self | ||
def timeout | ||
@@timeout ||= 2 | ||
end | ||
|
||
def timeout=(timeout) | ||
@@timeout = timeout | ||
end | ||
|
||
def synchronizers | ||
@@synchronizers ||= {} | ||
end | ||
|
||
def receive(id, msg_id, &forward) | ||
synchronizers[id] ||= new(id) | ||
synchronizers[id].receive(msg_id, &forward) | ||
end | ||
end | ||
|
||
attr_reader :id, :last_id, :finalizer | ||
|
||
def initialize(id) | ||
@id = id | ||
@last_id = 0 | ||
end | ||
|
||
def receive(msg_id, &forward) | ||
if msg_id | ||
synchronize(msg_id, &forward) | ||
else | ||
forward.call | ||
end | ||
end | ||
|
||
def synchronize(msg_id, &forward) | ||
with_finalizer do | ||
push([msg_id.to_i, forward]) | ||
sort! | ||
while !empty? && last_id == self[0][0].to_i - 1 | ||
@last_id = self[0][0].to_i | ||
shift[1].call | ||
end | ||
end | ||
end | ||
|
||
def with_finalizer | ||
EM.cancel_timer(finalizer) | ||
yield | ||
@finalizer = EM.add_timer(self.class.timeout) { finalize } | ||
end | ||
|
||
def finalize | ||
shift[1].call until empty? | ||
self.class.synchronizers.delete(id) | ||
end | ||
|
||
def sort! | ||
super { |lft, rgt| lft[0] <=> rgt[0] } | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'rubygems' | ||
require 'eventmachine' | ||
require 'travis' | ||
|
||
EM.run do | ||
Travis::Synchronizer.timeout = 0.2 | ||
|
||
Travis::Synchronizer.receive(1, 2) { p 2 } | ||
Travis::Synchronizer.receive(1, 1) { p 1 } | ||
Travis::Synchronizer.receive(1, 4) { p 4 } | ||
|
||
EM.add_timer(0.3) { EM.stop } | ||
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
Oops, something went wrong.