Class: FiberRecycling::NormalFiberBackend

Inherits:
FiberBackend show all
Defined in:
lib/fiber_recycling/normal_fiber_backend.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fiber, block) ⇒ NormalFiberBackend

Returns a new instance of NormalFiberBackend.



17
18
19
20
21
22
23
24
# File 'lib/fiber_recycling/normal_fiber_backend.rb', line 17

def initialize(fiber, block)
  @state = 'created'
  @variables = {}
  @recycled_fiber_borrower =  RecycledFiberPool.local.borrower
  @recycled_fiber = @recycled_fiber_borrower.retrieve
  @recycled_fiber.run { execute(fiber, block) }
  ObjectSpace.define_finalizer(self, self.class.finalizer(@recycled_fiber_borrower))
end

Instance Attribute Details

#variablesObject (readonly)

Returns the value of attribute variables.



15
16
17
# File 'lib/fiber_recycling/normal_fiber_backend.rb', line 15

def variables
  @variables
end

Class Method Details

.finalizer(recycled_fiber_borrower) ⇒ Object



4
5
6
# File 'lib/fiber_recycling/normal_fiber_backend.rb', line 4

def self.finalizer(recycled_fiber_borrower)
  Proc.new { recycled_fiber_borrower.return }
end

.yield(*args) ⇒ Object



8
9
10
11
12
13
# File 'lib/fiber_recycling/normal_fiber_backend.rb', line 8

def self.yield(*args)
  @state = 'suspended'
  return_value = RecycledFiber.yield(*args)
  @state = 'resumed'
  return_value
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/fiber_recycling/normal_fiber_backend.rb', line 26

def alive?
  @state != 'terminated'
end

#inspectObject



30
31
32
# File 'lib/fiber_recycling/normal_fiber_backend.rb', line 30

def inspect
  to_s
end

#resume(*args) ⇒ Object

Raises:



34
35
36
37
# File 'lib/fiber_recycling/normal_fiber_backend.rb', line 34

def resume(*args)
  raise FiberError, 'dead fiber called' unless alive?
  @recycled_fiber.resume(*args)
end

#to_sObject



39
40
41
# File 'lib/fiber_recycling/normal_fiber_backend.rb', line 39

def to_s
  "#<RecycledFiber::Fiber:#{object_hexid} (#{@state})>"
end

#transfer(*args) ⇒ Object

Raises:



43
44
45
46
# File 'lib/fiber_recycling/normal_fiber_backend.rb', line 43

def transfer(*args)
  raise FiberError, 'dead fiber called' unless alive?
  @recycled_fiber.transfer(*args)
end