nio4r 2.5.2-java → 2.5.7-java

Sign up to get free protection for your applications and to get access to all the features.
data/ext/nio4r/nio4r.h CHANGED
@@ -6,12 +6,11 @@
6
6
  #ifndef NIO4R_H
7
7
  #define NIO4R_H
8
8
 
9
+ #include "libev.h"
9
10
  #include "ruby.h"
10
11
  #include "ruby/io.h"
11
- #include "libev.h"
12
12
 
13
- struct NIO_Selector
14
- {
13
+ struct NIO_Selector {
15
14
  struct ev_loop *ev_loop;
16
15
  struct ev_timer timer; /* for timeouts */
17
16
  struct ev_io wakeup;
@@ -24,31 +23,27 @@ struct NIO_Selector
24
23
  VALUE ready_array;
25
24
  };
26
25
 
27
- struct NIO_callback_data
28
- {
26
+ struct NIO_callback_data {
29
27
  VALUE *monitor;
30
28
  struct NIO_Selector *selector;
31
29
  };
32
30
 
33
- struct NIO_Monitor
34
- {
31
+ struct NIO_Monitor {
35
32
  VALUE self;
36
33
  int interests, revents;
37
34
  struct ev_io ev_io;
38
35
  struct NIO_Selector *selector;
39
36
  };
40
37
 
41
- struct NIO_ByteBuffer
42
- {
38
+ struct NIO_ByteBuffer {
43
39
  char *buffer;
44
40
  int position, limit, capacity, mark;
45
41
  };
46
42
 
47
-
48
43
  #ifdef GetReadFile
49
- # define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
44
+ #define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
50
45
  #else
51
- # define FPTR_TO_FD(fptr) fptr->fd
46
+ #define FPTR_TO_FD(fptr) fptr->fd
52
47
  #endif /* GetReadFile */
53
48
 
54
49
  /* Thunk between libev callbacks in NIO::Monitors and NIO::Selectors */
@@ -1,6 +1,7 @@
1
1
  package org.nio4r;
2
2
 
3
3
  import java.io.IOException;
4
+ import java.io.Serializable;
4
5
  import java.nio.channels.Channel;
5
6
  import java.nio.channels.SelectableChannel;
6
7
  import java.nio.channels.ReadableByteChannel;
@@ -25,6 +26,7 @@ import org.jruby.runtime.Block;
25
26
  created by Upekshej
26
27
  */
27
28
  public class ByteBuffer extends RubyObject {
29
+ private static final long serialVersionUID = -6903439483039149324L;
28
30
  private java.nio.ByteBuffer byteBuffer;
29
31
 
30
32
  public static RaiseException newOverflowError(ThreadContext context, String message) {
@@ -13,6 +13,7 @@ import org.jruby.runtime.ThreadContext;
13
13
  import org.jruby.runtime.builtin.IRubyObject;
14
14
 
15
15
  public class Monitor extends RubyObject {
16
+ private static final long serialVersionUID = -3733782997115074794L;
16
17
  private SelectionKey key;
17
18
  private RubyIO io;
18
19
  private IRubyObject interests, selector, value, closed;
@@ -7,7 +7,6 @@ import java.io.IOException;
7
7
  import java.nio.channels.Channel;
8
8
  import java.nio.channels.SelectableChannel;
9
9
  import java.nio.channels.SelectionKey;
10
- import java.nio.channels.CancelledKeyException;
11
10
 
12
11
  import org.jruby.Ruby;
13
12
  import org.jruby.RubyArray;
@@ -21,9 +20,8 @@ import org.jruby.runtime.ThreadContext;
21
20
  import org.jruby.runtime.builtin.IRubyObject;
22
21
  import org.jruby.util.io.OpenFile;
23
22
 
24
- import org.nio4r.Monitor;
25
-
26
23
  public class Selector extends RubyObject {
24
+ private static final long serialVersionUID = -14562818539414873L;
27
25
  private java.nio.channels.Selector selector;
28
26
  private HashMap<SelectableChannel,SelectionKey> cancelledKeys;
29
27
  private volatile boolean wakeupFired;
@@ -45,7 +43,7 @@ public class Selector extends RubyObject {
45
43
 
46
44
  @JRubyMethod
47
45
  public IRubyObject initialize(ThreadContext context, IRubyObject backend) {
48
- if(backend != context.runtime.newSymbol("java")) {
46
+ if(backend != context.runtime.newSymbol("java") && !backend.isNil()) {
49
47
  throw context.runtime.newArgumentError(":java is the only supported backend");
50
48
  }
51
49
 
@@ -203,15 +201,15 @@ public class Selector extends RubyObject {
203
201
  return context.nil;
204
202
  }
205
203
 
206
- RubyArray array = null;
204
+ RubyArray<?> array = null;
207
205
 
208
206
  if(!block.isGiven()) {
209
207
  array = runtime.newArray(this.selector.selectedKeys().size());
210
208
  }
211
209
 
212
- Iterator selectedKeys = this.selector.selectedKeys().iterator();
210
+ Iterator<SelectionKey> selectedKeys = this.selector.selectedKeys().iterator();
213
211
  while(selectedKeys.hasNext()) {
214
- SelectionKey key = (SelectionKey)selectedKeys.next();
212
+ SelectionKey key = selectedKeys.next();
215
213
  processKey(key);
216
214
 
217
215
  selectedKeys.remove();
@@ -263,10 +261,10 @@ public class Selector extends RubyObject {
263
261
 
264
262
  /* Flush our internal buffer of cancelled keys */
265
263
  private void cancelKeys() {
266
- Iterator cancelledKeys = this.cancelledKeys.entrySet().iterator();
264
+ Iterator<Map.Entry<SelectableChannel, SelectionKey>> cancelledKeys = this.cancelledKeys.entrySet().iterator();
267
265
  while(cancelledKeys.hasNext()) {
268
- Map.Entry entry = (Map.Entry)cancelledKeys.next();
269
- SelectionKey key = (SelectionKey)entry.getValue();
266
+ Map.Entry<SelectableChannel, SelectionKey> entry = cancelledKeys.next();
267
+ SelectionKey key = entry.getValue();
270
268
  key.cancel();
271
269
  cancelledKeys.remove();
272
270
  }
data/ext/nio4r/selector.c CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  #include "nio4r.h"
7
7
  #ifdef HAVE_RUBYSIG_H
8
- # include "rubysig.h"
8
+ #include "rubysig.h"
9
9
  #endif
10
10
 
11
11
  #ifdef HAVE_UNISTD_H
@@ -14,11 +14,11 @@
14
14
  #include <io.h>
15
15
  #endif
16
16
 
17
- #include <fcntl.h>
18
17
  #include <assert.h>
18
+ #include <fcntl.h>
19
19
 
20
20
  static VALUE mNIO = Qnil;
21
- static VALUE cNIO_Monitor = Qnil;
21
+ static VALUE cNIO_Monitor = Qnil;
22
22
  static VALUE cNIO_Selector = Qnil;
23
23
 
24
24
  /* Allocator/deallocator */
@@ -80,7 +80,7 @@ void Init_NIO_Selector()
80
80
  rb_define_method(cNIO_Selector, "closed?", NIO_Selector_closed, 0);
81
81
  rb_define_method(cNIO_Selector, "empty?", NIO_Selector_is_empty, 0);
82
82
 
83
- cNIO_Monitor = rb_define_class_under(mNIO, "Monitor", rb_cObject);
83
+ cNIO_Monitor = rb_define_class_under(mNIO, "Monitor", rb_cObject);
84
84
  }
85
85
 
86
86
  /* Create the libev event loop and incoming event buffer */
@@ -95,13 +95,12 @@ static VALUE NIO_Selector_allocate(VALUE klass)
95
95
  safety. Pipes are nice and safe to use between threads.
96
96
 
97
97
  Note that Java NIO uses this same mechanism */
98
- if(pipe(fds) < 0) {
98
+ if (pipe(fds) < 0) {
99
99
  rb_sys_fail("pipe");
100
100
  }
101
101
 
102
102
  /* Use non-blocking reads/writes during wakeup, in case the buffer is full */
103
- if(fcntl(fds[0], F_SETFL, O_NONBLOCK) < 0 ||
104
- fcntl(fds[1], F_SETFL, O_NONBLOCK) < 0) {
103
+ if (fcntl(fds[0], F_SETFL, O_NONBLOCK) < 0 || fcntl(fds[1], F_SETFL, O_NONBLOCK) < 0) {
105
104
  rb_sys_fail("fcntl");
106
105
  }
107
106
 
@@ -127,7 +126,7 @@ static VALUE NIO_Selector_allocate(VALUE klass)
127
126
  /* NIO selectors store all Ruby objects in instance variables so mark is a stub */
128
127
  static void NIO_Selector_mark(struct NIO_Selector *selector)
129
128
  {
130
- if(selector->ready_array != Qnil) {
129
+ if (selector->ready_array != Qnil) {
131
130
  rb_gc_mark(selector->ready_array);
132
131
  }
133
132
  }
@@ -136,14 +135,14 @@ static void NIO_Selector_mark(struct NIO_Selector *selector)
136
135
  Called by both NIO::Selector#close and the finalizer below */
137
136
  static void NIO_Selector_shutdown(struct NIO_Selector *selector)
138
137
  {
139
- if(selector->closed) {
138
+ if (selector->closed) {
140
139
  return;
141
140
  }
142
141
 
143
142
  close(selector->wakeup_reader);
144
143
  close(selector->wakeup_writer);
145
144
 
146
- if(selector->ev_loop) {
145
+ if (selector->ev_loop) {
147
146
  ev_loop_destroy(selector->ev_loop);
148
147
  selector->ev_loop = 0;
149
148
  }
@@ -159,30 +158,39 @@ static void NIO_Selector_free(struct NIO_Selector *selector)
159
158
  }
160
159
 
161
160
  /* Return an array of symbols for supported backends */
162
- static VALUE NIO_Selector_supported_backends(VALUE klass) {
161
+ static VALUE NIO_Selector_supported_backends(VALUE klass)
162
+ {
163
163
  unsigned int backends = ev_supported_backends();
164
164
  VALUE result = rb_ary_new();
165
165
 
166
- if(backends & EVBACKEND_EPOLL) {
166
+ if (backends & EVBACKEND_EPOLL) {
167
167
  rb_ary_push(result, ID2SYM(rb_intern("epoll")));
168
168
  }
169
169
 
170
- if(backends & EVBACKEND_POLL) {
170
+ if (backends & EVBACKEND_POLL) {
171
171
  rb_ary_push(result, ID2SYM(rb_intern("poll")));
172
172
  }
173
173
 
174
- if(backends & EVBACKEND_KQUEUE) {
174
+ if (backends & EVBACKEND_KQUEUE) {
175
175
  rb_ary_push(result, ID2SYM(rb_intern("kqueue")));
176
176
  }
177
177
 
178
- if(backends & EVBACKEND_SELECT) {
178
+ if (backends & EVBACKEND_SELECT) {
179
179
  rb_ary_push(result, ID2SYM(rb_intern("select")));
180
180
  }
181
181
 
182
- if(backends & EVBACKEND_PORT) {
182
+ if (backends & EVBACKEND_PORT) {
183
183
  rb_ary_push(result, ID2SYM(rb_intern("port")));
184
184
  }
185
185
 
186
+ if (backends & EVBACKEND_LINUXAIO) {
187
+ rb_ary_push(result, ID2SYM(rb_intern("linuxaio")));
188
+ }
189
+
190
+ if (backends & EVBACKEND_IOURING) {
191
+ rb_ary_push(result, ID2SYM(rb_intern("io_uring")));
192
+ }
193
+
186
194
  return result;
187
195
  }
188
196
 
@@ -201,27 +209,29 @@ static VALUE NIO_Selector_initialize(int argc, VALUE *argv, VALUE self)
201
209
 
202
210
  rb_scan_args(argc, argv, "01", &backend);
203
211
 
204
- if(backend != Qnil) {
205
- if(!rb_ary_includes(NIO_Selector_supported_backends(CLASS_OF(self)), backend)) {
206
- rb_raise(rb_eArgError, "unsupported backend: %s",
207
- RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0)));
212
+ if (backend != Qnil) {
213
+ if (!rb_ary_includes(NIO_Selector_supported_backends(CLASS_OF(self)), backend)) {
214
+ rb_raise(rb_eArgError, "unsupported backend: %s", RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0)));
208
215
  }
209
216
 
210
217
  backend_id = SYM2ID(backend);
211
218
 
212
- if(backend_id == rb_intern("epoll")) {
219
+ if (backend_id == rb_intern("epoll")) {
213
220
  flags = EVBACKEND_EPOLL;
214
- } else if(backend_id == rb_intern("poll")) {
221
+ } else if (backend_id == rb_intern("poll")) {
215
222
  flags = EVBACKEND_POLL;
216
- } else if(backend_id == rb_intern("kqueue")) {
223
+ } else if (backend_id == rb_intern("kqueue")) {
217
224
  flags = EVBACKEND_KQUEUE;
218
- } else if(backend_id == rb_intern("select")) {
225
+ } else if (backend_id == rb_intern("select")) {
219
226
  flags = EVBACKEND_SELECT;
220
- } else if(backend_id == rb_intern("port")) {
227
+ } else if (backend_id == rb_intern("port")) {
221
228
  flags = EVBACKEND_PORT;
229
+ } else if (backend_id == rb_intern("linuxaio")) {
230
+ flags = EVBACKEND_LINUXAIO;
231
+ } else if (backend_id == rb_intern("io_uring")) {
232
+ flags = EVBACKEND_IOURING;
222
233
  } else {
223
- rb_raise(rb_eArgError, "unsupported backend: %s",
224
- RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0)));
234
+ rb_raise(rb_eArgError, "unsupported backend: %s", RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0)));
225
235
  }
226
236
  }
227
237
 
@@ -229,7 +239,7 @@ static VALUE NIO_Selector_initialize(int argc, VALUE *argv, VALUE self)
229
239
  assert(!selector->ev_loop);
230
240
 
231
241
  selector->ev_loop = ev_loop_new(flags);
232
- if(!selector->ev_loop) {
242
+ if (!selector->ev_loop) {
233
243
  rb_raise(rb_eIOError, "error initializing event loop");
234
244
  }
235
245
 
@@ -245,11 +255,12 @@ static VALUE NIO_Selector_initialize(int argc, VALUE *argv, VALUE self)
245
255
  return Qnil;
246
256
  }
247
257
 
248
- static VALUE NIO_Selector_backend(VALUE self) {
258
+ static VALUE NIO_Selector_backend(VALUE self)
259
+ {
249
260
  struct NIO_Selector *selector;
250
261
 
251
262
  Data_Get_Struct(self, struct NIO_Selector, selector);
252
- if(selector->closed) {
263
+ if (selector->closed) {
253
264
  rb_raise(rb_eIOError, "selector is closed");
254
265
  }
255
266
 
@@ -264,6 +275,10 @@ static VALUE NIO_Selector_backend(VALUE self) {
264
275
  return ID2SYM(rb_intern("select"));
265
276
  case EVBACKEND_PORT:
266
277
  return ID2SYM(rb_intern("port"));
278
+ case EVBACKEND_LINUXAIO:
279
+ return ID2SYM(rb_intern("linuxaio"));
280
+ case EVBACKEND_IOURING:
281
+ return ID2SYM(rb_intern("io_uring"));
267
282
  }
268
283
 
269
284
  return ID2SYM(rb_intern("unknown"));
@@ -277,7 +292,7 @@ static VALUE NIO_Selector_synchronize(VALUE self, VALUE (*func)(VALUE *args), VA
277
292
  current_thread = rb_thread_current();
278
293
  lock_holder = rb_ivar_get(self, rb_intern("lock_holder"));
279
294
 
280
- if(lock_holder != current_thread) {
295
+ if (lock_holder != current_thread) {
281
296
  lock = rb_ivar_get(self, rb_intern("lock"));
282
297
  rb_funcall(lock, rb_intern("lock"), 0);
283
298
  rb_ivar_set(self, rb_intern("lock_holder"), current_thread);
@@ -306,7 +321,7 @@ static VALUE NIO_Selector_unlock(VALUE self)
306
321
  /* Register an IO object with the selector for the given interests */
307
322
  static VALUE NIO_Selector_register(VALUE self, VALUE io, VALUE interests)
308
323
  {
309
- VALUE args[3] = {self, io, interests};
324
+ VALUE args[3] = { self, io, interests };
310
325
  return NIO_Selector_synchronize(self, NIO_Selector_register_synchronized, args);
311
326
  }
312
327
 
@@ -322,14 +337,14 @@ static VALUE NIO_Selector_register_synchronized(VALUE *args)
322
337
  interests = args[2];
323
338
 
324
339
  Data_Get_Struct(self, struct NIO_Selector, selector);
325
- if(selector->closed) {
340
+ if (selector->closed) {
326
341
  rb_raise(rb_eIOError, "selector is closed");
327
342
  }
328
343
 
329
344
  selectables = rb_ivar_get(self, rb_intern("selectables"));
330
345
  monitor = rb_hash_lookup(selectables, io);
331
346
 
332
- if(monitor != Qnil)
347
+ if (monitor != Qnil)
333
348
  rb_raise(rb_eArgError, "this IO is already registered with selector");
334
349
 
335
350
  /* Create a new NIO::Monitor */
@@ -346,7 +361,7 @@ static VALUE NIO_Selector_register_synchronized(VALUE *args)
346
361
  /* Deregister an IO object from the selector */
347
362
  static VALUE NIO_Selector_deregister(VALUE self, VALUE io)
348
363
  {
349
- VALUE args[2] = {self, io};
364
+ VALUE args[2] = { self, io };
350
365
  return NIO_Selector_synchronize(self, NIO_Selector_deregister_synchronized, args);
351
366
  }
352
367
 
@@ -361,7 +376,7 @@ static VALUE NIO_Selector_deregister_synchronized(VALUE *args)
361
376
  selectables = rb_ivar_get(self, rb_intern("selectables"));
362
377
  monitor = rb_hash_delete(selectables, io);
363
378
 
364
- if(monitor != Qnil) {
379
+ if (monitor != Qnil) {
365
380
  rb_funcall(monitor, rb_intern("close"), 1, Qfalse);
366
381
  }
367
382
 
@@ -385,7 +400,7 @@ static VALUE NIO_Selector_select(int argc, VALUE *argv, VALUE self)
385
400
 
386
401
  rb_scan_args(argc, argv, "01", &timeout);
387
402
 
388
- if(timeout != Qnil && NUM2DBL(timeout) < 0) {
403
+ if (timeout != Qnil && NUM2DBL(timeout) < 0) {
389
404
  rb_raise(rb_eArgError, "time interval must be positive");
390
405
  }
391
406
 
@@ -404,26 +419,26 @@ static VALUE NIO_Selector_select_synchronized(VALUE *args)
404
419
 
405
420
  Data_Get_Struct(args[0], struct NIO_Selector, selector);
406
421
 
407
- if(selector->closed) {
422
+ if (selector->closed) {
408
423
  rb_raise(rb_eIOError, "selector is closed");
409
424
  }
410
425
 
411
- if(!rb_block_given_p()) {
426
+ if (!rb_block_given_p()) {
412
427
  selector->ready_array = rb_ary_new();
413
428
  }
414
429
 
415
430
  ready = NIO_Selector_run(selector, args[1]);
416
431
 
417
432
  /* Timeout */
418
- if(ready < 0) {
419
- if(!rb_block_given_p()) {
433
+ if (ready < 0) {
434
+ if (!rb_block_given_p()) {
420
435
  selector->ready_array = Qnil;
421
436
  }
422
437
 
423
438
  return Qnil;
424
439
  }
425
440
 
426
- if(rb_block_given_p()) {
441
+ if (rb_block_given_p()) {
427
442
  return INT2NUM(ready);
428
443
  } else {
429
444
  ready_array = selector->ready_array;
@@ -441,12 +456,12 @@ static int NIO_Selector_run(struct NIO_Selector *selector, VALUE timeout)
441
456
  selector->selecting = 1;
442
457
  selector->wakeup_fired = 0;
443
458
 
444
- if(timeout == Qnil) {
459
+ if (timeout == Qnil) {
445
460
  /* Don't fire a wakeup timeout if we weren't passed one */
446
461
  ev_timer_stop(selector->ev_loop, &selector->timer);
447
462
  } else {
448
463
  timeout_val = NUM2DBL(timeout);
449
- if(timeout_val == 0) {
464
+ if (timeout_val == 0) {
450
465
  /* If we've been given an explicit timeout of 0, perform a non-blocking
451
466
  select operation */
452
467
  ev_run_flags = EVRUN_NOWAIT;
@@ -462,7 +477,7 @@ static int NIO_Selector_run(struct NIO_Selector *selector, VALUE timeout)
462
477
  result = selector->ready_count;
463
478
  selector->selecting = selector->ready_count = 0;
464
479
 
465
- if(result > 0 || selector->wakeup_fired) {
480
+ if (result > 0 || selector->wakeup_fired) {
466
481
  selector->wakeup_fired = 0;
467
482
  return result;
468
483
  } else {
@@ -476,7 +491,7 @@ static VALUE NIO_Selector_wakeup(VALUE self)
476
491
  struct NIO_Selector *selector;
477
492
  Data_Get_Struct(self, struct NIO_Selector, selector);
478
493
 
479
- if(selector->closed) {
494
+ if (selector->closed) {
480
495
  rb_raise(rb_eIOError, "selector is closed");
481
496
  }
482
497
 
@@ -489,7 +504,7 @@ static VALUE NIO_Selector_wakeup(VALUE self)
489
504
  /* Close the selector and free system resources */
490
505
  static VALUE NIO_Selector_close(VALUE self)
491
506
  {
492
- VALUE args[1] = {self};
507
+ VALUE args[1] = { self };
493
508
  return NIO_Selector_synchronize(self, NIO_Selector_close_synchronized, args);
494
509
  }
495
510
 
@@ -507,7 +522,7 @@ static VALUE NIO_Selector_close_synchronized(VALUE *args)
507
522
  /* Is the selector closed? */
508
523
  static VALUE NIO_Selector_closed(VALUE self)
509
524
  {
510
- VALUE args[1] = {self};
525
+ VALUE args[1] = { self };
511
526
  return NIO_Selector_synchronize(self, NIO_Selector_closed_synchronized, args);
512
527
  }
513
528
 
@@ -528,7 +543,6 @@ static VALUE NIO_Selector_is_empty(VALUE self)
528
543
  return rb_funcall(selectables, rb_intern("empty?"), 0) == Qtrue ? Qtrue : Qfalse;
529
544
  }
530
545
 
531
-
532
546
  /* Called whenever a timeout fires on the event loop */
533
547
  static void NIO_Selector_timeout_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents)
534
548
  {
@@ -542,7 +556,8 @@ static void NIO_Selector_wakeup_callback(struct ev_loop *ev_loop, struct ev_io *
542
556
  selector->selecting = 0;
543
557
 
544
558
  /* Drain the wakeup pipe, giving us level-triggered behavior */
545
- while(read(selector->wakeup_reader, buffer, 128) > 0);
559
+ while (read(selector->wakeup_reader, buffer, 128) > 0)
560
+ ;
546
561
  }
547
562
 
548
563
  /* libev callback fired whenever a monitor gets an event */
@@ -558,7 +573,7 @@ void NIO_Selector_monitor_callback(struct ev_loop *ev_loop, struct ev_io *io, in
558
573
  selector->ready_count++;
559
574
  monitor_data->revents = revents;
560
575
 
561
- if(rb_block_given_p()) {
576
+ if (rb_block_given_p()) {
562
577
  rb_yield(monitor);
563
578
  } else {
564
579
  assert(selector->ready_array != Qnil);