Class: Thespian::Strategy::Fiber

Inherits:
Object
  • Object
show all
Includes:
Interface
Defined in:
lib/thespian/strategies/fiber.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Interface

#salvage_mailbox

Constructor Details

#initialize(&block) ⇒ Fiber

Returns a new instance of Fiber.



15
16
17
18
19
# File 'lib/thespian/strategies/fiber.rb', line 15

def initialize(&block)
  @block        = block
  @mailbox      = []
  @mailbox_cond = Strand::ConditionVariable.new
end

Instance Method Details

#<<(message) ⇒ Object



31
32
33
34
35
# File 'lib/thespian/strategies/fiber.rb', line 31

def <<(message)
  @mailbox << message
  @mailbox_cond.signal
  self
end

#mailbox_sizeObject



37
38
39
# File 'lib/thespian/strategies/fiber.rb', line 37

def mailbox_size
  @mailbox.size
end

#messagesObject



41
42
43
# File 'lib/thespian/strategies/fiber.rb', line 41

def messages
  @mailbox.dup
end

#receiveObject



26
27
28
29
# File 'lib/thespian/strategies/fiber.rb', line 26

def receive
  @mailbox_cond.wait while @mailbox.empty?
  @mailbox.shift
end

#startObject



21
22
23
24
# File 'lib/thespian/strategies/fiber.rb', line 21

def start
  @strand = Strand.new{ @block.call }
  self
end

#stopObject



45
46
47
48
# File 'lib/thespian/strategies/fiber.rb', line 45

def stop
  self << Stop.new
  @strand.join
end