Sha256: c7dbf78379b0107ac38be266b50f9ffa46aab1bbe893d17a20e3f72091a3548d

Contents?: true

Size: 955 Bytes

Versions: 7

Compression:

Stored size: 955 Bytes

Contents

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

module Rev
  # 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
      @reader.read 1
      on_signal
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rev-0.3.1 lib/rev/async_watcher.rb
rev-0.3.0 lib/rev/async_watcher.rb
rev-0.2.2 lib/rev/async_watcher.rb
rev-0.2.0 lib/rev/async_watcher.rb
rev-0.2.4 lib/rev/async_watcher.rb
rev-0.2.3 lib/rev/async_watcher.rb
rev-0.2.1 lib/rev/async_watcher.rb