Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 93 lines (79 sloc) 2.323 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
2f218cc Made equality changes an optional separate:
Larry Diehl authored
4 VERSION = "0.2.0"
23d7bef Added a Rakefile for building the gem
Larry Diehl authored
5
c106962 Added a class-level "declare" method to create object-specific
Larry Diehl authored
6 def self.included(cls)
7 class << cls
8 def declare(*readers)
9 readers.each do |name|
f97609a Made declare properly be instance-specific so amiller can use it in
Larry Diehl authored
10 class_eval <<-RUBY
11 def #{name}
bf47662 Performance optimizations, and got rid of a small race condition in
Larry Diehl authored
12 return @__dataflow_#{name}__ if defined? @__dataflow_#{name}__
13 Variable::LOCK.synchronize { @__dataflow_#{name}__ ||= Variable.new }
f97609a Made declare properly be instance-specific so amiller can use it in
Larry Diehl authored
14 end
15 RUBY
c106962 Added a class-level "declare" method to create object-specific
Larry Diehl authored
16 end
17 end
18 end
19 end
20
2a9790f Refactored to use real proxy variables
Larry Diehl authored
21 def local(&block)
22 vars = Array.new(block.arity) { Variable.new }
23 block.call *vars
24 end
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
25
2a9790f Refactored to use real proxy variables
Larry Diehl authored
26 def unify(variable, value)
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
27 variable.__unify__ value
2a9790f Refactored to use real proxy variables
Larry Diehl authored
28 end
69cab66 Added basic hash-like functionality
Larry Diehl authored
29
3c57d00 Added by_need triggers for dataflow-powered lazy evaluation
Larry Diehl authored
30 def by_need(&block)
31 Variable.new &block
32 end
33
1c946c3 Added need_later expressions that return futures
Larry Diehl authored
34 def need_later(&block)
35 local do |future|
36 Thread.new { unify future, block.call }
37 future
38 end
39 end
40
bf47662 Performance optimizations, and got rid of a small race condition in
Larry Diehl authored
41 # Note that this class uses instance variables directly rather than nicely
b6c5d62 Lazy-load binding conditions for memory reasons.
Larry Diehl authored
42 # initialized instance variables in get/set methods for memory and
43 # performance reasons
2a9790f Refactored to use real proxy variables
Larry Diehl authored
44 class Variable
1034c54 Made equality work transparently, making dataflow variables more
Larry Diehl authored
45 instance_methods.each { |m| undef_method m }
ca59c52 Use monitors for handling locking and waiting
Larry Diehl authored
46 LOCK = Monitor.new
3c57d00 Added by_need triggers for dataflow-powered lazy evaluation
Larry Diehl authored
47 def initialize(&block) @__trigger__ = block if block_given? end
48
b6c5d62 Lazy-load binding conditions for memory reasons.
Larry Diehl authored
49 # Lazy-load conditions to be nice on memory usage
50 def __binding_condition__() @__binding_condition__ ||= LOCK.new_cond end
1034c54 Made equality work transparently, making dataflow variables more
Larry Diehl authored
51
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
52 def __unify__(value)
ca59c52 Use monitors for handling locking and waiting
Larry Diehl authored
53 LOCK.synchronize do
fa6893f Specs for proper lazy behavior with regard to unification/binding
Larry Diehl authored
54 __activate_trigger__ if @__trigger__
b6c5d62 Lazy-load binding conditions for memory reasons.
Larry Diehl authored
55 if @__bound__
1034c54 Made equality work transparently, making dataflow variables more
Larry Diehl authored
56 raise UnificationError if self != value
ca59c52 Use monitors for handling locking and waiting
Larry Diehl authored
57 else
58 @__value__ = value
b6c5d62 Lazy-load binding conditions for memory reasons.
Larry Diehl authored
59 @__bound__ = true
60 __binding_condition__.broadcast # wakeup all method callers
9584a40 Added equality spec exposing failure with the way 2 proxy objects are…
Larry Diehl authored
61 @__binding_condition__ = nil # GC
916059f Added locking to work with real concurrency in JRuby in addition to
Larry Diehl authored
62 end
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
63 end
ca59c52 Use monitors for handling locking and waiting
Larry Diehl authored
64 @__value__
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
65 end
916059f Added locking to work with real concurrency in JRuby in addition to
Larry Diehl authored
66
3c57d00 Added by_need triggers for dataflow-powered lazy evaluation
Larry Diehl authored
67 def __activate_trigger__
68 @__value__ = @__trigger__.call
69 @__bound__ = true
9584a40 Added equality spec exposing failure with the way 2 proxy objects are…
Larry Diehl authored
70 @__trigger__ = nil # GC
3c57d00 Added by_need triggers for dataflow-powered lazy evaluation
Larry Diehl authored
71 end
72
2a9790f Refactored to use real proxy variables
Larry Diehl authored
73 def method_missing(name, *args, &block)
ca59c52 Use monitors for handling locking and waiting
Larry Diehl authored
74 LOCK.synchronize do
3c57d00 Added by_need triggers for dataflow-powered lazy evaluation
Larry Diehl authored
75 unless @__bound__
1b2ed0e Added support for #inspect'ing unbound dataflow variables in order to
Larry Diehl authored
76 return '#<Dataflow::Variable unbound>' if name == :inspect
3c57d00 Added by_need triggers for dataflow-powered lazy evaluation
Larry Diehl authored
77 if @__trigger__
78 __activate_trigger__
79 else
80 __binding_condition__.wait
81 end
bf47662 Performance optimizations, and got rid of a small race condition in
Larry Diehl authored
82 end
83 end unless @__bound__
3084b36 Got first dataflow concurrency example working
Larry Diehl authored
84 @__value__.__send__(name, *args, &block)
69cab66 Added basic hash-like functionality
Larry Diehl authored
85 end
86 end
2a9790f Refactored to use real proxy variables
Larry Diehl authored
87
88 UnificationError = Class.new StandardError
69cab66 Added basic hash-like functionality
Larry Diehl authored
89 end
fa4441d Merged amiller's changes.
Larry Diehl authored
90
2f218cc Made equality changes an optional separate:
Larry Diehl authored
91 require "#{File.dirname(__FILE__)}/dataflow/port"
92 require "#{File.dirname(__FILE__)}/dataflow/actor"
Something went wrong with that request. Please try again.