Sha256: 39e012f245319325bfea44aa4f603c40da273294532092a199c81ae406eff4b5

Contents?: true

Size: 1.66 KB

Versions: 8

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Libuv; end

# 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.
class Libuv::FiberPool
    def initialize(thread)
        @reactor = thread
        reset
    end

    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

    def on_error(&block)
        @on_error = block
    end

    def available
        @pool.size
    end

    def size
        @count
    end

    def reset
        @pool = []
        @count = 0
    end


    protected


    def next_fiber(&block)
        fib = if @pool.empty?
            new_fiber
        else
            @pool.pop
        end

        @job = block
        fib.resume
    end

    # Fibers are never cleaned up which shouldn't be much of an issue
    # This might lead to issues on Rubinius or JRuby however it should
    # generally improve performance on these platforms
    def new_fiber
        @count += 1

        Fiber.new do
            loop do
                job = @job
                @job = nil
                job.call

                @pool << Fiber.current
                Fiber.yield
            end
        end
    end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
libuv-4.0.9 lib/libuv/fiber_pool.rb
libuv-4.0.2 lib/libuv/fiber_pool.rb
libuv-4.0.1 lib/libuv/fiber_pool.rb
libuv-4.0.0 lib/libuv/fiber_pool.rb
libuv-3.3.0 lib/libuv/fiber_pool.rb
libuv-3.2.4 lib/libuv/fiber_pool.rb
libuv-3.2.3 lib/libuv/fiber_pool.rb
libuv-3.2.2 lib/libuv/fiber_pool.rb