Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 122 lines (103 sloc) 3.036 kb
ca59c52 Use monitors for handling locking and waiting
Larry Diehl authored
1 require 'monitor'
916059f Added locking to work with real concurrency in JRuby in addition to
Larry Diehl authored
2
69cab66 Added basic hash-like functionality
Larry Diehl authored
3 module Dataflow
a1068cb Version bump
Larry Diehl authored
4 VERSION = "0.3.1"
5199701 Support custom forkers
Larry Diehl authored
5 class << self
6 attr_accessor :forker
7 end
8 self.forker = Thread.method(:fork)
23d7bef Added a Rakefile for building the gem
Larry Diehl authored
9
c106962 Added a class-level "declare" method to create object-specific
Larry Diehl authored
10 def self.included(cls)
11 class << cls
12 def declare(*readers)
13 readers.each do |name|
f97609a Made declare properly be instance-specific so amiller can use it in
Larry Diehl authored
14 class_eval <<-RUBY
15 def #{name}
bf47662 Performance optimizations, and got rid of a small race condition in
Larry Diehl authored
16 return @__dataflow_#{name}__ if defined? @__dataflow_#{name}__
17 Variable::LOCK.synchronize { @__dataflow_#{name}__ ||= Variable.new }
f97609a Made declare properly be instance-specific so amiller can use it in
Larry Diehl authored
18 end
19 RUBY
c106962 Added a class-level "declare" method to create object-specific
Larry Diehl authored
20 end
21 end
22 end
23 end
24
2a9790f Refactored to use real proxy variables
Larry Diehl authored
25 def local(&block)
cc2d78e Made #local accept no block to return an anonymous variable
Larry Diehl authored
26 return Variable.new unless block_given?
2a9790f Refactored to use real proxy variables
Larry Diehl authored
27 vars = Array.new(block.arity) { Variable.new }
28 block.call *vars
29 end
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
30
2a9790f Refactored to use real proxy variables
Larry Diehl authored
31 def unify(variable, value)
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
32 variable.__unify__ value
2a9790f Refactored to use real proxy variables
Larry Diehl authored
33 end
69cab66 Added basic hash-like functionality
Larry Diehl authored
34
3c57d00 Added by_need triggers for dataflow-powered lazy evaluation
Larry Diehl authored
35 def by_need(&block)
36 Variable.new &block
37 end
38
c07becd Added barrier
Larry Diehl authored
39 def barrier(*variables)
40 variables.each{|v| v.__wait__ }
41 end
42
bbd08ad Added flow
Larry Diehl authored
43 def flow(output=nil, &block)
5199701 Support custom forkers
Larry Diehl authored
44 Dataflow.forker.call do
bbd08ad Added flow
Larry Diehl authored
45 result = block.call
46 unify output, result if output
47 end
48 end
49
1c946c3 Added need_later expressions that return futures
Larry Diehl authored
50 def need_later(&block)
51 local do |future|
f4ce4f2 Made use of flow's output variable capability in the definition of need_...
Larry Diehl authored
52 flow(future) { block.call }
1c946c3 Added need_later expressions that return futures
Larry Diehl authored
53 future
54 end
55 end
56
6d38ed8 Added support for using Dataflow class/module methods
Larry Diehl authored
57 extend self
58
bf47662 Performance optimizations, and got rid of a small race condition in
Larry Diehl authored
59 # Note that this class uses instance variables directly rather than nicely
b6c5d62 Lazy-load binding conditions for memory reasons.
Larry Diehl authored
60 # initialized instance variables in get/set methods for memory and
61 # performance reasons
2a9790f Refactored to use real proxy variables
Larry Diehl authored
62 class Variable
7e1ab9f Leave __send__ and __id__ alone when acting as a proxy
Larry Diehl authored
63 instance_methods.each { |m| undef_method m unless m =~ /^__/ }
ca59c52 Use monitors for handling locking and waiting
Larry Diehl authored
64 LOCK = Monitor.new
3c57d00 Added by_need triggers for dataflow-powered lazy evaluation
Larry Diehl authored
65 def initialize(&block) @__trigger__ = block if block_given? end
66
b6c5d62 Lazy-load binding conditions for memory reasons.
Larry Diehl authored
67 # Lazy-load conditions to be nice on memory usage
68 def __binding_condition__() @__binding_condition__ ||= LOCK.new_cond end
1034c54 Made equality work transparently, making dataflow variables more
Larry Diehl authored
69
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
70 def __unify__(value)
ca59c52 Use monitors for handling locking and waiting
Larry Diehl authored
71 LOCK.synchronize do
fa6893f Specs for proper lazy behavior with regard to unification/binding
Larry Diehl authored
72 __activate_trigger__ if @__trigger__
b6c5d62 Lazy-load binding conditions for memory reasons.
Larry Diehl authored
73 if @__bound__
fcbd2f3 Nested binding through variables possible
Larry Diehl authored
74 return @__value__.__unify__(value) if @__value__.__dataflow__? rescue nil
fb9cdc0 More helpful debugging info
Larry Diehl authored
75 raise UnificationError, "#{@__value__.inspect} != #{value.inspect}" if self != value
ca59c52 Use monitors for handling locking and waiting
Larry Diehl authored
76 else
77 @__value__ = value
b6c5d62 Lazy-load binding conditions for memory reasons.
Larry Diehl authored
78 @__bound__ = true
79 __binding_condition__.broadcast # wakeup all method callers
9584a40 Added equality spec exposing failure with the way 2 proxy objects are co...
Larry Diehl authored
80 @__binding_condition__ = nil # GC
916059f Added locking to work with real concurrency in JRuby in addition to
Larry Diehl authored
81 end
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
82 end
ca59c52 Use monitors for handling locking and waiting
Larry Diehl authored
83 @__value__
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
84 end
916059f Added locking to work with real concurrency in JRuby in addition to
Larry Diehl authored
85
3c57d00 Added by_need triggers for dataflow-powered lazy evaluation
Larry Diehl authored
86 def __activate_trigger__
87 @__value__ = @__trigger__.call
88 @__bound__ = true
9584a40 Added equality spec exposing failure with the way 2 proxy objects are co...
Larry Diehl authored
89 @__trigger__ = nil # GC
3c57d00 Added by_need triggers for dataflow-powered lazy evaluation
Larry Diehl authored
90 end
91
c07becd Added barrier
Larry Diehl authored
92 def __wait__
ca59c52 Use monitors for handling locking and waiting
Larry Diehl authored
93 LOCK.synchronize do
c07becd Added barrier
Larry Diehl authored
94 unless @__bound__
3c57d00 Added by_need triggers for dataflow-powered lazy evaluation
Larry Diehl authored
95 if @__trigger__
96 __activate_trigger__
97 else
98 __binding_condition__.wait
99 end
bf47662 Performance optimizations, and got rid of a small race condition in
Larry Diehl authored
100 end
101 end unless @__bound__
c07becd Added barrier
Larry Diehl authored
102 end
103
104 def method_missing(name, *args, &block)
105 return "#<Dataflow::Variable:#{__id__} unbound>" if !@__bound__ && name == :inspect
106 __wait__
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
107 @__value__.__send__(name, *args, &block)
69cab66 Added basic hash-like functionality
Larry Diehl authored
108 end
fcbd2f3 Nested binding through variables possible
Larry Diehl authored
109
110 def __dataflow__?
111 true
112 end
69cab66 Added basic hash-like functionality
Larry Diehl authored
113 end
2a9790f Refactored to use real proxy variables
Larry Diehl authored
114
115 UnificationError = Class.new StandardError
69cab66 Added basic hash-like functionality
Larry Diehl authored
116 end
fa4441d Merged amiller's changes.
Larry Diehl authored
117
c856552 @larrytheliquid map_later, map_barrier, & map_needed
authored
118 require "#{File.dirname(__FILE__)}/dataflow/enumerable"
2f218cc Made equality changes an optional separate:
Larry Diehl authored
119 require "#{File.dirname(__FILE__)}/dataflow/port"
120 require "#{File.dirname(__FILE__)}/dataflow/actor"
2eede19 Added FutureQueue
Larry Diehl authored
121 require "#{File.dirname(__FILE__)}/dataflow/future_queue"
Something went wrong with that request. Please try again.