Giter Site home page Giter Site logo

kqueue's Introduction

kqueue

Build Status

A binding of kqueue on Ruby.

kqueue can use BSD system only. (because must be installed sys/event.h)

Usage

require 'kqueue'

# Inheritance
# class Kqueue < IO
#   # ident: file descriptor identifier for this event
#   # filter: filter for event(just one)
#   # flags: general flags(complex fragment)
#   # fflags: filter-specific flags(complex fragment)
#   # data: filter-specific data
#   # udata: opaque user data(can set any object)
#   class Event < Struct.new(:ident, :filter, :flags, :fflags, :data, :udata)
#     # Event::Constants include all kqueue constants
#     include Event::Constants
#   end
# end

# create kqueue object and ensure close
Kqueue.open do |kq|
  # ...
end

# or
begin
  kq = Kqueue.new
  # ...
ensure
  kq.close
end

# watch event
IO.pipe do |r, w|
  Kqueue.open do |kq|
    kev = Kqueue::Event.new(r.fileno, EVFILT_READ, EV_ADD, 0, 0, proc {
      puts "you can read #{r.fileno} with non block!"
    })
    # set a event
    kq.kevent([kev], 0)

    # wait a event
    # wait until event occur
    kq.kevent(nil, 1)

    # wait 0.1 sec
    kq.kevent(nil, 1, 0.1) #=> []

    w.write 'ok go'
    w.flush

    # get a notified event
    kevs = kq.kevent(nil, 1) #=> [#<a Event object>]

    # udata have one any object
    kevs.first.udata.call #=> you can read 7 with non block!
  end
end

filter

filter description
EVFILT_READ on read
EVFILT_WRITE on write
EVFILT_AIO attached to aio requests
EVFILT_VNODE attached to vnodes
EVFILT_PROC attached to struct proc
EVFILT_SIGNAL attached to signal
EVFILT_TIMER timers
EVFILT_MACHPORT Mach portsets

flags

flags description
EV_ADD add event to kq (implies enable)
EV_DELETE delete event from kq
EV_ENABLE enable event
EV_DISABLE disable event (not reported)
EV_ONESHOT only report one occurrence
EV_CLEAR clear event state after reporting
EV_RECEIPT force EV_ERROR on success, data == 0
EV_ERROR error, data contains errno(returned value)
EV_EOF EOF detected(returned value)

fflags

fflags description
NOTE_DELETE vnode was removed
NOTE_WRITE data contents changed
NOTE_EXTEND size increased
NOTE_ATTRIB attributes changed
NOTE_LINK link count changed
NOTE_RENAME vnode was renamed
NOTE_REVOKE vnode access was revoked
NOTE_EXIT process exited
NOTE_EXITSTATUS exit status to be returned, valid for child process only
NOTE_FORK process forked
NOTE_EXEC process exec'd
NOTE_SIGNAL shared with EVFILT_SIGNAL
NOTE_REAP process reaped

Installation

Add this line to your application's Gemfile:

gem 'kqueue'

And then execute:

$ bundle

Or install it yourself as:

$ gem install kqueue

Pro Tips

  • Support call without GVL in CRuby (use rb_thread_call_without_gvl())

Fork Me !

This is experimental implementation. I'm waiting for your idea and Pull Request !

see also

kqueue's People

Contributors

ksss avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.