Class: Libuv::FiberPool

Inherits:
Object show all
Defined in:
lib/libuv/fiber_pool.rb

Overview

Use of a Fiber Pool increases performance as stack allocations don’t need to continually occur. Especially useful on JRuby and Rubinius where multiple kernel threads and locks emulate Fibers.

Instance Method Summary collapse

Constructor Details

#initialize(thread) ⇒ FiberPool

Returns a new instance of FiberPool.



9
10
11
12
13
# File 'lib/libuv/fiber_pool.rb', line 9

def initialize(thread)
    @reactor = thread
    @pool = []
    @count = 0
end

Instance Method Details

#availableObject



39
40
41
# File 'lib/libuv/fiber_pool.rb', line 39

def available
    @pool.size
end

#execObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/libuv/fiber_pool.rb', line 15

def exec
    if @reactor.reactor_thread?
        # Execute the block in a Fiber
        next_fiber do
            begin
                yield
            rescue Exception => e
                @on_error.call(e) if @on_error
            end
        end
    else
        # move the block onto the reactor thread
        @reactor.schedule do
            exec do
                yield
            end
        end
    end
end

#on_error(&block) ⇒ Object



35
36
37
# File 'lib/libuv/fiber_pool.rb', line 35

def on_error(&block)
    @on_error = block
end

#sizeObject



43
44
45
# File 'lib/libuv/fiber_pool.rb', line 43

def size
    @count
end