Sha256: 95c9b220807a0e147a508c79de34cc8d80ea9cc488ddb9868b9c52b71634af65

Contents?: true

Size: 1.55 KB

Versions: 12

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Libuv
    class Signal < Handle


        define_callback function: :on_sig, params: [:pointer, :int]


        SIGNALS = {
            :HUP => 1,
            :SIGHUP => 1,
            :INT => 2,
            :SIGINT => 2,
            :TERM => 15,
            :SIGTERM => 15,
            :BREAK => 21,
            :SIGBREAK => 21,
            :WINCH => 28,
            :SIGWINCH => 28
        }


        # @param reactor [::Libuv::Reactor] reactor this signal handler will be associated
        # @param callback [Proc] callback to be called when the signal is triggered
        def initialize(reactor)
            @reactor = reactor

            signal_ptr = ::Libuv::Ext.allocate_handle_signal
            error = check_result(::Libuv::Ext.signal_init(reactor.handle, signal_ptr))

            super(signal_ptr, error)
        end

        # Enables the signal handler.
        def start(signal)
            return if @closed
            signal = SIGNALS[signal] if signal.is_a? Symbol
            error = check_result ::Libuv::Ext.signal_start(handle, callback(:on_sig), signal)
            reject(error) if error
            self
        end

        # Disables the signal handler.
        def stop
            return if @closed
            error = check_result ::Libuv::Ext.signal_stop(handle)
            reject(error) if error
            self
        end


        private


        def on_sig(handle, signal)
            ::Fiber.new {
                defer.notify(signal)   # notify of a call
            }.resume
        end
    end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
libuv-3.1.9 lib/libuv/signal.rb
libuv-3.1.8 lib/libuv/signal.rb
libuv-3.1.7 lib/libuv/signal.rb
libuv-3.1.6 lib/libuv/signal.rb
libuv-3.1.5 lib/libuv/signal.rb
libuv-3.1.4 lib/libuv/signal.rb
libuv-3.1.3 lib/libuv/signal.rb
libuv-3.1.2 lib/libuv/signal.rb
libuv-3.1.1 lib/libuv/signal.rb
libuv-3.1.0 lib/libuv/signal.rb
libuv-3.0.1 lib/libuv/signal.rb
libuv-3.0.0 lib/libuv/signal.rb