Skip to content

Commit

Permalink
Added barrier
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Diehl committed Aug 29, 2009
1 parent fcbd2f3 commit c07becd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
14 changes: 11 additions & 3 deletions dataflow.rb
Expand Up @@ -31,6 +31,10 @@ def by_need(&block)
Variable.new &block
end

def barrier(*variables)
variables.each{|v| v.__wait__ }
end

def need_later(&block)
local do |future|
Thread.new { unify future, block.call }
Expand Down Expand Up @@ -71,17 +75,21 @@ def __activate_trigger__
@__trigger__ = nil # GC
end

def method_missing(name, *args, &block)
def __wait__
LOCK.synchronize do
unless @__bound__
return "#<Dataflow::Variable:#{__id__} unbound>" if name == :inspect
unless @__bound__
if @__trigger__
__activate_trigger__
else
__binding_condition__.wait
end
end
end unless @__bound__
end

def method_missing(name, *args, &block)
return "#<Dataflow::Variable:#{__id__} unbound>" if !@__bound__ && name == :inspect
__wait__
@__value__.__send__(name, *args, &block)
end

Expand Down
25 changes: 25 additions & 0 deletions spec/barrier_spec.rb
@@ -0,0 +1,25 @@
require "#{File.dirname(__FILE__)}/spec_helper"

describe 'A barrier' do
it 'waits for variables to be bound before continuing' do
local do |x, y, barrier_broken|
Thread.new do
barrier x, y
unify barrier_broken, true
end
Thread.new { unify x, :x }
Thread.new { unify y, :y }
barrier_broken.should be_true
end
end

it 'continues for variables that are already bound' do
local do |x, y, barrier_broken|
unify x, :x
unify y, :y
barrier x, y
unify barrier_broken, true
barrier_broken.should be_true
end
end
end

0 comments on commit c07becd

Please sign in to comment.