forked from ruby/rake
/
node.rb
165 lines (148 loc) · 3.59 KB
/
node.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require 'comp_tree/quix/diagnostic'
require 'thread'
module CompTree
#
# Base class for nodes in the computation tree.
#
class Node
include Quix::Diagnostic #:nodoc:
attr_reader :name #:nodoc:
attr_accessor :parents #:nodoc:
attr_accessor :children #:nodoc:
attr_accessor :function #:nodoc:
attr_accessor :result #:nodoc:
attr_accessor :shared_lock #:nodoc:
#
# Create a node
#
def initialize(name) #:nodoc:
@name = name
@mutex = Mutex.new
@children = []
@parents = []
reset_self
end
#
# Reset the computation for this node.
#
def reset_self #:nodoc:
@shared_lock = 0
@children_results = nil
@result = nil
end
#
# Reset the computation for this node and all children.
#
def reset #:nodoc:
each_downward { |node|
node.reset_self
}
end
def each_downward(&block) #:nodoc:
block.call(self)
@children.each { |child|
child.each_downward(&block)
}
end
def each_upward(&block) #:nodoc:
block.call(self)
@parents.each { |parent|
parent.each_upward(&block)
}
end
def each_child #:nodoc:
@children.each { |child|
yield(child)
}
end
#
# Force computation of all children; intended for
# single-threaded mode.
#
def compute_now #:nodoc:
unless @children_results
@children_results = @children.map { |child|
child.compute_now
}
end
compute
end
#
# If all children have been computed, return their results;
# otherwise return nil.
#
def children_results #:nodoc:
if @children_results
@children_results
else
results = @children.map { |child|
if child_result = child.result
child_result
else
return nil
end
}
@children_results = results
end
end
def trace_compute #:nodoc:
debug {
# --- own mutex
trace "Computing #{@name}"
raise Error::AssertionFailed if @result
raise Error::AssertionFailed unless @mutex.locked?
raise Error::AssertionFailed unless @children_results
}
end
#
# Compute this node; children must be computed and lock must be
# already acquired.
#
def compute #:nodoc:
unless defined?(@function) and @function
raise Error::NoFunctionError,
"No function was defined for node '#{@name.inspect}'"
end
@function.call(*@children_results)
end
def try_lock #:nodoc:
# --- shared tree mutex and own mutex
if @shared_lock == 0 and @mutex.try_lock
trace "Locking #{@name}"
each_upward { |node|
node.shared_lock += 1
trace "#{node.name} locked by #{@name}: level: #{node.shared_lock}"
}
true
else
false
end
end
def unlock #:nodoc:
# --- shared tree mutex and own mutex
debug {
raise Error::AssertionFailed unless @mutex.locked?
trace "Unlocking #{@name}"
}
each_upward { |node|
node.shared_lock -= 1
debug {
if node.shared_lock == 0
trace "#{node.name} unlocked by #{@name}"
end
}
}
@mutex.unlock
end
class << self
#
# Throw away the computation result?
#
# This Node base class always returns false.
#
def discard_result?
false
end
end
end
end