nio4r 0.2.0 → 2.7.5

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.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/ext/libev/Changes +232 -3
  4. data/ext/libev/LICENSE +2 -1
  5. data/ext/libev/README +11 -10
  6. data/ext/libev/ev.c +2249 -473
  7. data/ext/libev/ev.h +165 -135
  8. data/ext/libev/ev_epoll.c +68 -36
  9. data/ext/libev/ev_iouring.c +694 -0
  10. data/ext/libev/ev_kqueue.c +44 -18
  11. data/ext/libev/ev_linuxaio.c +620 -0
  12. data/ext/libev/ev_poll.c +28 -20
  13. data/ext/libev/ev_port.c +23 -10
  14. data/ext/libev/ev_select.c +18 -12
  15. data/ext/libev/ev_vars.h +65 -19
  16. data/ext/libev/ev_win32.c +16 -7
  17. data/ext/libev/ev_wrap.h +236 -160
  18. data/ext/nio4r/.clang-format +16 -0
  19. data/ext/nio4r/bytebuffer.c +465 -0
  20. data/ext/nio4r/extconf.rb +44 -35
  21. data/ext/nio4r/libev.h +3 -4
  22. data/ext/nio4r/monitor.c +186 -54
  23. data/ext/nio4r/nio4r.h +17 -23
  24. data/ext/nio4r/nio4r_ext.c +11 -3
  25. data/ext/nio4r/org/nio4r/ByteBuffer.java +295 -0
  26. data/ext/nio4r/org/nio4r/Monitor.java +176 -0
  27. data/ext/nio4r/org/nio4r/Nio4r.java +104 -0
  28. data/ext/nio4r/org/nio4r/Selector.java +297 -0
  29. data/ext/nio4r/selector.c +350 -182
  30. data/lib/nio/bytebuffer.rb +235 -0
  31. data/lib/nio/monitor.rb +100 -8
  32. data/lib/nio/selector.rb +110 -44
  33. data/lib/nio/version.rb +8 -1
  34. data/lib/nio.rb +47 -16
  35. data/lib/nio4r.rb +7 -0
  36. data/license.md +80 -0
  37. data/readme.md +91 -0
  38. data/releases.md +343 -0
  39. data.tar.gz.sig +2 -0
  40. metadata +114 -79
  41. metadata.gz.sig +0 -0
  42. data/.gitignore +0 -20
  43. data/.rspec +0 -4
  44. data/.travis.yml +0 -7
  45. data/CHANGES.md +0 -10
  46. data/Gemfile +0 -4
  47. data/LICENSE.txt +0 -20
  48. data/README.md +0 -171
  49. data/Rakefile +0 -9
  50. data/examples/echo_server.rb +0 -38
  51. data/ext/libev/README.embed +0 -3
  52. data/ext/libev/test_libev_win32.c +0 -123
  53. data/lib/nio/jruby/monitor.rb +0 -42
  54. data/lib/nio/jruby/selector.rb +0 -135
  55. data/nio4r.gemspec +0 -22
  56. data/spec/nio/monitor_spec.rb +0 -46
  57. data/spec/nio/selector_spec.rb +0 -269
  58. data/spec/spec_helper.rb +0 -3
  59. data/tasks/extension.rake +0 -10
  60. data/tasks/rspec.rake +0 -7
data/ext/nio4r/selector.c CHANGED
@@ -4,41 +4,57 @@
4
4
  */
5
5
 
6
6
  #include "nio4r.h"
7
+ #ifdef HAVE_RUBYSIG_H
7
8
  #include "rubysig.h"
9
+ #endif
10
+
11
+ #ifdef HAVE_UNISTD_H
12
+ #include <unistd.h>
13
+ #else
14
+ #include <io.h>
15
+ #endif
16
+
17
+ #include <assert.h>
18
+ #include <fcntl.h>
8
19
 
9
20
  static VALUE mNIO = Qnil;
10
- static VALUE cNIO_Channel = Qnil;
11
- static VALUE cNIO_Monitor = Qnil;
21
+ static VALUE cNIO_Monitor = Qnil;
12
22
  static VALUE cNIO_Selector = Qnil;
13
23
 
14
24
  /* Allocator/deallocator */
15
25
  static VALUE NIO_Selector_allocate(VALUE klass);
16
- static void NIO_Selector_mark(struct NIO_Selector *loop);
26
+ static void NIO_Selector_mark(void *data);
17
27
  static void NIO_Selector_shutdown(struct NIO_Selector *selector);
18
- static void NIO_Selector_free(struct NIO_Selector *loop);
28
+ static void NIO_Selector_free(void *data);
29
+ static size_t NIO_Selector_memsize(const void *data);
19
30
 
20
- /* Methods */
21
- static VALUE NIO_Selector_initialize(VALUE self);
31
+ /* Class methods */
32
+ static VALUE NIO_Selector_supported_backends(VALUE klass);
33
+
34
+ /* Instance methods */
35
+ static VALUE NIO_Selector_initialize(int argc, VALUE *argv, VALUE self);
36
+ static VALUE NIO_Selector_backend(VALUE self);
22
37
  static VALUE NIO_Selector_register(VALUE self, VALUE selectable, VALUE interest);
23
38
  static VALUE NIO_Selector_deregister(VALUE self, VALUE io);
24
39
  static VALUE NIO_Selector_is_registered(VALUE self, VALUE io);
25
40
  static VALUE NIO_Selector_select(int argc, VALUE *argv, VALUE self);
26
- static VALUE NIO_Selector_select_each(int argc, VALUE *argv, VALUE self);
27
41
  static VALUE NIO_Selector_wakeup(VALUE self);
28
42
  static VALUE NIO_Selector_close(VALUE self);
29
43
  static VALUE NIO_Selector_closed(VALUE self);
44
+ static VALUE NIO_Selector_is_empty(VALUE self);
30
45
 
31
46
  /* Internal functions */
32
- static VALUE NIO_Selector_synchronize(VALUE self, VALUE (*func)(VALUE *args), VALUE *args);
47
+ static VALUE NIO_Selector_synchronize(VALUE self, VALUE (*func)(VALUE arg), VALUE arg);
33
48
  static VALUE NIO_Selector_unlock(VALUE lock);
34
- static VALUE NIO_Selector_register_synchronized(VALUE *args);
35
- static VALUE NIO_Selector_deregister_synchronized(VALUE *args);
36
- static VALUE NIO_Selector_select_synchronized(VALUE *args);
37
- static VALUE NIO_Selector_select_each_synchronized(VALUE *args);
38
- static int NIO_Selector_fill_ready_buffer(VALUE *args);
39
- static VALUE NIO_Selector_run_evloop(void *ptr);
49
+ static VALUE NIO_Selector_register_synchronized(VALUE arg);
50
+ static VALUE NIO_Selector_deregister_synchronized(VALUE arg);
51
+ static VALUE NIO_Selector_select_synchronized(VALUE arg);
52
+ static VALUE NIO_Selector_close_synchronized(VALUE arg);
53
+ static VALUE NIO_Selector_closed_synchronized(VALUE arg);
54
+
55
+ static int NIO_Selector_run(struct NIO_Selector *selector, VALUE timeout);
40
56
  static void NIO_Selector_timeout_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents);
41
- static void NIO_Selector_wakeup_callback(struct ev_loop *ev_loop, struct ev_async *async, int revents);
57
+ static void NIO_Selector_wakeup_callback(struct ev_loop *ev_loop, struct ev_io *io, int revents);
42
58
 
43
59
  /* Default number of slots in the buffer for selected monitors */
44
60
  #define INITIAL_READY_BUFFER 32
@@ -47,107 +63,270 @@ static void NIO_Selector_wakeup_callback(struct ev_loop *ev_loop, struct ev_asyn
47
63
  #define BUSYWAIT_INTERVAL 0.01
48
64
 
49
65
  /* Selectors wait for events */
50
- void Init_NIO_Selector()
66
+ void Init_NIO_Selector(void)
51
67
  {
52
68
  mNIO = rb_define_module("NIO");
53
- cNIO_Channel = rb_define_class_under(mNIO, "Channel", rb_cObject);
54
- cNIO_Monitor = rb_define_class_under(mNIO, "Monitor", rb_cObject);
55
69
  cNIO_Selector = rb_define_class_under(mNIO, "Selector", rb_cObject);
56
70
  rb_define_alloc_func(cNIO_Selector, NIO_Selector_allocate);
57
71
 
58
- rb_define_method(cNIO_Selector, "initialize", NIO_Selector_initialize, 0);
72
+ rb_define_singleton_method(cNIO_Selector, "backends", NIO_Selector_supported_backends, 0);
73
+ rb_define_method(cNIO_Selector, "initialize", NIO_Selector_initialize, -1);
74
+ rb_define_method(cNIO_Selector, "backend", NIO_Selector_backend, 0);
59
75
  rb_define_method(cNIO_Selector, "register", NIO_Selector_register, 2);
60
76
  rb_define_method(cNIO_Selector, "deregister", NIO_Selector_deregister, 1);
61
77
  rb_define_method(cNIO_Selector, "registered?", NIO_Selector_is_registered, 1);
62
78
  rb_define_method(cNIO_Selector, "select", NIO_Selector_select, -1);
63
- rb_define_method(cNIO_Selector, "select_each", NIO_Selector_select_each, -1);
64
79
  rb_define_method(cNIO_Selector, "wakeup", NIO_Selector_wakeup, 0);
65
80
  rb_define_method(cNIO_Selector, "close", NIO_Selector_close, 0);
66
81
  rb_define_method(cNIO_Selector, "closed?", NIO_Selector_closed, 0);
82
+ rb_define_method(cNIO_Selector, "empty?", NIO_Selector_is_empty, 0);
83
+
84
+ cNIO_Monitor = rb_define_class_under(mNIO, "Monitor", rb_cObject);
67
85
  }
68
86
 
87
+ static const rb_data_type_t NIO_Selector_type = {
88
+ "NIO::Selector",
89
+ {
90
+ NIO_Selector_mark,
91
+ NIO_Selector_free,
92
+ NIO_Selector_memsize,
93
+ },
94
+ 0,
95
+ 0,
96
+ RUBY_TYPED_WB_PROTECTED // Don't free immediately because of shutdown
97
+ };
98
+
69
99
  /* Create the libev event loop and incoming event buffer */
70
100
  static VALUE NIO_Selector_allocate(VALUE klass)
71
101
  {
72
- struct NIO_Selector *selector = (struct NIO_Selector *)xmalloc(sizeof(struct NIO_Selector));
102
+ struct NIO_Selector *selector;
103
+ int fds[2];
104
+
105
+ /* Use a pipe to implement the wakeup mechanism. I know libev provides
106
+ async watchers that implement this same behavior, but I'm getting
107
+ segvs trying to use that between threads, despite claims of thread
108
+ safety. Pipes are nice and safe to use between threads.
109
+
110
+ Note that Java NIO uses this same mechanism */
111
+ if (pipe(fds) < 0) {
112
+ rb_sys_fail("pipe");
113
+ }
114
+
115
+ /* Use non-blocking reads/writes during wakeup, in case the buffer is full */
116
+ if (fcntl(fds[0], F_SETFL, O_NONBLOCK) < 0 || fcntl(fds[1], F_SETFL, O_NONBLOCK) < 0) {
117
+ rb_sys_fail("fcntl");
118
+ }
119
+
120
+ VALUE obj = TypedData_Make_Struct(klass, struct NIO_Selector, &NIO_Selector_type, selector);
121
+ /* Defer initializing the loop to #initialize */
122
+ selector->ev_loop = 0;
73
123
 
74
- selector->ev_loop = ev_loop_new(0);
75
124
  ev_init(&selector->timer, NIO_Selector_timeout_callback);
76
125
 
77
- ev_async_init(&selector->wakeup, NIO_Selector_wakeup_callback);
78
- selector->wakeup.data = (void *)selector;
126
+ selector->wakeup_reader = fds[0];
127
+ selector->wakeup_writer = fds[1];
79
128
 
80
- ev_async_start(selector->ev_loop, &selector->wakeup);
129
+ ev_io_init(&selector->wakeup, NIO_Selector_wakeup_callback, selector->wakeup_reader, EV_READ);
130
+ selector->wakeup.data = (void *)selector;
81
131
 
82
- selector->closed = selector->selecting = selector->ready_count = 0;
83
- selector->ready_buffer_size = INITIAL_READY_BUFFER;
84
- selector->ready_buffer = (VALUE *)xmalloc(sizeof(VALUE) * INITIAL_READY_BUFFER);
132
+ selector->closed = selector->selecting = selector->wakeup_fired = selector->ready_count = 0;
133
+ RB_OBJ_WRITE(obj, &selector->ready_array, Qnil);
134
+ return obj;
135
+ }
85
136
 
86
- return Data_Wrap_Struct(klass, NIO_Selector_mark, NIO_Selector_free, selector);
137
+ struct NIO_Selector *NIO_Selector_unwrap(VALUE self)
138
+ {
139
+ struct NIO_Selector *selector;
140
+ TypedData_Get_Struct(self, struct NIO_Selector, &NIO_Selector_type, selector);
141
+ return selector;
87
142
  }
88
143
 
89
144
  /* NIO selectors store all Ruby objects in instance variables so mark is a stub */
90
- static void NIO_Selector_mark(struct NIO_Selector *selector)
145
+ static void NIO_Selector_mark(void *data)
91
146
  {
147
+ struct NIO_Selector *selector = (struct NIO_Selector *)data;
148
+ if (selector->ready_array != Qnil) {
149
+ rb_gc_mark(selector->ready_array);
150
+ }
92
151
  }
93
152
 
94
153
  /* Free a Selector's system resources.
95
154
  Called by both NIO::Selector#close and the finalizer below */
96
155
  static void NIO_Selector_shutdown(struct NIO_Selector *selector)
97
156
  {
98
- if(selector->ev_loop) {
99
- ev_loop_destroy(selector->ev_loop);
100
- selector->ev_loop = 0;
157
+ if (selector->closed) {
158
+ return;
101
159
  }
102
160
 
103
- if(selector->closed) {
104
- return;
161
+ close(selector->wakeup_reader);
162
+ close(selector->wakeup_writer);
163
+
164
+ if (selector->ev_loop) {
165
+ ev_loop_destroy(selector->ev_loop);
166
+ selector->ev_loop = 0;
105
167
  }
106
168
 
107
169
  selector->closed = 1;
108
170
  }
109
171
 
110
172
  /* Ruby finalizer for selector objects */
111
- static void NIO_Selector_free(struct NIO_Selector *selector)
173
+ static void NIO_Selector_free(void *data)
112
174
  {
175
+ struct NIO_Selector *selector = (struct NIO_Selector *)data;
113
176
  NIO_Selector_shutdown(selector);
114
-
115
- xfree(selector->ready_buffer);
116
177
  xfree(selector);
117
178
  }
118
179
 
180
+ static size_t NIO_Selector_memsize(const void *data)
181
+ {
182
+ return sizeof(struct NIO_Selector);
183
+ }
184
+
185
+ /* Return an array of symbols for supported backends */
186
+ static VALUE NIO_Selector_supported_backends(VALUE klass)
187
+ {
188
+ unsigned int backends = ev_supported_backends();
189
+ VALUE result = rb_ary_new();
190
+
191
+ if (backends & EVBACKEND_EPOLL) {
192
+ rb_ary_push(result, ID2SYM(rb_intern("epoll")));
193
+ }
194
+
195
+ if (backends & EVBACKEND_POLL) {
196
+ rb_ary_push(result, ID2SYM(rb_intern("poll")));
197
+ }
198
+
199
+ if (backends & EVBACKEND_KQUEUE) {
200
+ rb_ary_push(result, ID2SYM(rb_intern("kqueue")));
201
+ }
202
+
203
+ if (backends & EVBACKEND_SELECT) {
204
+ rb_ary_push(result, ID2SYM(rb_intern("select")));
205
+ }
206
+
207
+ if (backends & EVBACKEND_PORT) {
208
+ rb_ary_push(result, ID2SYM(rb_intern("port")));
209
+ }
210
+
211
+ if (backends & EVBACKEND_LINUXAIO) {
212
+ rb_ary_push(result, ID2SYM(rb_intern("linuxaio")));
213
+ }
214
+
215
+ if (backends & EVBACKEND_IOURING) {
216
+ rb_ary_push(result, ID2SYM(rb_intern("io_uring")));
217
+ }
218
+
219
+ return result;
220
+ }
221
+
119
222
  /* Create a new selector. This is more or less the pure Ruby version
120
223
  translated into an MRI cext */
121
- static VALUE NIO_Selector_initialize(VALUE self)
224
+ static VALUE NIO_Selector_initialize(int argc, VALUE *argv, VALUE self)
122
225
  {
226
+ ID backend_id;
227
+ VALUE backend;
123
228
  VALUE lock;
124
229
 
230
+ struct NIO_Selector *selector;
231
+ unsigned int flags = 0;
232
+
233
+ TypedData_Get_Struct(self, struct NIO_Selector, &NIO_Selector_type, selector);
234
+
235
+ rb_scan_args(argc, argv, "01", &backend);
236
+
237
+ if (backend != Qnil) {
238
+ if (!rb_ary_includes(NIO_Selector_supported_backends(CLASS_OF(self)), backend)) {
239
+ rb_raise(rb_eArgError, "unsupported backend: %s", RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0)));
240
+ }
241
+
242
+ backend_id = SYM2ID(backend);
243
+
244
+ if (backend_id == rb_intern("epoll")) {
245
+ flags = EVBACKEND_EPOLL;
246
+ } else if (backend_id == rb_intern("poll")) {
247
+ flags = EVBACKEND_POLL;
248
+ } else if (backend_id == rb_intern("kqueue")) {
249
+ flags = EVBACKEND_KQUEUE;
250
+ } else if (backend_id == rb_intern("select")) {
251
+ flags = EVBACKEND_SELECT;
252
+ } else if (backend_id == rb_intern("port")) {
253
+ flags = EVBACKEND_PORT;
254
+ } else if (backend_id == rb_intern("linuxaio")) {
255
+ flags = EVBACKEND_LINUXAIO;
256
+ } else if (backend_id == rb_intern("io_uring")) {
257
+ flags = EVBACKEND_IOURING;
258
+ } else {
259
+ rb_raise(rb_eArgError, "unsupported backend: %s", RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0)));
260
+ }
261
+ }
262
+
263
+ /* Ensure the selector loop has not yet been initialized */
264
+ assert(!selector->ev_loop);
265
+
266
+ selector->ev_loop = ev_loop_new(flags);
267
+ if (!selector->ev_loop) {
268
+ rb_raise(rb_eIOError, "error initializing event loop");
269
+ }
270
+
271
+ ev_io_start(selector->ev_loop, &selector->wakeup);
272
+
125
273
  rb_ivar_set(self, rb_intern("selectables"), rb_hash_new());
274
+ rb_ivar_set(self, rb_intern("lock_holder"), Qnil);
126
275
 
127
276
  lock = rb_class_new_instance(0, 0, rb_const_get(rb_cObject, rb_intern("Mutex")));
128
277
  rb_ivar_set(self, rb_intern("lock"), lock);
278
+ rb_ivar_set(self, rb_intern("lock_holder"), Qnil);
129
279
 
130
280
  return Qnil;
131
281
  }
132
282
 
283
+ static VALUE NIO_Selector_backend(VALUE self)
284
+ {
285
+ struct NIO_Selector *selector;
286
+
287
+ TypedData_Get_Struct(self, struct NIO_Selector, &NIO_Selector_type, selector);
288
+ if (selector->closed) {
289
+ rb_raise(rb_eIOError, "selector is closed");
290
+ }
291
+
292
+ switch (ev_backend(selector->ev_loop)) {
293
+ case EVBACKEND_EPOLL:
294
+ return ID2SYM(rb_intern("epoll"));
295
+ case EVBACKEND_POLL:
296
+ return ID2SYM(rb_intern("poll"));
297
+ case EVBACKEND_KQUEUE:
298
+ return ID2SYM(rb_intern("kqueue"));
299
+ case EVBACKEND_SELECT:
300
+ return ID2SYM(rb_intern("select"));
301
+ case EVBACKEND_PORT:
302
+ return ID2SYM(rb_intern("port"));
303
+ case EVBACKEND_LINUXAIO:
304
+ return ID2SYM(rb_intern("linuxaio"));
305
+ case EVBACKEND_IOURING:
306
+ return ID2SYM(rb_intern("io_uring"));
307
+ }
308
+
309
+ return ID2SYM(rb_intern("unknown"));
310
+ }
311
+
133
312
  /* Synchronize around a reentrant selector lock */
134
- static VALUE NIO_Selector_synchronize(VALUE self, VALUE (*func)(VALUE *args), VALUE *args)
313
+ static VALUE NIO_Selector_synchronize(VALUE self, VALUE (*func)(VALUE arg), VALUE arg)
135
314
  {
136
315
  VALUE current_thread, lock_holder, lock;
137
316
 
138
317
  current_thread = rb_thread_current();
139
318
  lock_holder = rb_ivar_get(self, rb_intern("lock_holder"));
140
319
 
141
- if(lock_holder != rb_thread_current()) {
320
+ if (lock_holder != current_thread) {
142
321
  lock = rb_ivar_get(self, rb_intern("lock"));
143
- rb_funcall(lock, rb_intern("lock"), 0, 0);
144
- rb_ivar_set(self, rb_intern("lock_holder"), rb_thread_current());
322
+ rb_funcall(lock, rb_intern("lock"), 0);
323
+ rb_ivar_set(self, rb_intern("lock_holder"), current_thread);
145
324
 
146
325
  /* We've acquired the lock, so ensure we unlock it */
147
- return rb_ensure(func, (VALUE)args, NIO_Selector_unlock, self);
326
+ return rb_ensure(func, (VALUE)arg, NIO_Selector_unlock, self);
148
327
  } else {
149
328
  /* We already hold the selector lock, so no need to unlock it */
150
- func(args);
329
+ return func(arg);
151
330
  }
152
331
  }
153
332
 
@@ -159,39 +338,48 @@ static VALUE NIO_Selector_unlock(VALUE self)
159
338
  rb_ivar_set(self, rb_intern("lock_holder"), Qnil);
160
339
 
161
340
  lock = rb_ivar_get(self, rb_intern("lock"));
162
- rb_funcall(lock, rb_intern("unlock"), 0, 0);
341
+ rb_funcall(lock, rb_intern("unlock"), 0);
342
+
343
+ return Qnil;
163
344
  }
164
345
 
165
346
  /* Register an IO object with the selector for the given interests */
166
347
  static VALUE NIO_Selector_register(VALUE self, VALUE io, VALUE interests)
167
348
  {
168
349
  VALUE args[3] = {self, io, interests};
169
- return NIO_Selector_synchronize(self, NIO_Selector_register_synchronized, args);
350
+ return NIO_Selector_synchronize(self, NIO_Selector_register_synchronized, (VALUE)args);
170
351
  }
171
352
 
172
353
  /* Internal implementation of register after acquiring mutex */
173
- static VALUE NIO_Selector_register_synchronized(VALUE *args)
354
+ static VALUE NIO_Selector_register_synchronized(VALUE _args)
174
355
  {
175
356
  VALUE self, io, interests, selectables, monitor;
176
357
  VALUE monitor_args[3];
358
+ struct NIO_Selector *selector;
177
359
 
360
+ VALUE *args = (VALUE *)_args;
178
361
  self = args[0];
179
362
  io = args[1];
180
363
  interests = args[2];
181
364
 
365
+ TypedData_Get_Struct(self, struct NIO_Selector, &NIO_Selector_type, selector);
366
+ if (selector->closed) {
367
+ rb_raise(rb_eIOError, "selector is closed");
368
+ }
369
+
182
370
  selectables = rb_ivar_get(self, rb_intern("selectables"));
183
371
  monitor = rb_hash_lookup(selectables, io);
184
372
 
185
- if(monitor != Qnil)
373
+ if (monitor != Qnil)
186
374
  rb_raise(rb_eArgError, "this IO is already registered with selector");
187
375
 
188
376
  /* Create a new NIO::Monitor */
189
- monitor_args[0] = self;
190
- monitor_args[1] = io;
191
- monitor_args[2] = interests;
377
+ monitor_args[0] = io;
378
+ monitor_args[1] = interests;
379
+ monitor_args[2] = self;
192
380
 
193
381
  monitor = rb_class_new_instance(3, monitor_args, cNIO_Monitor);
194
- rb_hash_aset(selectables, io, monitor);
382
+ rb_hash_aset(selectables, rb_funcall(monitor, rb_intern("io"), 0), monitor);
195
383
 
196
384
  return monitor;
197
385
  }
@@ -200,23 +388,23 @@ static VALUE NIO_Selector_register_synchronized(VALUE *args)
200
388
  static VALUE NIO_Selector_deregister(VALUE self, VALUE io)
201
389
  {
202
390
  VALUE args[2] = {self, io};
203
- return NIO_Selector_synchronize(self, NIO_Selector_deregister_synchronized, args);
391
+ return NIO_Selector_synchronize(self, NIO_Selector_deregister_synchronized, (VALUE)args);
204
392
  }
205
393
 
206
394
  /* Internal implementation of register after acquiring mutex */
207
- static VALUE NIO_Selector_deregister_synchronized(VALUE *args)
395
+ static VALUE NIO_Selector_deregister_synchronized(VALUE _args)
208
396
  {
209
- VALUE self, io, interests, selectables, monitor;
210
- VALUE monitor_args[3];
397
+ VALUE self, io, selectables, monitor;
211
398
 
399
+ VALUE *args = (VALUE *)_args;
212
400
  self = args[0];
213
401
  io = args[1];
214
402
 
215
403
  selectables = rb_ivar_get(self, rb_intern("selectables"));
216
404
  monitor = rb_hash_delete(selectables, io);
217
405
 
218
- if(monitor != Qnil) {
219
- rb_funcall(monitor, rb_intern("close"), 0, 0);
406
+ if (monitor != Qnil) {
407
+ rb_funcall(monitor, rb_intern("close"), 1, Qfalse);
220
408
  }
221
409
 
222
410
  return monitor;
@@ -234,165 +422,122 @@ static VALUE NIO_Selector_is_registered(VALUE self, VALUE io)
234
422
  /* Select from all registered IO objects */
235
423
  static VALUE NIO_Selector_select(int argc, VALUE *argv, VALUE self)
236
424
  {
237
- VALUE timeout, array;
238
- VALUE args[2];
425
+ VALUE timeout;
239
426
 
240
427
  rb_scan_args(argc, argv, "01", &timeout);
241
428
 
242
- args[0] = self;
243
- args[1] = timeout;
244
-
245
- return NIO_Selector_synchronize(self, NIO_Selector_select_synchronized, args);
246
- }
247
-
248
- /* Select from all registered IO objects */
249
- static VALUE NIO_Selector_select_each(int argc, VALUE *argv, VALUE self)
250
- {
251
- VALUE timeout, array;
252
- VALUE args[2];
253
-
254
- if(!rb_block_given_p()) {
255
- rb_raise(rb_eArgError, "no block given");
429
+ if (timeout != Qnil && NUM2DBL(timeout) < 0) {
430
+ rb_raise(rb_eArgError, "time interval must be positive");
256
431
  }
257
432
 
258
- rb_scan_args(argc, argv, "01", &timeout);
259
-
260
- args[0] = self;
261
- args[1] = timeout;
262
-
263
- return NIO_Selector_synchronize(self, NIO_Selector_select_each_synchronized, args);
433
+ VALUE args[2] = {self, timeout};
434
+ return NIO_Selector_synchronize(self, NIO_Selector_select_synchronized, (VALUE)args);
264
435
  }
265
436
 
266
437
  /* Internal implementation of select with the selector lock held */
267
- static VALUE NIO_Selector_select_synchronized(VALUE *args)
438
+ static VALUE NIO_Selector_select_synchronized(VALUE _args)
268
439
  {
440
+ int ready;
441
+ VALUE ready_array;
269
442
  struct NIO_Selector *selector;
270
- int ready = NIO_Selector_fill_ready_buffer(args);
271
443
 
272
- Data_Get_Struct(args[0], struct NIO_Selector, selector);
444
+ VALUE *args = (VALUE *)_args;
273
445
 
274
- if(ready > 0) {
275
- return rb_ary_new4(ready, selector->ready_buffer);
276
- } else {
277
- return Qnil;
446
+ TypedData_Get_Struct(args[0], struct NIO_Selector, &NIO_Selector_type, selector);
447
+
448
+ if (selector->closed) {
449
+ rb_raise(rb_eIOError, "selector is closed");
278
450
  }
279
- }
280
451
 
281
- /* Internal implementation of select with the selector lock held */
282
- static VALUE NIO_Selector_select_each_synchronized(VALUE *args)
283
- {
284
- struct NIO_Selector *selector;
285
- int i, ready = NIO_Selector_fill_ready_buffer(args);
452
+ if (!rb_block_given_p()) {
453
+ RB_OBJ_WRITE(args[0], &selector->ready_array, rb_ary_new());
454
+ }
286
455
 
287
- Data_Get_Struct(args[0], struct NIO_Selector, selector);
456
+ ready = NIO_Selector_run(selector, args[1]);
288
457
 
289
- if(ready > 0) {
290
- for(i = 0; i < ready; i++) {
291
- rb_yield(selector->ready_buffer[i]);
458
+ /* Timeout */
459
+ if (ready < 0) {
460
+ if (!rb_block_given_p()) {
461
+ RB_OBJ_WRITE(args[0], &selector->ready_array, Qnil);
292
462
  }
293
463
 
464
+ return Qnil;
465
+ }
466
+
467
+ if (rb_block_given_p()) {
294
468
  return INT2NUM(ready);
295
469
  } else {
296
- return Qnil;
470
+ ready_array = selector->ready_array;
471
+ RB_OBJ_WRITE(args[0], &selector->ready_array, Qnil);
472
+ return ready_array;
297
473
  }
298
474
  }
299
475
 
300
- static int NIO_Selector_fill_ready_buffer(VALUE *args)
476
+ static int NIO_Selector_run(struct NIO_Selector *selector, VALUE timeout)
301
477
  {
302
- VALUE self, timeout;
303
- struct NIO_Selector *selector;
478
+ int ev_run_flags = EVRUN_ONCE;
304
479
  int result;
480
+ double timeout_val;
305
481
 
306
- self = args[0];
307
- timeout = args[1];
308
-
309
- Data_Get_Struct(self, struct NIO_Selector, selector);
310
482
  selector->selecting = 1;
483
+ selector->wakeup_fired = 0;
311
484
 
312
- #if defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_ALONE)
313
- /* Implement the optional timeout (if any) as a ev_timer */
314
- if(timeout != Qnil) {
315
- selector->timer.repeat = NUM2DBL(timeout);
316
- ev_timer_again(selector->ev_loop, &selector->timer);
317
- } else {
485
+ if (timeout == Qnil) {
486
+ /* Don't fire a wakeup timeout if we weren't passed one */
318
487
  ev_timer_stop(selector->ev_loop, &selector->timer);
319
- }
320
- #else
321
- /* Store when we started the loop so we can calculate the timeout */
322
- ev_tstamp started_at = ev_now(selector->ev_loop);
323
- #endif
324
-
325
- #if defined(HAVE_RB_THREAD_BLOCKING_REGION)
326
- /* Ruby 1.9 lets us release the GIL and make a blocking I/O call */
327
- rb_thread_blocking_region(NIO_Selector_run_evloop, selector, RUBY_UBF_IO, 0);
328
- #elif defined(HAVE_RB_THREAD_ALONE)
329
- /* If we're the only thread we can make a blocking system call */
330
- if(rb_thread_alone()) {
331
- #else
332
- /* If we don't have rb_thread_alone() we can't block */
333
- if(0) {
334
- #endif /* defined(HAVE_RB_THREAD_BLOCKING_REGION) */
335
-
336
- #if !defined(HAVE_RB_THREAD_BLOCKING_REGION)
337
- TRAP_BEG;
338
- NIO_Selector_run_evloop(selector);
339
- TRAP_END;
340
488
  } else {
341
- /* We need to busy wait as not to stall the green thread scheduler
342
- Ruby 1.8: just say no! :( */
343
- ev_timer_init(&selector->timer, NIO_Selector_timeout_callback, BUSYWAIT_INTERVAL, BUSYWAIT_INTERVAL);
344
- ev_timer_start(selector->ev_loop, &selector->timer);
345
-
346
- /* Loop until we receive events */
347
- while(selector->selecting && !selector->ready_count) {
348
- TRAP_BEG;
349
- NIO_Selector_run_evloop(selector);
350
- TRAP_END;
351
-
352
- /* Run the next green thread */
353
- rb_thread_schedule();
354
-
355
- /* Break if the timeout has elapsed */
356
- if(timeout != Qnil && ev_now(selector->ev_loop) - started_at >= NUM2DBL(timeout))
357
- break;
489
+ timeout_val = NUM2DBL(timeout);
490
+ if (timeout_val == 0) {
491
+ /* If we've been given an explicit timeout of 0, perform a non-blocking
492
+ select operation */
493
+ ev_run_flags = EVRUN_NOWAIT;
494
+ } else {
495
+ selector->timer.repeat = timeout_val;
496
+ ev_timer_again(selector->ev_loop, &selector->timer);
358
497
  }
359
-
360
- ev_timer_stop(selector->ev_loop, &selector->timer);
361
498
  }
362
- #endif /* defined(HAVE_RB_THREAD_BLOCKING_REGION) */
499
+
500
+ /* libev is patched to release the GIL when it makes its system call */
501
+ ev_run(selector->ev_loop, ev_run_flags);
363
502
 
364
503
  result = selector->ready_count;
365
504
  selector->selecting = selector->ready_count = 0;
366
505
 
367
- return result;
368
- }
369
-
370
- /* Run the libev event loop */
371
- static VALUE NIO_Selector_run_evloop(void *ptr)
372
- {
373
- struct NIO_Selector *selector = (struct NIO_Selector *)ptr;
374
-
375
- ev_loop(selector->ev_loop, EVLOOP_ONESHOT);
376
-
377
- return Qnil;
506
+ if (result > 0 || selector->wakeup_fired) {
507
+ selector->wakeup_fired = 0;
508
+ return result;
509
+ } else {
510
+ return -1;
511
+ }
378
512
  }
379
513
 
380
514
  /* Wake the selector up from another thread */
381
515
  static VALUE NIO_Selector_wakeup(VALUE self)
382
516
  {
383
517
  struct NIO_Selector *selector;
384
- Data_Get_Struct(self, struct NIO_Selector, selector);
518
+ TypedData_Get_Struct(self, struct NIO_Selector, &NIO_Selector_type, selector);
519
+
520
+ if (selector->closed) {
521
+ rb_raise(rb_eIOError, "selector is closed");
522
+ }
385
523
 
386
- ev_async_send(selector->ev_loop, &selector->wakeup);
524
+ selector->wakeup_fired = 1;
525
+ write(selector->wakeup_writer, "\0", 1);
387
526
 
388
527
  return Qnil;
389
528
  }
390
529
 
391
530
  /* Close the selector and free system resources */
392
531
  static VALUE NIO_Selector_close(VALUE self)
532
+ {
533
+ return NIO_Selector_synchronize(self, NIO_Selector_close_synchronized, self);
534
+ }
535
+
536
+ static VALUE NIO_Selector_close_synchronized(VALUE self)
393
537
  {
394
538
  struct NIO_Selector *selector;
395
- Data_Get_Struct(self, struct NIO_Selector, selector);
539
+
540
+ TypedData_Get_Struct(self, struct NIO_Selector, &NIO_Selector_type, selector);
396
541
 
397
542
  NIO_Selector_shutdown(selector);
398
543
 
@@ -401,38 +546,61 @@ static VALUE NIO_Selector_close(VALUE self)
401
546
 
402
547
  /* Is the selector closed? */
403
548
  static VALUE NIO_Selector_closed(VALUE self)
549
+ {
550
+ return NIO_Selector_synchronize(self, NIO_Selector_closed_synchronized, self);
551
+ }
552
+
553
+ static VALUE NIO_Selector_closed_synchronized(VALUE self)
404
554
  {
405
555
  struct NIO_Selector *selector;
406
- Data_Get_Struct(self, struct NIO_Selector, selector);
556
+
557
+ TypedData_Get_Struct(self, struct NIO_Selector, &NIO_Selector_type, selector);
407
558
 
408
559
  return selector->closed ? Qtrue : Qfalse;
409
560
  }
410
561
 
562
+ /* True if there are monitors on the loop */
563
+ static VALUE NIO_Selector_is_empty(VALUE self)
564
+ {
565
+ VALUE selectables = rb_ivar_get(self, rb_intern("selectables"));
566
+
567
+ return rb_funcall(selectables, rb_intern("empty?"), 0) == Qtrue ? Qtrue : Qfalse;
568
+ }
569
+
411
570
  /* Called whenever a timeout fires on the event loop */
412
571
  static void NIO_Selector_timeout_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents)
413
572
  {
414
- /* We don't actually need to do anything here, the mere firing of the
415
- timer is sufficient to interrupt the selector. However, libev still wants a callback */
416
573
  }
417
574
 
418
575
  /* Called whenever a wakeup request is sent to a selector */
419
- static void NIO_Selector_wakeup_callback(struct ev_loop *ev_loop, struct ev_async *async, int revents)
576
+ static void NIO_Selector_wakeup_callback(struct ev_loop *ev_loop, struct ev_io *io, int revents)
420
577
  {
421
- struct NIO_Selector *selector = (struct NIO_Selector *)async->data;
578
+ char buffer[128];
579
+ struct NIO_Selector *selector = (struct NIO_Selector *)io->data;
422
580
  selector->selecting = 0;
581
+
582
+ /* Drain the wakeup pipe, giving us level-triggered behavior */
583
+ while (read(selector->wakeup_reader, buffer, 128) > 0)
584
+ ;
423
585
  }
424
586
 
425
- /* This gets called from individual monitors. We must be careful here because
426
- the GIL isn't held, so we must rely only on standard C and can't touch
427
- anything Ruby-related */
428
- void NIO_Selector_handle_event(struct NIO_Selector *selector, VALUE monitor, int revents)
587
+ /* libev callback fired whenever a monitor gets an event */
588
+ void NIO_Selector_monitor_callback(struct ev_loop *ev_loop, struct ev_io *io, int revents)
429
589
  {
430
- /* Grow the ready buffer if it's too small */
431
- if(selector->ready_count >= selector->ready_buffer_size) {
432
- selector->ready_buffer_size *= 2;
433
- selector->ready_buffer = (VALUE *)xrealloc(selector->ready_buffer, sizeof(VALUE) * selector->ready_buffer_size);
434
- }
590
+ struct NIO_Monitor *monitor_data = (struct NIO_Monitor *)io->data;
591
+ struct NIO_Selector *selector = monitor_data->selector;
592
+ VALUE monitor = monitor_data->self;
435
593
 
436
- selector->ready_buffer[selector->ready_count] = monitor;
594
+ assert(monitor_data->interests != 0);
595
+
596
+ assert(selector != 0);
437
597
  selector->ready_count++;
598
+ monitor_data->revents = revents;
599
+
600
+ if (rb_block_given_p()) {
601
+ rb_yield(monitor);
602
+ } else {
603
+ assert(selector->ready_array != Qnil);
604
+ rb_ary_push(selector->ready_array, monitor);
605
+ }
438
606
  }