Class: Pwrake::TaskQueue
- Inherits:
-
Object
- Object
- Pwrake::TaskQueue
- Defined in:
- lib/pwrake/task_queue.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#enable_steal ⇒ Object
Returns the value of attribute enable_steal.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
Instance Method Summary collapse
- #after_check(tasks) ⇒ Object
- #clear ⇒ Object
-
#deq(hint = nil) ⇒ Object
deq.
- #deq_impl(hint, n) ⇒ Object
- #empty? ⇒ Boolean
-
#enq(item) ⇒ Object
enq.
- #enq_body(item) ⇒ Object
- #enq_impl(item) ⇒ Object
- #finish ⇒ Object
- #halt ⇒ Object
- #init_queue(*args) ⇒ Object
-
#initialize(*args) ⇒ TaskQueue
constructor
A new instance of TaskQueue.
- #reserve(item) ⇒ Object
- #resume ⇒ Object
- #stop ⇒ Object
- #synchronize(condition) ⇒ Object
- #thread_end(th) ⇒ Object
Constructor Details
#initialize(*args) ⇒ TaskQueue
Returns a new instance of TaskQueue.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pwrake/task_queue.rb', line 66 def initialize(*args) @finished = false @halt = false @mutex = Mutex.new @th_end = [] @enable_steal = true @reservation = {} @reserved_q = {} case Pwrake.application.['QUEUE_PRIORITY']||"DFS" when /dfs/i @array_class = PriorityQueueArray when /fifo/i @array_class = Array # Fifo when /lifo/i @array_class = LifoQueueArray else raise RuntimeError,"unknown option for QUEUE_PRIORITY" end Log.debug "--- TQ#initialize @array_class=#{@array_class.inspect}" init_queue(*args) end |
Instance Attribute Details
#enable_steal ⇒ Object
Returns the value of attribute enable_steal.
95 96 97 |
# File 'lib/pwrake/task_queue.rb', line 95 def enable_steal @enable_steal end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
94 95 96 |
# File 'lib/pwrake/task_queue.rb', line 94 def mutex @mutex end |
Instance Method Details
#after_check(tasks) ⇒ Object
248 249 250 |
# File 'lib/pwrake/task_queue.rb', line 248 def after_check(tasks) # implimented at subclass end |
#clear ⇒ Object
220 221 222 223 224 |
# File 'lib/pwrake/task_queue.rb', line 220 def clear @q_prior.clear @q_later.clear @reserved_q.clear end |
#deq(hint = nil) ⇒ Object
deq
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/pwrake/task_queue.rb', line 173 def deq(hint=nil) n = 0 loop do @mutex.synchronize do t0 = Time.now if @th_end.first == Thread.current @th_end.shift return false elsif @halt Log.debug "--- halt in TQ#deq @q=#{@q.inspect}" @cv.wait(@mutex) n = 0 elsif item = @reserved_q.delete(Thread.current) Log.debug "--- deq from reserved_q=#{item.inspect}" return item elsif empty? # no item in queue #Log.debug "--- empty=true in #{self.class}#deq @finished=#{@finished.inspect}" if @finished @cv.signal return false end #Log.debug "--- waiting in #{self.class}#deq @finished=#{@finished.inspect}" @cv.wait(@mutex) n = 0 else if t = deq_impl(hint,n) t_inspect = t.inspect[0..1000] Log.debug "--- TQ#deq #{t_inspect} deq_time=#{Time.now-t0}" return t end #@cv.signal([hint]) n += 1 end end #Thread.pass end end |
#deq_impl(hint, n) ⇒ Object
215 216 217 218 |
# File 'lib/pwrake/task_queue.rb', line 215 def deq_impl(hint,n) Log.debug "--- TQ#deq_impl #{@q.inspect}" @q_prior.shift || @q_later.shift end |
#empty? ⇒ Boolean
226 227 228 |
# File 'lib/pwrake/task_queue.rb', line 226 def empty? @q_prior.empty? && @q_later.empty? && @reserved_q.empty? end |
#enq(item) ⇒ Object
enq
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/pwrake/task_queue.rb', line 137 def enq(item) Log.debug "--- TQ#enq #{item.name}" t0 = Time.now if @halt enq_body(item) else @mutex.synchronize do enq_body(item) @cv.signal(item.suggest_location) end end @reserved_q.keys.each{|th| Log.debug "--- run #{th}"; th.run } Log.debug "--- TQ#enq #{item.name} enq_time=#{Time.now-t0}" end |
#enq_body(item) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/pwrake/task_queue.rb', line 155 def enq_body(item) if th = @reservation[item] @reserved_q[th] = item else enq_impl(item) end end |
#enq_impl(item) ⇒ Object
163 164 165 166 167 168 169 |
# File 'lib/pwrake/task_queue.rb', line 163 def enq_impl(item) if item.prior? @q_prior.push(item) else @q_later.push(item) end end |
#finish ⇒ Object
230 231 232 233 234 |
# File 'lib/pwrake/task_queue.rb', line 230 def finish Log.debug "--- TQ#finish" @finished = true @cv.broadcast end |
#halt ⇒ Object
101 102 103 104 105 |
# File 'lib/pwrake/task_queue.rb', line 101 def halt @mutex.synchronize do @halt = true end end |
#init_queue(*args) ⇒ Object
88 89 90 91 92 |
# File 'lib/pwrake/task_queue.rb', line 88 def init_queue(*args) @cv = TaskConditionVariable.new @q_prior = @array_class.new @q_later = Array.new end |
#reserve(item) ⇒ Object
97 98 99 |
# File 'lib/pwrake/task_queue.rb', line 97 def reserve(item) @reservation[item] = Thread.current end |
#resume ⇒ Object
107 108 109 110 111 112 |
# File 'lib/pwrake/task_queue.rb', line 107 def resume @mutex.synchronize do @halt = false @cv.broadcast end end |
#stop ⇒ Object
236 237 238 239 240 241 |
# File 'lib/pwrake/task_queue.rb', line 236 def stop @mutex.synchronize do clear finish end end |
#synchronize(condition) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/pwrake/task_queue.rb', line 114 def synchronize(condition) ret = nil if condition @mutex.lock @halt = true begin ret = yield @cv.broadcast ensure @halt = false @mutex.unlock end else ret = yield end @reserved_q.keys.each do |th| Log.debug "--- run #{th}"; th.run end ret end |
#thread_end(th) ⇒ Object
243 244 245 246 |
# File 'lib/pwrake/task_queue.rb', line 243 def thread_end(th) @th_end.push(th) @cv.broadcast end |