Class: Threadesque::SafeYielder

Inherits:
Object
  • Object
show all
Defined in:
lib/quack_concurrency/yielder.rb

Class Method Summary collapse

Instance Method Summary collapse

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_currentObject



6
7
8
# File 'lib/quack_concurrency/yielder.rb', line 6

def self.for_current
  new(Thread.current)
end

Instance Method Details

#resumeObject



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

#yieldObject



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