rev 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +58 -0
- data/README +107 -0
- data/ext/libev/ev.c +2440 -0
- data/ext/libev/ev.h +551 -0
- data/ext/libev/ev_epoll.c +174 -0
- data/ext/libev/ev_kqueue.c +186 -0
- data/ext/libev/ev_poll.c +127 -0
- data/ext/libev/ev_port.c +155 -0
- data/ext/libev/ev_select.c +236 -0
- data/ext/libev/ev_vars.h +108 -0
- data/ext/libev/ev_win32.c +117 -0
- data/ext/libev/ev_wrap.h +132 -0
- data/ext/rev/extconf.rb +36 -0
- data/ext/rev/rev.h +44 -0
- data/ext/rev/rev_ext.c +29 -0
- data/ext/rev/rev_io_watcher.c +157 -0
- data/ext/rev/rev_loop.c +254 -0
- data/ext/rev/rev_timer_watcher.c +153 -0
- data/ext/rev/rev_watcher.c +222 -0
- data/ext/rev/rev_watcher.h +79 -0
- data/lib/rev.rb +21 -0
- data/lib/rev/buffered_io.rb +123 -0
- data/lib/rev/dns_resolver.rb +178 -0
- data/lib/rev/io_watcher.rb +18 -0
- data/lib/rev/listener.rb +50 -0
- data/lib/rev/loop.rb +101 -0
- data/lib/rev/server.rb +53 -0
- data/lib/rev/socket.rb +186 -0
- data/lib/rev/timer_watcher.rb +18 -0
- data/lib/rev/watcher.rb +49 -0
- data/spec/rev_spec.rb +26 -0
- metadata +93 -0
data/ext/rev/rev_loop.c
ADDED
@@ -0,0 +1,254 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2007 Tony Arcieri
|
3
|
+
* You may redistribute this under the terms of the Ruby license.
|
4
|
+
* See LICENSE for details
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <assert.h>
|
8
|
+
#include "ruby.h"
|
9
|
+
|
10
|
+
#define EV_STANDALONE 1
|
11
|
+
#include "../libev/ev.h"
|
12
|
+
|
13
|
+
#include "rev.h"
|
14
|
+
|
15
|
+
/* Module and object handles */
|
16
|
+
static VALUE mRev = Qnil;
|
17
|
+
static VALUE cRev_Loop = Qnil;
|
18
|
+
|
19
|
+
/* Data allocators and deallocators */
|
20
|
+
static VALUE Rev_Loop_allocate(VALUE klass);
|
21
|
+
static void Rev_Loop_mark(struct Rev_Loop *loop);
|
22
|
+
static void Rev_Loop_free(struct Rev_Loop *loop);
|
23
|
+
|
24
|
+
/* Method implementations */
|
25
|
+
static VALUE Rev_Loop_default(VALUE klass);
|
26
|
+
static VALUE Rev_Loop_ev_loop_new(VALUE self, VALUE flags);
|
27
|
+
static VALUE Rev_Loop_run_once(VALUE self);
|
28
|
+
static VALUE Rev_Loop_run_once_blocking(void *ptr);
|
29
|
+
static VALUE Rev_Loop_run_nonblock(VALUE self);
|
30
|
+
|
31
|
+
static void Rev_Loop_dispatch_events(struct Rev_Loop *loop_data);
|
32
|
+
|
33
|
+
#define DEFAULT_EVENTBUF_SIZE 32
|
34
|
+
|
35
|
+
void Init_rev_loop()
|
36
|
+
{
|
37
|
+
mRev = rb_define_module("Rev");
|
38
|
+
cRev_Loop = rb_define_class_under(mRev, "Loop", rb_cObject);
|
39
|
+
rb_define_alloc_func(cRev_Loop, Rev_Loop_allocate);
|
40
|
+
rb_define_singleton_method(cRev_Loop, "default", Rev_Loop_default, 0);
|
41
|
+
|
42
|
+
rb_define_private_method(cRev_Loop, "ev_loop_new", Rev_Loop_ev_loop_new, 1);
|
43
|
+
rb_define_method(cRev_Loop, "run_once", Rev_Loop_run_once, 0);
|
44
|
+
rb_define_method(cRev_Loop, "run_nonblock", Rev_Loop_run_nonblock, 0);
|
45
|
+
|
46
|
+
rb_cv_set(cRev_Loop, "@@default_loop", Qnil);
|
47
|
+
}
|
48
|
+
|
49
|
+
static VALUE Rev_Loop_allocate(VALUE klass)
|
50
|
+
{
|
51
|
+
struct Rev_Loop *loop = (struct Rev_Loop *)xmalloc(sizeof(struct Rev_Loop));
|
52
|
+
|
53
|
+
loop->ev_loop = 0;
|
54
|
+
loop->default_loop = 0;
|
55
|
+
|
56
|
+
loop->events_received = 0;
|
57
|
+
loop->eventbuf_size = DEFAULT_EVENTBUF_SIZE;
|
58
|
+
loop->eventbuf = (struct Rev_Event *)xmalloc(sizeof(struct Rev_Event) * DEFAULT_EVENTBUF_SIZE);
|
59
|
+
|
60
|
+
return Data_Wrap_Struct(klass, Rev_Loop_mark, Rev_Loop_free, loop);
|
61
|
+
}
|
62
|
+
|
63
|
+
static void Rev_Loop_mark(struct Rev_Loop *loop)
|
64
|
+
{
|
65
|
+
}
|
66
|
+
|
67
|
+
static void Rev_Loop_free(struct Rev_Loop *loop)
|
68
|
+
{
|
69
|
+
if(!loop->ev_loop)
|
70
|
+
return;
|
71
|
+
|
72
|
+
if(loop->default_loop)
|
73
|
+
ev_default_destroy();
|
74
|
+
else
|
75
|
+
ev_loop_destroy(loop->ev_loop);
|
76
|
+
|
77
|
+
xfree(loop->eventbuf);
|
78
|
+
xfree(loop);
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
/**
|
83
|
+
* call-seq:
|
84
|
+
* Rev::Loop.default -> Rev::Loop
|
85
|
+
*
|
86
|
+
* Retrieve a singleton instance of the default loop for the application
|
87
|
+
*/
|
88
|
+
static VALUE Rev_Loop_default(VALUE klass)
|
89
|
+
{
|
90
|
+
struct Rev_Loop *loop_data;
|
91
|
+
VALUE default_loop = rb_cv_get(klass, "@@default_loop");
|
92
|
+
|
93
|
+
if(default_loop == Qnil) {
|
94
|
+
default_loop = rb_obj_alloc(klass);
|
95
|
+
Data_Get_Struct(default_loop, struct Rev_Loop, loop_data);
|
96
|
+
|
97
|
+
loop_data->ev_loop = ev_default_loop(0);
|
98
|
+
loop_data->default_loop = 1;
|
99
|
+
|
100
|
+
rb_cv_set(klass, "@@default_loop", default_loop);
|
101
|
+
rb_iv_set(default_loop, "@active_watchers", INT2NUM(0));
|
102
|
+
rb_iv_set(default_loop, "@watchers", rb_ary_new());
|
103
|
+
}
|
104
|
+
|
105
|
+
return default_loop;
|
106
|
+
}
|
107
|
+
|
108
|
+
/* Wrapper for populating a Rev_Loop struct with a new event loop */
|
109
|
+
static VALUE Rev_Loop_ev_loop_new(VALUE self, VALUE flags)
|
110
|
+
{
|
111
|
+
struct Rev_Loop *loop_data;
|
112
|
+
Data_Get_Struct(self, struct Rev_Loop, loop_data);
|
113
|
+
|
114
|
+
if(loop_data->ev_loop)
|
115
|
+
rb_raise(rb_eRuntimeError, "loop already initialized");
|
116
|
+
|
117
|
+
loop_data->ev_loop = ev_loop_new(NUM2INT(flags));
|
118
|
+
loop_data->default_loop = 0;
|
119
|
+
|
120
|
+
return Qnil;
|
121
|
+
}
|
122
|
+
|
123
|
+
/* libev callback for receiving events */
|
124
|
+
void Rev_Loop_process_event(VALUE watcher, int revents)
|
125
|
+
{
|
126
|
+
struct Rev_Loop *loop_data;
|
127
|
+
struct Rev_Watcher *watcher_data;
|
128
|
+
|
129
|
+
/* The Global VM lock isn't held right now, but hopefully
|
130
|
+
* we can still do this safely */
|
131
|
+
Data_Get_Struct(watcher, struct Rev_Watcher, watcher_data);
|
132
|
+
Data_Get_Struct(watcher_data->loop, struct Rev_Loop, loop_data);
|
133
|
+
|
134
|
+
/* Well, what better place to explain how this all works than
|
135
|
+
* where the most wonky and convoluted stuff is going on!
|
136
|
+
*
|
137
|
+
* Our call path up to here looks a little something like:
|
138
|
+
*
|
139
|
+
* -> release GVL -> event syscall -> libev callback
|
140
|
+
* (GVL = Global VM Lock) ^^^ You are here
|
141
|
+
*
|
142
|
+
* We released the GVL in the Rev_Loop_run_once() function
|
143
|
+
* so other Ruby threads can run while we make a blocking
|
144
|
+
* system call (one of epoll, kqueue, port, poll, or select,
|
145
|
+
* depending on the platform).
|
146
|
+
*
|
147
|
+
* More specifically, this is a libev callback abstraction
|
148
|
+
* called from a real libev callback in every watcher,
|
149
|
+
* hence this function not being static. The real libev
|
150
|
+
* callbacks are event-specific and handled in a watcher.
|
151
|
+
*
|
152
|
+
* For syscalls like epoll and kqueue, the kernel tells libev
|
153
|
+
* a pointer (to a structure with a pointer) to the watcher
|
154
|
+
* object. No data structure lookups are required at all
|
155
|
+
* (beyond structs), it's smooth O(1) sailing the entire way.
|
156
|
+
* Then libev calls out to the watcher's callback, which
|
157
|
+
* calls this function.
|
158
|
+
*
|
159
|
+
* Now, you may be curious: if the watcher already knew what
|
160
|
+
* event fired, why the hell is it telling the loop? Why
|
161
|
+
* doesn't it just rb_funcall() the appropriate callback?
|
162
|
+
*
|
163
|
+
* Well, the problem is the Global VM Lock isn't held right
|
164
|
+
* now, so we can't rb_funcall() anything. In order to get
|
165
|
+
* it back we have to:
|
166
|
+
*
|
167
|
+
* stash event and return -> acquire GVL -> dispatch to Ruby
|
168
|
+
*
|
169
|
+
* Which is kinda ugly and confusing, but still gives us
|
170
|
+
* an O(1) event loop whose heart is in the kernel itself. w00t!
|
171
|
+
*
|
172
|
+
* So, stash the event in the loop's data struct. When we return
|
173
|
+
* the ev_loop() call being made in the Rev_Loop_run_once_blocking()
|
174
|
+
* function below will also return, at which point the GVL is
|
175
|
+
* reacquired and we can call out to Ruby */
|
176
|
+
|
177
|
+
/* Grow the event buffer if it's too small */
|
178
|
+
if(loop_data->events_received >= loop_data->eventbuf_size) {
|
179
|
+
loop_data->eventbuf_size *= 2;
|
180
|
+
loop_data->eventbuf = (struct Rev_Event *)xrealloc(
|
181
|
+
loop_data->eventbuf,
|
182
|
+
sizeof(struct Rev_Event) * loop_data->eventbuf_size
|
183
|
+
);
|
184
|
+
}
|
185
|
+
|
186
|
+
loop_data->eventbuf[loop_data->events_received].watcher = watcher;
|
187
|
+
loop_data->eventbuf[loop_data->events_received].revents = revents;
|
188
|
+
|
189
|
+
loop_data->events_received++;
|
190
|
+
}
|
191
|
+
|
192
|
+
/**
|
193
|
+
* call-seq:
|
194
|
+
* Rev::Loop.run_once -> nil
|
195
|
+
*
|
196
|
+
* Run the Rev::Loop once, blocking until events are received.
|
197
|
+
*/
|
198
|
+
static VALUE Rev_Loop_run_once(VALUE self)
|
199
|
+
{
|
200
|
+
struct Rev_Loop *loop_data;
|
201
|
+
Data_Get_Struct(self, struct Rev_Loop, loop_data);
|
202
|
+
|
203
|
+
assert(loop_data->ev_loop && !loop_data->events_received);
|
204
|
+
rb_thread_blocking_region(Rev_Loop_run_once_blocking, loop_data->ev_loop, RB_UBF_DFL, 0);
|
205
|
+
Rev_Loop_dispatch_events(loop_data);
|
206
|
+
loop_data->events_received = 0;
|
207
|
+
|
208
|
+
return Qnil;
|
209
|
+
}
|
210
|
+
|
211
|
+
static VALUE Rev_Loop_run_once_blocking(void *ptr)
|
212
|
+
{
|
213
|
+
/* The libev loop has now escaped through the Global VM Lock unscathed! */
|
214
|
+
struct ev_loop *loop = (struct ev_loop *)ptr;
|
215
|
+
|
216
|
+
ev_loop(loop, EVLOOP_ONESHOT);
|
217
|
+
return Qnil;
|
218
|
+
}
|
219
|
+
|
220
|
+
/**
|
221
|
+
* call-seq:
|
222
|
+
* Rev::Loop.run_once -> nil
|
223
|
+
*
|
224
|
+
* Run the Rev::Loop once, but return immediately if there are no pending events.
|
225
|
+
*/
|
226
|
+
static VALUE Rev_Loop_run_nonblock(VALUE self)
|
227
|
+
{
|
228
|
+
struct Rev_Loop *loop_data;
|
229
|
+
Data_Get_Struct(self, struct Rev_Loop, loop_data);
|
230
|
+
|
231
|
+
assert(loop_data->ev_loop && !loop_data->events_received);
|
232
|
+
ev_loop(loop_data->ev_loop, EVLOOP_NONBLOCK);
|
233
|
+
Rev_Loop_dispatch_events(loop_data);
|
234
|
+
loop_data->events_received = 0;
|
235
|
+
|
236
|
+
return Qnil;
|
237
|
+
}
|
238
|
+
|
239
|
+
static void Rev_Loop_dispatch_events(struct Rev_Loop *loop_data)
|
240
|
+
{
|
241
|
+
int i;
|
242
|
+
struct Rev_Watcher *watcher_data;
|
243
|
+
|
244
|
+
for(i = 0; i < loop_data->events_received; i++) {
|
245
|
+
/* A watcher with pending events may have been detached from the loop
|
246
|
+
* during the dispatch process. If so, the watcher clears the pending
|
247
|
+
* events, so skip over them */
|
248
|
+
if(loop_data->eventbuf[i].watcher == Qnil)
|
249
|
+
continue;
|
250
|
+
|
251
|
+
Data_Get_Struct(loop_data->eventbuf[i].watcher, struct Rev_Watcher, watcher_data);
|
252
|
+
watcher_data->dispatch_callback(loop_data->eventbuf[i].watcher, loop_data->eventbuf[i].revents);
|
253
|
+
}
|
254
|
+
}
|
@@ -0,0 +1,153 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2007 Tony Arcieri
|
3
|
+
* You may redistribute this under the terms of the Ruby license.
|
4
|
+
* See LICENSE for details
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include "ruby.h"
|
8
|
+
|
9
|
+
#define EV_STANDALONE 1
|
10
|
+
#include "../libev/ev.h"
|
11
|
+
|
12
|
+
#include "rev.h"
|
13
|
+
#include "rev_watcher.h"
|
14
|
+
|
15
|
+
/* Module and object handles */
|
16
|
+
static VALUE mRev = Qnil;
|
17
|
+
static VALUE cRev_Watcher = Qnil;
|
18
|
+
static VALUE cRev_TimerWatcher = Qnil;
|
19
|
+
static VALUE cRev_Loop = Qnil;
|
20
|
+
|
21
|
+
/* Method implementations */
|
22
|
+
static VALUE Rev_TimerWatcher_initialize(int argc, VALUE *argv, VALUE self);
|
23
|
+
static VALUE Rev_TimerWatcher_attach(VALUE self, VALUE loop);
|
24
|
+
static VALUE Rev_TimerWatcher_detach(VALUE self);
|
25
|
+
static VALUE Rev_TimerWatcher_enable(VALUE self);
|
26
|
+
static VALUE Rev_TimerWatcher_disable(VALUE self);
|
27
|
+
static VALUE Rev_TimerWatcher_reset(VALUE self);
|
28
|
+
static VALUE Rev_TimerWatcher_on_timer(VALUE self);
|
29
|
+
|
30
|
+
/* Callbacks */
|
31
|
+
static void Rev_TimerWatcher_libev_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents);
|
32
|
+
static void Rev_TimerWatcher_dispatch_callback(VALUE self, int revents);
|
33
|
+
|
34
|
+
void Init_rev_timer_watcher()
|
35
|
+
{
|
36
|
+
mRev = rb_define_module("Rev");
|
37
|
+
cRev_Watcher = rb_define_class_under(mRev, "Watcher", rb_cObject);
|
38
|
+
cRev_TimerWatcher = rb_define_class_under(mRev, "TimerWatcher", cRev_Watcher);
|
39
|
+
cRev_Loop = rb_define_class_under(mRev, "Loop", rb_cObject);
|
40
|
+
|
41
|
+
rb_define_method(cRev_TimerWatcher, "initialize", Rev_TimerWatcher_initialize, -1);
|
42
|
+
rb_define_method(cRev_TimerWatcher, "attach", Rev_TimerWatcher_attach, 1);
|
43
|
+
rb_define_method(cRev_TimerWatcher, "detach", Rev_TimerWatcher_detach, 0);
|
44
|
+
rb_define_method(cRev_TimerWatcher, "enable", Rev_TimerWatcher_enable, 0);
|
45
|
+
rb_define_method(cRev_TimerWatcher, "disable", Rev_TimerWatcher_disable, 0);
|
46
|
+
rb_define_method(cRev_TimerWatcher, "reset", Rev_TimerWatcher_reset, 0);
|
47
|
+
rb_define_method(cRev_TimerWatcher, "on_timer", Rev_TimerWatcher_on_timer, 0);
|
48
|
+
}
|
49
|
+
|
50
|
+
/**
|
51
|
+
* call-seq:
|
52
|
+
* Rev::TimerWatcher.initialize(interval, repeating = false) -> Rev::TimerWatcher
|
53
|
+
*
|
54
|
+
* Create a new Rev::TimerWatcher for the given IO object and add it to the given Rev::Loop
|
55
|
+
*/
|
56
|
+
static VALUE Rev_TimerWatcher_initialize(int argc, VALUE *argv, VALUE self)
|
57
|
+
{
|
58
|
+
VALUE interval, repeating;
|
59
|
+
struct Rev_Watcher *watcher_data;
|
60
|
+
|
61
|
+
rb_scan_args(argc, argv, "11", &interval, &repeating);
|
62
|
+
|
63
|
+
Data_Get_Struct(self, struct Rev_Watcher, watcher_data);
|
64
|
+
|
65
|
+
watcher_data->dispatch_callback = Rev_TimerWatcher_dispatch_callback;
|
66
|
+
ev_timer_init(&watcher_data->event_types.ev_timer, Rev_TimerWatcher_libev_callback, NUM2INT(interval), repeating == Qtrue);
|
67
|
+
watcher_data->event_types.ev_timer.data = (void *)self;
|
68
|
+
|
69
|
+
return Qnil;
|
70
|
+
}
|
71
|
+
|
72
|
+
static VALUE Rev_TimerWatcher_attach(VALUE self, VALUE loop)
|
73
|
+
{
|
74
|
+
Watcher_Attach(timer, Rev_TimerWatcher_detach, self, loop);
|
75
|
+
|
76
|
+
return self;
|
77
|
+
}
|
78
|
+
|
79
|
+
static VALUE Rev_TimerWatcher_detach(VALUE self)
|
80
|
+
{
|
81
|
+
Watcher_Detach(timer, self);
|
82
|
+
|
83
|
+
return self;
|
84
|
+
}
|
85
|
+
|
86
|
+
static VALUE Rev_TimerWatcher_enable(VALUE self)
|
87
|
+
{
|
88
|
+
Watcher_Enable(timer, self);
|
89
|
+
|
90
|
+
return self;
|
91
|
+
}
|
92
|
+
|
93
|
+
static VALUE Rev_TimerWatcher_disable(VALUE self)
|
94
|
+
{
|
95
|
+
Watcher_Disable(timer, self);
|
96
|
+
|
97
|
+
return self;
|
98
|
+
}
|
99
|
+
|
100
|
+
/**
|
101
|
+
* call-seq:
|
102
|
+
* Rev::TimerWatcher#reset -> Rev::TimerWatcher
|
103
|
+
*
|
104
|
+
* Reset the TimerWatcher. This behaves differently depending on if it's repeating.
|
105
|
+
*
|
106
|
+
* If the timer is pending, its pending status is cleared.
|
107
|
+
*
|
108
|
+
* If the timer is attached but nonrepeating, stop it (as if it timed out)
|
109
|
+
*
|
110
|
+
* If the timer is repeating, reset it so it will fire again after its given interval
|
111
|
+
*/
|
112
|
+
static VALUE Rev_TimerWatcher_reset(VALUE self)
|
113
|
+
{
|
114
|
+
struct Rev_Watcher *watcher_data;
|
115
|
+
struct Rev_Loop *loop_data;
|
116
|
+
|
117
|
+
Data_Get_Struct(self, struct Rev_Watcher, watcher_data);
|
118
|
+
|
119
|
+
if(watcher_data->loop == Qnil)
|
120
|
+
rb_raise(rb_eRuntimeError, "not attached to a loop");
|
121
|
+
|
122
|
+
Data_Get_Struct(watcher_data->loop, struct Rev_Loop, loop_data);
|
123
|
+
|
124
|
+
ev_timer_again(loop_data->ev_loop, &watcher_data->event_types.ev_timer);
|
125
|
+
|
126
|
+
return self;
|
127
|
+
}
|
128
|
+
|
129
|
+
/**
|
130
|
+
* call-seq:
|
131
|
+
* Rev::TimerWatcher#on_timer -> nil
|
132
|
+
*
|
133
|
+
* Called whenever the TimerWatcher fires
|
134
|
+
*/
|
135
|
+
static VALUE Rev_TimerWatcher_on_timer(VALUE self)
|
136
|
+
{
|
137
|
+
return Qnil;
|
138
|
+
}
|
139
|
+
|
140
|
+
/* libev callback */
|
141
|
+
static void Rev_TimerWatcher_libev_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents)
|
142
|
+
{
|
143
|
+
Rev_Loop_process_event((VALUE)timer->data, revents);
|
144
|
+
}
|
145
|
+
|
146
|
+
/* Rev::Loop dispatch callback */
|
147
|
+
static void Rev_TimerWatcher_dispatch_callback(VALUE self, int revents)
|
148
|
+
{
|
149
|
+
if(revents & EV_TIMEOUT)
|
150
|
+
rb_funcall(self, rb_intern("on_timer"), 0, 0);
|
151
|
+
else
|
152
|
+
rb_raise(rb_eRuntimeError, "unknown revents value for ev_timer: %d", revents);
|
153
|
+
}
|
@@ -0,0 +1,222 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2007 Tony Arcieri
|
3
|
+
* You may redistribute this under the terms of the Ruby license.
|
4
|
+
* See LICENSE for details
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include "ruby.h"
|
8
|
+
|
9
|
+
#define EV_STANDALONE 1
|
10
|
+
#include "../libev/ev.h"
|
11
|
+
|
12
|
+
#include "rev.h"
|
13
|
+
|
14
|
+
/* Module and object handles */
|
15
|
+
static VALUE mRev = Qnil;
|
16
|
+
static VALUE cRev_Watcher = Qnil;
|
17
|
+
|
18
|
+
/* Data allocators and deallocators */
|
19
|
+
static VALUE Rev_Watcher_allocate(VALUE klass);
|
20
|
+
static void Rev_Watcher_mark(struct Rev_Watcher *watcher);
|
21
|
+
static void Rev_Watcher_free(struct Rev_Watcher *watcher);
|
22
|
+
|
23
|
+
/* Method implementations */
|
24
|
+
static VALUE Rev_Watcher_initialize(VALUE self);
|
25
|
+
static VALUE Rev_Watcher_attach(VALUE self, VALUE loop);
|
26
|
+
static VALUE Rev_Watcher_detach(VALUE self);
|
27
|
+
static VALUE Rev_Watcher_enable(VALUE self);
|
28
|
+
static VALUE Rev_Watcher_disable(VALUE self);
|
29
|
+
static VALUE Rev_Watcher_evloop(VALUE self);
|
30
|
+
static VALUE Rev_Watcher_attached(VALUE self);
|
31
|
+
|
32
|
+
void Init_rev_watcher()
|
33
|
+
{
|
34
|
+
mRev = rb_define_module("Rev");
|
35
|
+
cRev_Watcher = rb_define_class_under(mRev, "Watcher", rb_cObject);
|
36
|
+
rb_define_alloc_func(cRev_Watcher, Rev_Watcher_allocate);
|
37
|
+
|
38
|
+
rb_define_method(cRev_Watcher, "initialize", Rev_Watcher_initialize, 0);
|
39
|
+
rb_define_method(cRev_Watcher, "attach", Rev_Watcher_attach, 1);
|
40
|
+
rb_define_method(cRev_Watcher, "detach", Rev_Watcher_detach, 0);
|
41
|
+
rb_define_method(cRev_Watcher, "enable", Rev_Watcher_enable, 0);
|
42
|
+
rb_define_method(cRev_Watcher, "disable", Rev_Watcher_disable, 0);
|
43
|
+
rb_define_method(cRev_Watcher, "evloop", Rev_Watcher_evloop, 0);
|
44
|
+
rb_define_method(cRev_Watcher, "attached?", Rev_Watcher_attached, 0);
|
45
|
+
}
|
46
|
+
|
47
|
+
static VALUE Rev_Watcher_allocate(VALUE klass)
|
48
|
+
{
|
49
|
+
struct Rev_Watcher *watcher_data = (struct Rev_Watcher *)xmalloc(sizeof(struct Rev_Watcher));
|
50
|
+
|
51
|
+
watcher_data->loop = Qnil;
|
52
|
+
watcher_data->enabled = 0;
|
53
|
+
|
54
|
+
return Data_Wrap_Struct(klass, Rev_Watcher_mark, Rev_Watcher_free, watcher_data);
|
55
|
+
}
|
56
|
+
|
57
|
+
static void Rev_Watcher_mark(struct Rev_Watcher *watcher_data)
|
58
|
+
{
|
59
|
+
if(watcher_data->loop != Qnil)
|
60
|
+
rb_gc_mark(watcher_data->loop);
|
61
|
+
}
|
62
|
+
|
63
|
+
static void Rev_Watcher_free(struct Rev_Watcher *watcher_data)
|
64
|
+
{
|
65
|
+
xfree(watcher_data);
|
66
|
+
}
|
67
|
+
|
68
|
+
static VALUE Rev_Watcher_initialize(VALUE self)
|
69
|
+
{
|
70
|
+
rb_raise(rb_eRuntimeError, "watcher base class should not be initialized directly");
|
71
|
+
}
|
72
|
+
|
73
|
+
/**
|
74
|
+
* call-seq:
|
75
|
+
* Rev::Watcher.attach(loop) -> Rev::Watcher
|
76
|
+
*
|
77
|
+
* Attach the watcher to the given Rev::Loop. If the watcher is already attached
|
78
|
+
* to a loop, detach it from the old one and attach it to the new one.
|
79
|
+
*/
|
80
|
+
static VALUE Rev_Watcher_attach(VALUE self, VALUE loop)
|
81
|
+
{
|
82
|
+
VALUE loop_watchers, active_watchers;
|
83
|
+
|
84
|
+
loop_watchers = rb_iv_get(loop, "@watchers");
|
85
|
+
|
86
|
+
if(loop_watchers == Qnil) {
|
87
|
+
loop_watchers = rb_ary_new();
|
88
|
+
rb_iv_set(loop, "@watchers", loop_watchers);
|
89
|
+
}
|
90
|
+
|
91
|
+
/* Add us to the loop's array of active watchers. This is mainly done
|
92
|
+
* to keep the VM from garbage collecting watchers that are associated
|
93
|
+
* with a loop (and also lets you see within Ruby which watchers are
|
94
|
+
* associated with a given loop), but isn't really necessary for any
|
95
|
+
* other reason */
|
96
|
+
rb_ary_push(loop_watchers, self);
|
97
|
+
|
98
|
+
active_watchers = rb_iv_get(loop, "@active_watchers");
|
99
|
+
if(active_watchers == Qnil)
|
100
|
+
active_watchers = INT2NUM(1);
|
101
|
+
else
|
102
|
+
active_watchers = INT2NUM(NUM2INT(active_watchers) + 1);
|
103
|
+
rb_iv_set(loop, "@active_watchers", active_watchers);
|
104
|
+
|
105
|
+
return self;
|
106
|
+
}
|
107
|
+
|
108
|
+
/**
|
109
|
+
* call-seq:
|
110
|
+
* Rev::Watcher.detach -> Rev::Watcher
|
111
|
+
*
|
112
|
+
* Detach the watcher from its current Rev::Loop.
|
113
|
+
*/
|
114
|
+
static VALUE Rev_Watcher_detach(VALUE self)
|
115
|
+
{
|
116
|
+
struct Rev_Watcher *watcher_data;
|
117
|
+
struct Rev_Loop *loop_data;
|
118
|
+
VALUE loop_watchers;
|
119
|
+
|
120
|
+
int i;
|
121
|
+
|
122
|
+
Data_Get_Struct(self, struct Rev_Watcher, watcher_data);
|
123
|
+
|
124
|
+
if(watcher_data->loop == Qnil)
|
125
|
+
rb_raise(rb_eRuntimeError, "not attached to a loop");
|
126
|
+
|
127
|
+
loop_watchers = rb_iv_get(watcher_data->loop, "@watchers");
|
128
|
+
|
129
|
+
/* Remove us from the loop's array of active watchers. This likely
|
130
|
+
* has negative performance and scalability characteristics as this
|
131
|
+
* isn't an O(1) operation. Hopefully there's a better way... */
|
132
|
+
rb_ary_delete(loop_watchers, self);
|
133
|
+
|
134
|
+
rb_iv_set(
|
135
|
+
watcher_data->loop,
|
136
|
+
"@active_watchers",
|
137
|
+
INT2NUM(NUM2INT(rb_iv_get(watcher_data->loop, "@active_watchers")) - 1)
|
138
|
+
);
|
139
|
+
|
140
|
+
Data_Get_Struct(watcher_data->loop, struct Rev_Loop, loop_data);
|
141
|
+
|
142
|
+
/* Iterate through the events in the loop's event buffer. If there
|
143
|
+
* are any pending events from this watcher, mark them NULL. The
|
144
|
+
* dispatch loop will skip them. This prevents watchers earlier
|
145
|
+
* in the event buffer from detaching others which may have pending
|
146
|
+
* events in the buffer but get garbage collected in the meantime */
|
147
|
+
for(i = 0; i < loop_data->events_received; i++) {
|
148
|
+
if(loop_data->eventbuf[i].watcher == self)
|
149
|
+
loop_data->eventbuf[i].watcher = Qnil;
|
150
|
+
}
|
151
|
+
|
152
|
+
watcher_data->loop = Qnil;
|
153
|
+
|
154
|
+
return self;
|
155
|
+
}
|
156
|
+
|
157
|
+
/**
|
158
|
+
* call-seq:
|
159
|
+
* Rev::Watcher.enable -> Rev::Watcher
|
160
|
+
*
|
161
|
+
* Re-enable a watcher which has been temporarily disabled. See the
|
162
|
+
* disable method for a more thorough explanation.
|
163
|
+
*/
|
164
|
+
static VALUE Rev_Watcher_enable(VALUE self)
|
165
|
+
{
|
166
|
+
struct Rev_Watcher *watcher_data;
|
167
|
+
Data_Get_Struct(self, struct Rev_Watcher, watcher_data);
|
168
|
+
|
169
|
+
rb_iv_set(
|
170
|
+
watcher_data->loop,
|
171
|
+
"@active_watchers",
|
172
|
+
INT2NUM(NUM2INT(rb_iv_get(watcher_data->loop, "@active_watchers")) + 1)
|
173
|
+
);
|
174
|
+
|
175
|
+
return self;
|
176
|
+
}
|
177
|
+
|
178
|
+
/**
|
179
|
+
* call-seq:
|
180
|
+
* Rev::Watcher.disable -> Rev::Watcher
|
181
|
+
*
|
182
|
+
* Temporarily disable an event watcher which is attached to a loop.
|
183
|
+
* This is useful if you wish to toggle event monitoring on and off.
|
184
|
+
*/
|
185
|
+
static VALUE Rev_Watcher_disable(VALUE self)
|
186
|
+
{
|
187
|
+
struct Rev_Watcher *watcher_data;
|
188
|
+
Data_Get_Struct(self, struct Rev_Watcher, watcher_data);
|
189
|
+
|
190
|
+
rb_iv_set(
|
191
|
+
watcher_data->loop,
|
192
|
+
"@active_watchers",
|
193
|
+
INT2NUM(NUM2INT(rb_iv_get(watcher_data->loop, "@active_watchers")) - 1)
|
194
|
+
);
|
195
|
+
|
196
|
+
return self;
|
197
|
+
}
|
198
|
+
|
199
|
+
/**
|
200
|
+
* call-seq:
|
201
|
+
* Rev::Watcher.evloop -> Rev::Loop
|
202
|
+
*
|
203
|
+
* Return the loop to which we're currently attached
|
204
|
+
*/
|
205
|
+
static VALUE Rev_Watcher_evloop(VALUE self)
|
206
|
+
{
|
207
|
+
struct Rev_Watcher *watcher_data;
|
208
|
+
|
209
|
+
Data_Get_Struct(self, struct Rev_Watcher, watcher_data);
|
210
|
+
return watcher_data->loop;
|
211
|
+
}
|
212
|
+
|
213
|
+
/**
|
214
|
+
* call-seq:
|
215
|
+
* Rev::Watcher.attached? -> Boolean
|
216
|
+
*
|
217
|
+
* Is the watcher currently attached to an event loop?
|
218
|
+
*/
|
219
|
+
static VALUE Rev_Watcher_attached(VALUE self)
|
220
|
+
{
|
221
|
+
return Rev_Watcher_evloop(self) != Qnil;
|
222
|
+
}
|