Class: Higgs::SharedWork
- Inherits:
-
Object
- Object
- Higgs::SharedWork
- Defined in:
- lib/higgs/thread.rb
Constant Summary collapse
- CVS_ID =
for ident(1)
'$Id: thread.rb 841 2008-12-24 09:23:20Z toki $'
Instance Method Summary collapse
-
#initialize(&work) ⇒ SharedWork
constructor
A new instance of SharedWork.
- #result ⇒ Object
- #result=(value) ⇒ Object
Constructor Details
#initialize(&work) ⇒ SharedWork
Returns a new instance of SharedWork.
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/higgs/thread.rb', line 197 def initialize(&work) unless (work) then raise ArgumentError, 'required work block' end @work = work @lock = Mutex.new @cond = ConditionVariable.new @state = :init @abort = nil @error = nil @result = nil end |
Instance Method Details
#result ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/higgs/thread.rb', line 225 def result @lock.synchronize{ case (@state) when :done return __result__ when :init @state = :working # fall through when :working until (@state == :done) @cond.wait(@lock) end return __result__ else raise 'internal error' end } completed = false begin r = @result = @work.call completed = true ensure @lock.synchronize{ @state = :done unless (completed) then @abort = true @error = $! end @cond.broadcast } end r end |
#result=(value) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/higgs/thread.rb', line 260 def result=(value) @lock.synchronize{ case (@state) when :init @state = :done when :working until (@state == :done) @cond.wait(@lock) end when :done # nothing to do. else raise 'internal error' end if (@abort) then raise RuntimeError, abort_msg end @result = value } end |