Module: Libuv

Extended by:
Accessors
Defined in:
lib/libuv.rb,
lib/libuv/q.rb,
lib/libuv/dns.rb,
lib/libuv/tcp.rb,
lib/libuv/tty.rb,
lib/libuv/udp.rb,
lib/libuv/file.rb,
lib/libuv/idle.rb,
lib/libuv/pipe.rb,
lib/libuv/work.rb,
lib/libuv/async.rb,
lib/libuv/check.rb,
lib/libuv/error.rb,
lib/libuv/spawn.rb,
lib/libuv/timer.rb,
lib/libuv/handle.rb,
lib/libuv/signal.rb,
lib/libuv/ext/ext.rb,
lib/libuv/prepare.rb,
lib/libuv/reactor.rb,
lib/libuv/version.rb,
lib/libuv/fs_event.rb,
lib/libuv/ext/types.rb,
lib/libuv/coroutines.rb,
lib/libuv/fiber_pool.rb,
lib/libuv/filesystem.rb,
lib/libuv/mixins/net.rb,
lib/libuv/mixins/stream.rb,
lib/libuv/mixins/listener.rb,
lib/libuv/mixins/resource.rb,
lib/libuv/mixins/accessors.rb,
lib/libuv/mixins/fs_checks.rb,
lib/libuv/ext/platform/unix.rb,
lib/libuv/mixins/assertions.rb,
lib/libuv/ext/platform/windows.rb,
lib/libuv/ext/platform/darwin_x64.rb

Defined Under Namespace

Modules: Accessors, Assertions, Ext, FsChecks, Listener, Net, Q, Resource, Stream Classes: Async, Check, Dns, Error, FSEvent, FiberPool, File, Filesystem, Handle, Idle, Pipe, Prepare, Reactor, Signal, Spawn, TCP, TTY, Timer, UDP, Work

Constant Summary collapse

DefaultThread =
Thread.current
VERSION =
'4.0.9'

Constants included from Accessors

Accessors::Functions

Class Method Summary collapse

Methods included from Accessors

reactor

Class Method Details

.co(*yieldable) ⇒ Object

Takes a Promise response and turns it into a co-routine for code execution without using callbacks

Parameters:

  • *promises (::Libuv::Q::Promise)

    a number of promises that will be combined into a single promise

Returns:

  • (Object)

    Returns the result of a single promise or an array of results if provided multiple promises

Raises:

  • (Exception)

    if the promise is rejected



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/libuv/coroutines.rb', line 16

def co(*yieldable)
    on_reactor = Libuv::Reactor.current
    raise 'must be running on a reactor thread to use coroutines' unless on_reactor

    f = Fiber.current
    wasError = false

    # Convert the input into a promise on the current reactor
    if yieldable.length == 1
        promise = yieldable[0]
        # Passed independently as this is often overwritten for performance
        promise.progress &Proc.new if block_given?
    else
        promise = on_reactor.all(*yieldable)
    end

    # Use the promise to resume the Fiber
    promise.then(proc { |res|
        if Libuv::Reactor.current == on_reactor
            f.resume res
        else
            on_reactor.schedule { f.resume(res) }
        end
    }, proc { |err|
        wasError = true
        if Libuv::Reactor.current == on_reactor
            f.resume err
        else
            on_reactor.schedule { f.resume(err) }
        end
    })

    # We want to prevent the reactor from stopping while we are waiting on a response
    on_reactor.ref
    result = Fiber.yield # Assign the result from the resume
    on_reactor.unref

    # Either return the result or raise an error
    if wasError
        if result.is_a?(Exception)
            backtrace = caller
            backtrace.shift
            if result.respond_to?(:backtrace) && result.backtrace
                backtrace << '---- continuation ----'
                backtrace.concat(result.backtrace)
            end
            result.set_backtrace(backtrace)
            raise result
        else
            e = case result
            when String, Symbol
                CoroutineRejection.new(result.to_s)
            else
                CoroutineRejection.new
            end
            e.value = result
            raise e
        end
    end
    result
end

.cpu_countInteger?

Returns the number of CPU cores on the host platform

Returns:

  • (Integer, nil)

    representing the number of CPU cores or nil if failed



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/libuv.rb', line 53

def self.cpu_count
    cpu_info = FFI::MemoryPointer.new(:pointer)
    cpu_count = FFI::MemoryPointer.new(:int)
    if ::Libuv::Ext.cpu_info(cpu_info, cpu_count) >= 0
        count = cpu_count.read_int
        ::Libuv::Ext.free_cpu_info(cpu_info.read_pointer, count)
        return count
    else
        return nil
    end
end