Class: Rev::Loop

Inherits:
Object
  • Object
show all
Defined in:
lib/rev/loop.rb,
ext/rev/rev_loop.c,
ext/rev/rev_io_watcher.c,
ext/rev/rev_timer_watcher.c

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Loop

Create a new Rev::Loop

Options:

:skip_environment (boolean)

Ignore the $LIBEV_FLAGS environment variable

:fork_check (boolean)

Enable autodetection of forks

:backend

Choose the default backend, one (or many in an array) of:
  :select (most platforms)
  :poll   (most platforms except Windows)
  :epoll  (Linux)
  :kqueue (BSD/Mac OS X)
  :port   (Solaris 10)


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
# File 'lib/rev/loop.rb', line 31

def initialize(options = {})
  @watchers = []
  @active_watchers = 0
  
  flags = 0

  options.each do |option, value|
    case option
    when :skip_environment
      flags |= EVFLAG_NOEV if value
    when :fork_check
      flags |= EVFLAG_FORKCHECK if value
    when :backend
      value = [value] unless value.is_a? Array
      value.each do |backend|
        case backend
        when :select then flags |= EVBACKEND_SELECT
        when :poll   then flags |= EVBACKEND_POLL
        when :epoll  then flags |= EVBACKEND_EPOLL
        when :kqueue then flags |= EVBACKEND_KQUEUE
        when :port   then flags |= EVBACKEND_PORT
        else raise ArgumentError, "no such backend: #{backend}"
        end
      end
    else raise ArgumentError, "no such option: #{option}"
    end
  end

  @loop = ev_loop_new(flags)
end

Instance Attribute Details

#watchersObject (readonly)

Returns the value of attribute watchers.



11
12
13
# File 'lib/rev/loop.rb', line 11

def watchers
  @watchers
end

Class Method Details

.defaultObject

Method implementations



25
# File 'ext/rev/rev_loop.c', line 25

static VALUE Rev_Loop_default(VALUE klass);

Instance Method Details

#attach(watcher) ⇒ Object

Attach a watcher to the loop



63
64
65
# File 'lib/rev/loop.rb', line 63

def attach(watcher)
  watcher.attach self
end

#runObject

Run the event loop and dispatch events back to Ruby. If there are no watchers associated with the event loop it will return immediately. Otherwise, run will continue blocking and making event callbacks to watchers until all watchers associated with the loop have been disabled or detached. The loop may be explicitly stopped by calling the stop method on the loop object.

Raises:

  • (RuntimeError)


73
74
75
76
77
78
79
80
# File 'lib/rev/loop.rb', line 73

def run
  raise RuntimeError, "no watchers for this loop" if @watchers.empty?

  @running = true
  while @running and not @active_watchers.zero?
    run_once
  end
end

#run_nonblockObject



29
# File 'ext/rev/rev_loop.c', line 29

static VALUE Rev_Loop_run_nonblock(VALUE self);

#run_onceObject



27
# File 'ext/rev/rev_loop.c', line 27

static VALUE Rev_Loop_run_once(VALUE self);

#stopObject

Stop the event loop if it’s running

Raises:

  • (RuntimeError)


83
84
85
86
# File 'lib/rev/loop.rb', line 83

def stop
  raise RuntimeError, "loop not running" unless @running
  @running = false
end