Skip to content

Instantly share code, notes, and snippets.

@fogus
Forked from samaaron/gist:6509442
Created September 10, 2013 13:35
Show Gist options
  • Save fogus/6509493 to your computer and use it in GitHub Desktop.
Save fogus/6509493 to your computer and use it in GitHub Desktop.
require 'thread'
module SonicPi
class Promise
VAL_SEM = Mutex.new
PUSH_SEM = Mutex.new
def initialize
@box = Queue.new
@value = nil
@delivered = false
@pushed = false
end
def get
return @value if @delivered
VAL_SEM.synchronize do
return @value if @delivered
val = @box.pop
@value = val
@delivered = true
val
end
end
def deliver!(val)
PUSH_SEM.synchronize do
if(@box.empty? && !@pushed)
@box.push val
@pushed = true
else
raise "Promise already delivered"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment