Class: Threadesque::SafeYielder
- Inherits:
-
Object
- Object
- Threadesque::SafeYielder
- Defined in:
- lib/quack_concurrency/yielder.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(thread) ⇒ SafeYielder
constructor
A new instance of SafeYielder.
- #resume ⇒ Object
- #yield ⇒ Object
Constructor Details
#initialize(thread) ⇒ SafeYielder
Returns a new instance of SafeYielder.
10 11 12 13 14 |
# File 'lib/quack_concurrency/yielder.rb', line 10 def initialize(thread) raise 'not ready yet' @thread = thread @state = :running end |
Class Method Details
.for_current ⇒ Object
6 7 8 |
# File 'lib/quack_concurrency/yielder.rb', line 6 def self.for_current new(Thread.current) end |
Instance Method Details
#resume ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/quack_concurrency/yielder.rb', line 16 def resume raise 'Thread is not sleeping' unless @state == :sleeping :wait until @thread.status == 'sleep' @state = :running @thread.run nil end |
#yield ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/quack_concurrency/yielder.rb', line 24 def yield raise 'can only stop current Thread' unless Thread.current == @thread @state = :sleeping loop do Thread.stop redo if @state == :sleeping end nil end |