Sha256: 72e5c1152c364bfc1ac3a9da1623d71cad86aaf96cf663349032a4f6f5681a6e

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

#--
# Copyright (C)2007-10 Tony Arcieri
# You can redistribute this under the terms of the Ruby license
# See file LICENSE for details
#++

module Coolio
  # The AsyncWatcher lets you signal another thread to wake up.  Its
  # intended use is notifying another thread of events.
  class AsyncWatcher < IOWatcher
    def initialize
      @reader, @writer = ::IO.pipe
      super(@reader)
    end

    # Signal the async watcher.  This call is thread safe.
    def signal
      # Write a byte to the pipe.  What we write is meaningless, it
      # merely signals an event has occurred for each byte written.
      @writer.write "\0"
    end
    
    # Called whenever a signal is received
    def on_signal; end
    event_callback :on_signal

    #########
    protected
    #########

    def on_readable
      # Read a byte from the pipe.  This clears readability, unless
      # another signal is pending
      begin
        @reader.read_nonblock 1
      rescue Errno::EAGAIN
        # in case there are spurious wakeups from forked processs
        return
      end
      on_signal
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cool.io-1.0.0 lib/cool.io/async_watcher.rb
cool.io-0.9.0 lib/cool.io/async_watcher.rb