rev 0.1.4 → 0.2.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.
@@ -0,0 +1,130 @@
1
+ libev is a high-performance event loop/event model with lots of features.
2
+ (see benchmark at http://libev.schmorp.de/bench.html)
3
+
4
+ Homepage: http://software.schmorp.de/pkg/libev
5
+ E-Mail: libev@lists.schmorp.de
6
+ Library Documentation: http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod
7
+
8
+ It is modelled (very losely) after libevent and the Event perl module,
9
+ but aims to be faster and more correct, and also more featureful. And
10
+ also smaller. Yay.
11
+
12
+ ABOUT THIS DISTRIBUTION
13
+
14
+ If you downloaded the libevent+libev distribution of libev, you will
15
+ find it looks very much like libevent. In fact, the distributed libev
16
+ tarballs are indeed libevent tarballs patched up with the libev
17
+ event core, taking the evbuffer, evtag, evdns and evhttpd parts from
18
+ libevent (they use the libevent emulation inside libev). Configure and
19
+ Makefile stuff is also a more or less direct copy of libevent, and are
20
+ maintained by the libevent authors.
21
+
22
+ If you downloaded the libev distribution (without libevent), then
23
+ you only get the core parts of the library, meaning http and dns
24
+ client/server code and similar things are missing. Only the core event
25
+ loop is included.
26
+
27
+ If you are looking for an easily embeddable version, I recommend using
28
+ the libev standalone distribution or the CVS repository.
29
+
30
+ Examples of programs that embed libev: the EV perl module,
31
+ rxvt-unicode, gvpe (GNU Virtual Private Ethernet) and deliantra
32
+ (http://www.deliantra.net).
33
+
34
+ DIFFERENCES AND COMPARISON TO LIBEVENT
35
+
36
+ The comparisons below are relative to libevent-1.3e.
37
+
38
+ - multiple watchers can wait for the same event without deregistering others,
39
+ both for file descriptors as well as signals.
40
+ (registering two read events on fd 10 and unregistering one will not
41
+ break the other).
42
+
43
+ - fork() is supported and can be handled
44
+ (there is no way to recover from a fork with libevent).
45
+
46
+ - timers are handled as a priority queue (important operations are O(1))
47
+ (libevent uses a much less efficient but more complex red-black tree).
48
+
49
+ - supports absolute (wallclock-based) timers in addition to relative ones,
50
+ i.e. can schedule timers to occur after n seconds, or at a specific time.
51
+
52
+ - timers can be repeating (both absolute and relative ones).
53
+
54
+ - absolute timers can have customised rescheduling hooks (suitable for cron-like
55
+ applications).
56
+
57
+ - detects time jumps and adjusts timers
58
+ (works for both forward and backward time jumps and also for absolute timers).
59
+
60
+ - race-free signal processing
61
+ (libevent may delay processing signals till after the next event).
62
+
63
+ - more efficient epoll backend
64
+ (stopping and starting an io watcher between two loop iterations will not
65
+ result in spurious epoll_ctl calls).
66
+
67
+ - usually less calls to gettimeofday and clock_gettime
68
+ (libevent calls it on every timer event change, libev twice per iteration).
69
+
70
+ - watchers use less memory
71
+ (libevent watcher on amd64: 152 bytes, libev native: <= 56 bytes, libevent emulation: 144 bytes).
72
+
73
+ - library uses less memory
74
+ (libevent allocates large data structures wether used or not, libev
75
+ scales all its data structures dynamically).
76
+
77
+ - no hardcoded arbitrary limits
78
+ (libevent contains an off-by-one bug and sometimes hardcodes limits).
79
+
80
+ - libev separates timer, signal and io watchers from each other
81
+ (libevent combines them, but with libev you can combine them yourself
82
+ by reusing the same callback and still save memory).
83
+
84
+ - simpler design, backends are potentially much simpler
85
+ (in libevent, backends have to deal with watchers, thus the problems with
86
+ wildly different semantics between diferent backends)
87
+ (epoll backend in libevent: 366 lines no caching, libev: 90 lines full caching).
88
+
89
+ - libev handles EBADF gracefully by removing the offending fds.
90
+
91
+ - libev communicates errors to the callback, libevent to the
92
+ event adder or not at all.
93
+
94
+ - doesn't rely on nonportable BSD header files.
95
+
96
+ - an event.h compatibility header exists, and can be used to run a wide
97
+ range of libevent programs unchanged (such as evdns.c).
98
+
99
+ - win32 compatibility for the core parts.
100
+ (the backend is fd-based as documented and on other platforms,
101
+ not handle-based like libevent, and can be used for both winscoket environments
102
+ and unix-like ones).
103
+
104
+ - libev can be embedded easily with or without autoconf support into
105
+ other programs, with no changes to the source code necessary.
106
+
107
+ - the event core library (ev and event layer) compiles and works both as
108
+ C and C++.
109
+
110
+ - a simple C++ wrapper that supports methods as callbacks exists.
111
+
112
+ - a full featured and widely used perl module is available.
113
+
114
+ whats missing?
115
+
116
+ - no event-like priority support at the moment (the ev priorities work
117
+ differently, but you can use idle watchers to get a similar effect).
118
+
119
+ AUTHOR
120
+
121
+ libev was written and designed by Marc Lehmann and Emanuele Giaquinta.
122
+
123
+ The following people sent in patches or made other noteworthy
124
+ contributions to the design (if I forgot to include you, please shout
125
+ at me, it was an accident):
126
+
127
+ W.C.A. Wijngaards
128
+ Christopher Layne
129
+ Chris Brody
130
+
@@ -0,0 +1,3 @@
1
+ This file is now included in the main libev documentation, see
2
+
3
+ http://cvs.schmorp.de/libev/ev.html
@@ -0,0 +1,19 @@
1
+ #!/bin/sh
2
+
3
+ (
4
+ echo '#define VAR(name,decl) name'
5
+ echo '#define EV_GENWRAP 1'
6
+ cat ev_vars.h
7
+ ) | cc -E -o - - | perl -ne '
8
+ while (<>) {
9
+ push @syms, $1 if /(^\w+)/;
10
+ }
11
+ print "/* DO NOT EDIT, automatically generated by update_ev_wrap */\n",
12
+ "#ifndef EV_WRAP_H\n",
13
+ "#define EV_WRAP_H\n",
14
+ (map "#define $_ ((loop)->$_)\n", @syms),
15
+ "#else\n",
16
+ "#undef EV_WRAP_H\n",
17
+ (map "#undef $_\n", @syms),
18
+ "#endif\n";
19
+ ' >ev_wrap.h
@@ -1,36 +1,49 @@
1
1
  require 'mkmf'
2
2
 
3
- flags = []
3
+ cflags = []
4
+ libs = []
4
5
 
5
- unless have_func('rb_thread_blocking_region') and have_macro('RB_UBF_DFL', 'ruby.h')
6
- abort "Rev requires Ruby 1.9.0 or greater"
6
+ if have_func('rb_thread_blocking_region')
7
+ cflags << '-DHAVE_RB_THREAD_BLOCKING_REGION'
7
8
  end
8
9
 
9
10
  if have_header('sys/select.h')
10
- flags << '-DEV_USE_SELECT'
11
+ cflags << '-DEV_USE_SELECT'
11
12
  end
12
13
 
13
14
  if have_header('poll.h')
14
- flags << '-DEV_USE_POLL'
15
+ cflags << '-DEV_USE_POLL'
15
16
  end
16
17
 
17
18
  if have_header('sys/epoll.h')
18
- flags << '-DEV_USE_EPOLL'
19
+ cflags << '-DEV_USE_EPOLL'
19
20
  end
20
21
 
21
22
  if have_header('sys/event.h') and have_header('sys/queue.h')
22
- flags << '-DEV_USE_KQUEUE'
23
+ cflags << '-DEV_USE_KQUEUE'
23
24
  end
24
25
 
25
26
  if have_header('port.h')
26
- flags << '-DEV_USE_PORT'
27
+ cflags << '-DEV_USE_PORT'
27
28
  end
28
29
 
29
- if have_header('sys/inotify.h')
30
- flags << '-DEV_USE_INOTIFY'
30
+ if have_header('openssl/ssl.h')
31
+ cflags << '-DHAVE_OPENSSL_SSL_H'
32
+ libs << '-lssl -lcrypto'
31
33
  end
32
34
 
33
- $CFLAGS << ' ' << flags.join(' ')
35
+ # ncpu detection specifics
36
+ case RUBY_PLATFORM
37
+ when /linux/
38
+ cflags << '-DHAVE_LINUX_PROCFS'
39
+ else
40
+ if have_func('sysctlbyname', ['sys/param.h', 'sys/sysctl.h'])
41
+ cflags << '-DHAVE_SYSCTLBYNAME'
42
+ end
43
+ end
44
+
45
+ $CFLAGS << ' ' << cflags.join(' ')
46
+ $LIBS << ' ' << libs.join(' ')
34
47
 
35
48
  dir_config('rev_ext')
36
49
  create_makefile('rev_ext')
@@ -8,6 +8,13 @@
8
8
  #define REV_H
9
9
 
10
10
  #include "ruby.h"
11
+ #include "rubyio.h"
12
+
13
+ #if HAVE_RB_IO_T
14
+ #define FPTR_TO_FD(fptr) fptr->fd
15
+ #else
16
+ #define FPTR_TO_FD(fptr) fileno(fptr->f)
17
+ #endif
11
18
 
12
19
  struct Rev_Event
13
20
  {
@@ -6,6 +6,12 @@
6
6
 
7
7
  #include "ruby.h"
8
8
  #include "rubyio.h"
9
+
10
+ #define EV_STANDALONE 1
11
+ #include "../libev/ev.h"
12
+
13
+ #include "rev.h"
14
+
9
15
  #include <assert.h>
10
16
 
11
17
  #include <string.h>
@@ -37,16 +43,13 @@ struct buffer_node {
37
43
  unsigned char data[0];
38
44
  };
39
45
 
40
- /* Module and object handles */
41
46
  static VALUE mRev = Qnil;
42
47
  static VALUE cRev_Buffer = Qnil;
43
48
 
44
- /* Data allocators and deallocators */
45
49
  static VALUE Rev_Buffer_allocate(VALUE klass);
46
50
  static void Rev_Buffer_mark(struct buffer *);
47
51
  static void Rev_Buffer_free(struct buffer *);
48
52
 
49
- /* Method implementations */
50
53
  static VALUE Rev_Buffer_initialize(int argc, VALUE *argv, VALUE self);
51
54
  static VALUE Rev_Buffer_clear(VALUE self);
52
55
  static VALUE Rev_Buffer_size(VALUE self);
@@ -58,7 +61,6 @@ static VALUE Rev_Buffer_to_str(VALUE self);
58
61
  static VALUE Rev_Buffer_read_from(VALUE self, VALUE io);
59
62
  static VALUE Rev_Buffer_write_to(VALUE self, VALUE io);
60
63
 
61
- /* Prototypes for internal functions */
62
64
  static struct buffer *buffer_new(void);
63
65
  static void buffer_clear(struct buffer *buf);
64
66
  static void buffer_free(struct buffer *buf);
@@ -71,8 +73,7 @@ static int buffer_read_from(struct buffer *buf, int fd);
71
73
  static int buffer_write_to(struct buffer *buf, int fd);
72
74
 
73
75
  /*
74
- * High speed buffering geared towards non-blocking I/O. This was largely
75
- * written in response to String#slice! being incredibly slow in Ruby 1.9.
76
+ * High speed buffering geared towards non-blocking I/O.
76
77
  *
77
78
  * Data is stored in a byte queue implemented as a linked list of equal size
78
79
  * chunks. Since every node in the list is the same size they are easily
@@ -287,13 +288,17 @@ static VALUE Rev_Buffer_to_str(VALUE self) {
287
288
  */
288
289
  static VALUE Rev_Buffer_read_from(VALUE self, VALUE io) {
289
290
  struct buffer *buf;
291
+ #if HAVE_RB_IO_T
290
292
  rb_io_t *fptr;
293
+ #else
294
+ OpenFile *fptr;
295
+ #endif
291
296
 
292
297
  Data_Get_Struct(self, struct buffer, buf);
293
298
  GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
294
299
  rb_io_set_nonblock(fptr);
295
300
 
296
- return INT2NUM(buffer_read_from(buf, fptr->fd));
301
+ return INT2NUM(buffer_read_from(buf, FPTR_TO_FD(fptr)));
297
302
  }
298
303
 
299
304
  /**
@@ -306,13 +311,17 @@ static VALUE Rev_Buffer_read_from(VALUE self, VALUE io) {
306
311
  */
307
312
  static VALUE Rev_Buffer_write_to(VALUE self, VALUE io) {
308
313
  struct buffer *buf;
314
+ #if HAVE_RB_IO_T
309
315
  rb_io_t *fptr;
316
+ #else
317
+ OpenFile *fptr;
318
+ #endif
310
319
 
311
320
  Data_Get_Struct(self, struct buffer, buf);
312
321
  GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
313
322
  rb_io_set_nonblock(fptr);
314
323
 
315
- return INT2NUM(buffer_write_to(buf, fptr->fd));
324
+ return INT2NUM(buffer_write_to(buf, FPTR_TO_FD(fptr)));
316
325
  }
317
326
 
318
327
  /*
@@ -16,10 +16,6 @@ static VALUE mRev = Qnil;
16
16
  void Init_rev_ext()
17
17
  {
18
18
  ev_set_allocator((void *(*)(void *, long))xrealloc);
19
- mRev = rb_define_module("Rev");
20
-
21
- /* Make libev version available in Ruby */
22
- rb_define_const(mRev, "LIBEV_VERSION", rb_sprintf("%d.%d", ev_version_major(), ev_version_minor()));
23
19
 
24
20
  /* Initializers for other modules */
25
21
  Init_rev_loop();
@@ -27,4 +23,9 @@ void Init_rev_ext()
27
23
  Init_rev_io_watcher();
28
24
  Init_rev_timer_watcher();
29
25
  Init_rev_buffer();
26
+ Init_rev_utils();
27
+
28
+ #ifdef HAVE_OPENSSL_SSL_H
29
+ Init_rev_ssl();
30
+ #endif
30
31
  }
@@ -13,13 +13,11 @@
13
13
  #include "rev.h"
14
14
  #include "rev_watcher.h"
15
15
 
16
- /* Module and object handles */
17
16
  static VALUE mRev = Qnil;
18
17
  static VALUE cRev_Watcher = Qnil;
19
18
  static VALUE cRev_Loop = Qnil;
20
19
  static VALUE cRev_IOWatcher = Qnil;
21
20
 
22
- /* Method implementations */
23
21
  static VALUE Rev_IOWatcher_initialize(int argc, VALUE *argv, VALUE self);
24
22
  static VALUE Rev_IOWatcher_attach(VALUE self, VALUE loop);
25
23
  static VALUE Rev_IOWatcher_detach(VALUE self);
@@ -28,7 +26,6 @@ static VALUE Rev_IOWatcher_disable(VALUE self);
28
26
  static VALUE Rev_IOWatcher_on_readable(VALUE self);
29
27
  static VALUE Rev_IOWatcher_on_writable(VALUE self);
30
28
 
31
- /* Callbacks */
32
29
  static void Rev_IOWatcher_libev_callback(struct ev_loop *ev_loop, struct ev_io *io, int revents);
33
30
  static void Rev_IOWatcher_dispatch_callback(VALUE self, int revents);
34
31
 
@@ -66,8 +63,12 @@ static VALUE Rev_IOWatcher_initialize(int argc, VALUE *argv, VALUE self)
66
63
  VALUE io, flags;
67
64
  char *flags_str;
68
65
  int events;
69
- rb_io_t *fptr;
70
66
  struct Rev_Watcher *watcher_data;
67
+ #if HAVE_RB_IO_T
68
+ rb_io_t *fptr;
69
+ #else
70
+ OpenFile *fptr;
71
+ #endif
71
72
 
72
73
  rb_scan_args(argc, argv, "11", &io, &flags);
73
74
 
@@ -85,11 +86,11 @@ static VALUE Rev_IOWatcher_initialize(int argc, VALUE *argv, VALUE self)
85
86
  else
86
87
  rb_raise(rb_eArgError, "invalid event type: '%s' (must be 'r', 'w', or 'rw')", flags_str);
87
88
 
88
- GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
89
89
  Data_Get_Struct(self, struct Rev_Watcher, watcher_data);
90
+ GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
90
91
 
91
92
  watcher_data->dispatch_callback = Rev_IOWatcher_dispatch_callback;
92
- ev_io_init(&watcher_data->event_types.ev_io, Rev_IOWatcher_libev_callback, fptr->fd, events);
93
+ ev_io_init(&watcher_data->event_types.ev_io, Rev_IOWatcher_libev_callback, FPTR_TO_FD(fptr), events);
93
94
  watcher_data->event_types.ev_io.data = (void *)self;
94
95
 
95
96
  return Qnil;
@@ -12,22 +12,19 @@
12
12
 
13
13
  #include "rev.h"
14
14
 
15
- /* Module and object handles */
16
15
  static VALUE mRev = Qnil;
17
16
  static VALUE cRev_Loop = Qnil;
18
17
 
19
- /* Data allocators and deallocators */
20
18
  static VALUE Rev_Loop_allocate(VALUE klass);
21
19
  static void Rev_Loop_mark(struct Rev_Loop *loop);
22
20
  static void Rev_Loop_free(struct Rev_Loop *loop);
23
21
 
24
- /* Method implementations */
25
22
  static VALUE Rev_Loop_initialize(VALUE self);
26
23
  static VALUE Rev_Loop_ev_loop_new(VALUE self, VALUE flags);
27
24
  static VALUE Rev_Loop_run_once(VALUE self);
28
- static VALUE Rev_Loop_run_once_blocking(void *ptr);
29
25
  static VALUE Rev_Loop_run_nonblock(VALUE self);
30
26
 
27
+ static void Rev_Loop_ev_loop_oneshot(struct Rev_Loop *loop_data);
31
28
  static void Rev_Loop_dispatch_events(struct Rev_Loop *loop_data);
32
29
 
33
30
  #define DEFAULT_EVENTBUF_SIZE 32
@@ -184,15 +181,20 @@ static VALUE Rev_Loop_run_once(VALUE self)
184
181
  assert(loop_data->ev_loop && !loop_data->events_received);
185
182
 
186
183
  loop_data->running = 1;
187
- rb_thread_blocking_region(Rev_Loop_run_once_blocking, loop_data->ev_loop, RB_UBF_DFL, 0);
184
+
185
+ Rev_Loop_ev_loop_oneshot(loop_data);
188
186
  Rev_Loop_dispatch_events(loop_data);
189
187
  loop_data->events_received = 0;
188
+
190
189
  loop_data->running = 0;
191
190
 
192
191
  return Qnil;
193
192
  }
194
193
 
195
- static VALUE Rev_Loop_run_once_blocking(void *ptr)
194
+ /* Ruby 1.9 supports blocking system calls through rb_thread_blocking_region() */
195
+ #ifdef HAVE_RB_THREAD_BLOCKING_REGION
196
+ #define HAVE_EV_LOOP_ONESHOT
197
+ static VALUE Rev_Loop_ev_loop_oneshot_blocking(void *ptr)
196
198
  {
197
199
  /* The libev loop has now escaped through the Global VM Lock unscathed! */
198
200
  struct ev_loop *loop = (struct ev_loop *)ptr;
@@ -201,6 +203,46 @@ static VALUE Rev_Loop_run_once_blocking(void *ptr)
201
203
  return Qnil;
202
204
  }
203
205
 
206
+ static void Rev_Loop_ev_loop_oneshot(struct Rev_Loop *loop_data)
207
+ {
208
+ /* Use Ruby 1.9's rb_thread_blocking_region call to make a blocking system call */
209
+ rb_thread_blocking_region(Rev_Loop_ev_loop_oneshot_blocking, loop_data->ev_loop, RB_UBF_DFL, 0);
210
+ }
211
+ #endif
212
+
213
+ /* Ruby 1.8 requires us to periodically run the event loop then defer back to
214
+ * the green threads scheduler */
215
+ #ifndef HAVE_EV_LOOP_ONESHOT
216
+ #define BLOCKING_INTERVAL 0.01 /* Block for 10ms at a time */
217
+
218
+ /* Stub callback */
219
+ static void timer_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents)
220
+ {
221
+ }
222
+
223
+ static void Rev_Loop_ev_loop_oneshot(struct Rev_Loop *loop_data)
224
+ {
225
+ struct ev_timer timer;
226
+ struct timeval tv;
227
+
228
+ /* Set up an ev_timer to unblock the loop every 10ms */
229
+ ev_timer_init(&timer, timer_callback, BLOCKING_INTERVAL, BLOCKING_INTERVAL);
230
+ ev_timer_start(loop_data->ev_loop, &timer);
231
+
232
+ do {
233
+ /* Since blocking calls would hang the Ruby 1.8 thread scheduler, don't block */
234
+ ev_loop(loop_data->ev_loop, EVLOOP_ONESHOT);
235
+
236
+ /* Call rb_thread_select to resume the Ruby scheduler */
237
+ tv.tv_sec = 0;
238
+ tv.tv_usec = 0;
239
+ rb_thread_select(0, NULL, NULL, NULL, &tv);
240
+ } while(!loop_data->events_received);
241
+
242
+ ev_timer_stop(loop_data->ev_loop, &timer);
243
+ }
244
+ #endif
245
+
204
246
  /**
205
247
  * call-seq:
206
248
  * Rev::Loop.run_once -> nil