nio4r 2.5.4 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/workflow.yml +34 -16
  3. data/.mailmap +16 -0
  4. data/Gemfile +5 -5
  5. data/{CHANGES.md → changes.md} +54 -0
  6. data/examples/echo_server.rb +7 -0
  7. data/ext/libev/Changes +71 -2
  8. data/ext/libev/ev.c +611 -198
  9. data/ext/libev/ev.h +25 -22
  10. data/ext/libev/ev_epoll.c +16 -14
  11. data/ext/libev/ev_iouring.c +694 -0
  12. data/ext/libev/ev_kqueue.c +4 -4
  13. data/ext/libev/ev_linuxaio.c +78 -100
  14. data/ext/libev/ev_poll.c +6 -6
  15. data/ext/libev/ev_port.c +3 -3
  16. data/ext/libev/ev_select.c +6 -6
  17. data/ext/libev/ev_vars.h +34 -0
  18. data/ext/libev/ev_win32.c +2 -2
  19. data/ext/libev/ev_wrap.h +56 -0
  20. data/ext/nio4r/bytebuffer.c +75 -38
  21. data/ext/nio4r/extconf.rb +24 -1
  22. data/ext/nio4r/monitor.c +47 -22
  23. data/ext/nio4r/nio4r.h +1 -5
  24. data/ext/nio4r/org/nio4r/ByteBuffer.java +2 -0
  25. data/ext/nio4r/org/nio4r/Monitor.java +1 -0
  26. data/ext/nio4r/org/nio4r/Selector.java +8 -10
  27. data/ext/nio4r/selector.c +88 -48
  28. data/lib/nio/bytebuffer.rb +6 -0
  29. data/lib/nio/monitor.rb +7 -0
  30. data/lib/nio/selector.rb +26 -9
  31. data/lib/nio/version.rb +6 -1
  32. data/lib/nio.rb +29 -1
  33. data/lib/nio4r.rb +5 -0
  34. data/license.md +77 -0
  35. data/nio4r.gemspec +4 -3
  36. data/rakelib/extension.rake +1 -2
  37. data/readme.md +91 -0
  38. data/spec/nio/acceptables_spec.rb +4 -0
  39. data/spec/nio/bytebuffer_spec.rb +6 -0
  40. data/spec/nio/monitor_spec.rb +7 -0
  41. data/spec/nio/selectables/pipe_spec.rb +6 -0
  42. data/spec/nio/selectables/ssl_socket_spec.rb +8 -3
  43. data/spec/nio/selectables/tcp_socket_spec.rb +7 -0
  44. data/spec/nio/selectables/udp_socket_spec.rb +7 -0
  45. data/spec/nio/selector_spec.rb +16 -0
  46. data/spec/spec_helper.rb +7 -2
  47. data/spec/support/selectable_examples.rb +8 -0
  48. metadata +14 -9
  49. data/README.md +0 -132
@@ -8,8 +8,8 @@ static VALUE cNIO_ByteBuffer_MarkUnsetError = Qnil;
8
8
 
9
9
  /* Allocator/deallocator */
10
10
  static VALUE NIO_ByteBuffer_allocate(VALUE klass);
11
- static void NIO_ByteBuffer_gc_mark(struct NIO_ByteBuffer *byteBuffer);
12
- static void NIO_ByteBuffer_free(struct NIO_ByteBuffer *byteBuffer);
11
+ static void NIO_ByteBuffer_free(void *data);
12
+ static size_t NIO_ByteBuffer_memsize(const void *data);
13
13
 
14
14
  /* Methods */
15
15
  static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity);
@@ -36,6 +36,26 @@ static VALUE NIO_ByteBuffer_inspect(VALUE self);
36
36
 
37
37
  #define MARK_UNSET -1
38
38
 
39
+ /* Compatibility for Ruby <= 3.1 */
40
+ #ifndef HAVE_RB_IO_DESCRIPTOR
41
+ static int
42
+ io_descriptor_fallback(VALUE io)
43
+ {
44
+ rb_io_t *fptr;
45
+ GetOpenFile(io, fptr);
46
+ return fptr->fd;
47
+ }
48
+ #define rb_io_descriptor io_descriptor_fallback
49
+ #endif
50
+
51
+ static void
52
+ io_set_nonblock(VALUE io)
53
+ {
54
+ rb_io_t *fptr;
55
+ GetOpenFile(io, fptr);
56
+ rb_io_set_nonblock(fptr);
57
+ }
58
+
39
59
  void Init_NIO_ByteBuffer()
40
60
  {
41
61
  mNIO = rb_define_module("NIO");
@@ -72,28 +92,46 @@ void Init_NIO_ByteBuffer()
72
92
  rb_define_method(cNIO_ByteBuffer, "inspect", NIO_ByteBuffer_inspect, 0);
73
93
  }
74
94
 
95
+ static const rb_data_type_t NIO_ByteBuffer_type = {
96
+ "NIO::ByteBuffer",
97
+ {
98
+ NULL, // Nothing to mark
99
+ NIO_ByteBuffer_free,
100
+ NIO_ByteBuffer_memsize,
101
+ },
102
+ 0,
103
+ 0,
104
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
105
+ };
106
+
75
107
  static VALUE NIO_ByteBuffer_allocate(VALUE klass)
76
108
  {
77
109
  struct NIO_ByteBuffer *bytebuffer = (struct NIO_ByteBuffer *)xmalloc(sizeof(struct NIO_ByteBuffer));
78
110
  bytebuffer->buffer = NULL;
79
- return Data_Wrap_Struct(klass, NIO_ByteBuffer_gc_mark, NIO_ByteBuffer_free, bytebuffer);
111
+ return TypedData_Wrap_Struct(klass, &NIO_ByteBuffer_type, bytebuffer);
80
112
  }
81
113
 
82
- static void NIO_ByteBuffer_gc_mark(struct NIO_ByteBuffer *buffer)
114
+ static void NIO_ByteBuffer_free(void *data)
83
115
  {
116
+ struct NIO_ByteBuffer *buffer = (struct NIO_ByteBuffer *)data;
117
+ if (buffer->buffer)
118
+ xfree(buffer->buffer);
119
+ xfree(buffer);
84
120
  }
85
121
 
86
- static void NIO_ByteBuffer_free(struct NIO_ByteBuffer *buffer)
122
+ static size_t NIO_ByteBuffer_memsize(const void *data)
87
123
  {
124
+ const struct NIO_ByteBuffer *buffer = (const struct NIO_ByteBuffer *)data;
125
+ size_t memsize = sizeof(struct NIO_ByteBuffer);
88
126
  if (buffer->buffer)
89
- xfree(buffer->buffer);
90
- xfree(buffer);
127
+ memsize += buffer->capacity;
128
+ return memsize;
91
129
  }
92
130
 
93
131
  static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity)
94
132
  {
95
133
  struct NIO_ByteBuffer *buffer;
96
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
134
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
97
135
 
98
136
  buffer->capacity = NUM2INT(capacity);
99
137
  buffer->buffer = xmalloc(buffer->capacity);
@@ -106,7 +144,7 @@ static VALUE NIO_ByteBuffer_initialize(VALUE self, VALUE capacity)
106
144
  static VALUE NIO_ByteBuffer_clear(VALUE self)
107
145
  {
108
146
  struct NIO_ByteBuffer *buffer;
109
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
147
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
110
148
 
111
149
  memset(buffer->buffer, 0, buffer->capacity);
112
150
 
@@ -120,7 +158,7 @@ static VALUE NIO_ByteBuffer_clear(VALUE self)
120
158
  static VALUE NIO_ByteBuffer_get_position(VALUE self)
121
159
  {
122
160
  struct NIO_ByteBuffer *buffer;
123
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
161
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
124
162
 
125
163
  return INT2NUM(buffer->position);
126
164
  }
@@ -129,7 +167,7 @@ static VALUE NIO_ByteBuffer_set_position(VALUE self, VALUE new_position)
129
167
  {
130
168
  int pos;
131
169
  struct NIO_ByteBuffer *buffer;
132
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
170
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
133
171
 
134
172
  pos = NUM2INT(new_position);
135
173
 
@@ -153,7 +191,7 @@ static VALUE NIO_ByteBuffer_set_position(VALUE self, VALUE new_position)
153
191
  static VALUE NIO_ByteBuffer_get_limit(VALUE self)
154
192
  {
155
193
  struct NIO_ByteBuffer *buffer;
156
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
194
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
157
195
 
158
196
  return INT2NUM(buffer->limit);
159
197
  }
@@ -162,7 +200,7 @@ static VALUE NIO_ByteBuffer_set_limit(VALUE self, VALUE new_limit)
162
200
  {
163
201
  int lim;
164
202
  struct NIO_ByteBuffer *buffer;
165
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
203
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
166
204
 
167
205
  lim = NUM2INT(new_limit);
168
206
 
@@ -190,7 +228,7 @@ static VALUE NIO_ByteBuffer_set_limit(VALUE self, VALUE new_limit)
190
228
  static VALUE NIO_ByteBuffer_capacity(VALUE self)
191
229
  {
192
230
  struct NIO_ByteBuffer *buffer;
193
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
231
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
194
232
 
195
233
  return INT2NUM(buffer->capacity);
196
234
  }
@@ -198,7 +236,7 @@ static VALUE NIO_ByteBuffer_capacity(VALUE self)
198
236
  static VALUE NIO_ByteBuffer_remaining(VALUE self)
199
237
  {
200
238
  struct NIO_ByteBuffer *buffer;
201
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
239
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
202
240
 
203
241
  return INT2NUM(buffer->limit - buffer->position);
204
242
  }
@@ -206,7 +244,7 @@ static VALUE NIO_ByteBuffer_remaining(VALUE self)
206
244
  static VALUE NIO_ByteBuffer_full(VALUE self)
207
245
  {
208
246
  struct NIO_ByteBuffer *buffer;
209
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
247
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
210
248
 
211
249
  return buffer->position == buffer->limit ? Qtrue : Qfalse;
212
250
  }
@@ -216,7 +254,7 @@ static VALUE NIO_ByteBuffer_get(int argc, VALUE *argv, VALUE self)
216
254
  int len;
217
255
  VALUE length, result;
218
256
  struct NIO_ByteBuffer *buffer;
219
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
257
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
220
258
 
221
259
  rb_scan_args(argc, argv, "01", &length);
222
260
 
@@ -244,7 +282,7 @@ static VALUE NIO_ByteBuffer_fetch(VALUE self, VALUE index)
244
282
  {
245
283
  int i;
246
284
  struct NIO_ByteBuffer *buffer;
247
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
285
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
248
286
 
249
287
  i = NUM2INT(index);
250
288
 
@@ -263,7 +301,7 @@ static VALUE NIO_ByteBuffer_put(VALUE self, VALUE string)
263
301
  {
264
302
  long length;
265
303
  struct NIO_ByteBuffer *buffer;
266
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
304
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
267
305
 
268
306
  StringValue(string);
269
307
  length = RSTRING_LEN(string);
@@ -281,19 +319,19 @@ static VALUE NIO_ByteBuffer_put(VALUE self, VALUE string)
281
319
  static VALUE NIO_ByteBuffer_read_from(VALUE self, VALUE io)
282
320
  {
283
321
  struct NIO_ByteBuffer *buffer;
284
- rb_io_t *fptr;
285
322
  ssize_t nbytes, bytes_read;
286
323
 
287
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
288
- GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
289
- rb_io_set_nonblock(fptr);
324
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
325
+
326
+ io = rb_convert_type(io, T_FILE, "IO", "to_io");
327
+ io_set_nonblock(io);
290
328
 
291
329
  nbytes = buffer->limit - buffer->position;
292
330
  if (nbytes == 0) {
293
331
  rb_raise(cNIO_ByteBuffer_OverflowError, "buffer is full");
294
332
  }
295
333
 
296
- bytes_read = read(FPTR_TO_FD(fptr), buffer->buffer + buffer->position, nbytes);
334
+ bytes_read = read(rb_io_descriptor(io), buffer->buffer + buffer->position, nbytes);
297
335
 
298
336
  if (bytes_read < 0) {
299
337
  if (errno == EAGAIN) {
@@ -305,25 +343,24 @@ static VALUE NIO_ByteBuffer_read_from(VALUE self, VALUE io)
305
343
 
306
344
  buffer->position += bytes_read;
307
345
 
308
- return INT2NUM(bytes_read);
346
+ return SIZET2NUM(bytes_read);
309
347
  }
310
348
 
311
349
  static VALUE NIO_ByteBuffer_write_to(VALUE self, VALUE io)
312
350
  {
313
351
  struct NIO_ByteBuffer *buffer;
314
- rb_io_t *fptr;
315
352
  ssize_t nbytes, bytes_written;
316
353
 
317
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
318
- GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
319
- rb_io_set_nonblock(fptr);
354
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
355
+ io = rb_convert_type(io, T_FILE, "IO", "to_io");
356
+ io_set_nonblock(io);
320
357
 
321
358
  nbytes = buffer->limit - buffer->position;
322
359
  if (nbytes == 0) {
323
360
  rb_raise(cNIO_ByteBuffer_UnderflowError, "no data remaining in buffer");
324
361
  }
325
362
 
326
- bytes_written = write(FPTR_TO_FD(fptr), buffer->buffer + buffer->position, nbytes);
363
+ bytes_written = write(rb_io_descriptor(io), buffer->buffer + buffer->position, nbytes);
327
364
 
328
365
  if (bytes_written < 0) {
329
366
  if (errno == EAGAIN) {
@@ -335,13 +372,13 @@ static VALUE NIO_ByteBuffer_write_to(VALUE self, VALUE io)
335
372
 
336
373
  buffer->position += bytes_written;
337
374
 
338
- return INT2NUM(bytes_written);
375
+ return SIZET2NUM(bytes_written);
339
376
  }
340
377
 
341
378
  static VALUE NIO_ByteBuffer_flip(VALUE self)
342
379
  {
343
380
  struct NIO_ByteBuffer *buffer;
344
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
381
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
345
382
 
346
383
  buffer->limit = buffer->position;
347
384
  buffer->position = 0;
@@ -353,7 +390,7 @@ static VALUE NIO_ByteBuffer_flip(VALUE self)
353
390
  static VALUE NIO_ByteBuffer_rewind(VALUE self)
354
391
  {
355
392
  struct NIO_ByteBuffer *buffer;
356
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
393
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
357
394
 
358
395
  buffer->position = 0;
359
396
  buffer->mark = MARK_UNSET;
@@ -364,7 +401,7 @@ static VALUE NIO_ByteBuffer_rewind(VALUE self)
364
401
  static VALUE NIO_ByteBuffer_mark(VALUE self)
365
402
  {
366
403
  struct NIO_ByteBuffer *buffer;
367
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
404
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
368
405
 
369
406
  buffer->mark = buffer->position;
370
407
  return self;
@@ -373,7 +410,7 @@ static VALUE NIO_ByteBuffer_mark(VALUE self)
373
410
  static VALUE NIO_ByteBuffer_reset(VALUE self)
374
411
  {
375
412
  struct NIO_ByteBuffer *buffer;
376
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
413
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
377
414
 
378
415
  if (buffer->mark < 0) {
379
416
  rb_raise(cNIO_ByteBuffer_MarkUnsetError, "mark has not been set");
@@ -387,7 +424,7 @@ static VALUE NIO_ByteBuffer_reset(VALUE self)
387
424
  static VALUE NIO_ByteBuffer_compact(VALUE self)
388
425
  {
389
426
  struct NIO_ByteBuffer *buffer;
390
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
427
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
391
428
 
392
429
  memmove(buffer->buffer, buffer->buffer + buffer->position, buffer->limit - buffer->position);
393
430
  buffer->position = buffer->limit - buffer->position;
@@ -400,7 +437,7 @@ static VALUE NIO_ByteBuffer_each(VALUE self)
400
437
  {
401
438
  int i;
402
439
  struct NIO_ByteBuffer *buffer;
403
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
440
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
404
441
 
405
442
  if (rb_block_given_p()) {
406
443
  for (i = 0; i < buffer->limit; i++) {
@@ -416,7 +453,7 @@ static VALUE NIO_ByteBuffer_each(VALUE self)
416
453
  static VALUE NIO_ByteBuffer_inspect(VALUE self)
417
454
  {
418
455
  struct NIO_ByteBuffer *buffer;
419
- Data_Get_Struct(self, struct NIO_ByteBuffer, buffer);
456
+ TypedData_Get_Struct(self, struct NIO_ByteBuffer, &NIO_ByteBuffer_type, buffer);
420
457
 
421
458
  return rb_sprintf(
422
459
  "#<%s:%p @position=%d @limit=%d @capacity=%d>",
data/ext/nio4r/extconf.rb CHANGED
@@ -1,10 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Released under the MIT License.
4
+ # Copyright, 2011-2020, by Tony Arcieri.
5
+ # Copyright, 2014, by Hiroshi Shibata.
6
+ # Copyright, 2014, by Sergey Avseyev.
7
+ # Copyright, 2015, by Daniel Berger.
8
+ # Copyright, 2017, by Jun Aruga.
9
+ # Copyright, 2017, by Usaku Nakamura.
10
+ # Copyright, 2017, by Lars Kanis.
11
+ # Copyright, 2019-2023, by Samuel Williams.
12
+ # Copyright, 2020, by Gregory Longtin.
13
+ # Copyright, 2020, by Boaz Segev.
14
+ # Copyright, 2020, by Joao Fernandes.
15
+ # Copyright, 2021, by Jeffrey Martin.
16
+
3
17
  require "rubygems"
4
18
 
5
19
  # Write a dummy Makefile on Windows because we use the pure Ruby implementation there
6
20
  if Gem.win_platform?
7
- require "devkit" if RUBY_PLATFORM.include?("mingw")
21
+ begin
22
+ require "devkit" if RUBY_PLATFORM.include?("mingw")
23
+ rescue LoadError => e
24
+ end
8
25
  File.write("Makefile", "all install::\n")
9
26
  File.write("nio4r_ext.so", "")
10
27
  exit
@@ -13,8 +30,10 @@ end
13
30
  require "mkmf"
14
31
 
15
32
  have_header("unistd.h")
33
+ have_func("rb_io_descriptor")
16
34
 
17
35
  $defs << "-DEV_USE_LINUXAIO" if have_header("linux/aio_abi.h")
36
+ $defs << "-DEV_USE_IOURING" if have_header("linux/io_uring.h")
18
37
  $defs << "-DEV_USE_SELECT" if have_header("sys/select.h")
19
38
  $defs << "-DEV_USE_POLL" if have_type("port_event_t", "poll.h")
20
39
  $defs << "-DEV_USE_EPOLL" if have_header("sys/epoll.h")
@@ -26,5 +45,9 @@ $defs << "-DEV_STANDALONE" # prevent libev from assuming "config.h" exists
26
45
 
27
46
  CONFIG["optflags"] << " -fno-strict-aliasing" unless RUBY_PLATFORM =~ /mswin/
28
47
 
48
+ if RUBY_PLATFORM =~ /darwin/
49
+ $DLDFLAGS.gsub!(/\-arch\s+[^\s]+/, "")
50
+ end
51
+
29
52
  dir_config "nio4r_ext"
30
53
  create_makefile "nio4r_ext"
data/ext/nio4r/monitor.c CHANGED
@@ -11,8 +11,8 @@ static VALUE cNIO_Monitor = Qnil;
11
11
 
12
12
  /* Allocator/deallocator */
13
13
  static VALUE NIO_Monitor_allocate(VALUE klass);
14
- static void NIO_Monitor_mark(struct NIO_Monitor *monitor);
15
- static void NIO_Monitor_free(struct NIO_Monitor *monitor);
14
+ static void NIO_Monitor_mark(void *data);
15
+ static size_t NIO_Monitor_memsize(const void *data);
16
16
 
17
17
  /* Methods */
18
18
  static VALUE NIO_Monitor_initialize(VALUE self, VALUE selector, VALUE io, VALUE interests);
@@ -34,6 +34,18 @@ static VALUE NIO_Monitor_readiness(VALUE self);
34
34
  static int NIO_Monitor_symbol2interest(VALUE interests);
35
35
  static void NIO_Monitor_update_interests(VALUE self, int interests);
36
36
 
37
+ /* Compatibility for Ruby <= 3.1 */
38
+ #ifndef HAVE_RB_IO_DESCRIPTOR
39
+ static int
40
+ io_descriptor_fallback(VALUE io)
41
+ {
42
+ rb_io_t *fptr;
43
+ GetOpenFile(io, fptr);
44
+ return fptr->fd;
45
+ }
46
+ #define rb_io_descriptor io_descriptor_fallback
47
+ #endif
48
+
37
49
  /* Monitor control how a channel is being waited for by a monitor */
38
50
  void Init_NIO_Monitor()
39
51
  {
@@ -58,22 +70,36 @@ void Init_NIO_Monitor()
58
70
  rb_define_method(cNIO_Monitor, "writeable?", NIO_Monitor_is_writable, 0);
59
71
  }
60
72
 
73
+ static const rb_data_type_t NIO_Monitor_type = {
74
+ "NIO::Monitor",
75
+ {
76
+ NIO_Monitor_mark,
77
+ RUBY_TYPED_DEFAULT_FREE,
78
+ NIO_Monitor_memsize,
79
+ },
80
+ 0,
81
+ 0,
82
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
83
+ };
84
+
61
85
  static VALUE NIO_Monitor_allocate(VALUE klass)
62
86
  {
63
87
  struct NIO_Monitor *monitor = (struct NIO_Monitor *)xmalloc(sizeof(struct NIO_Monitor));
64
88
  assert(monitor);
65
89
  *monitor = (struct NIO_Monitor){.self = Qnil};
66
- return Data_Wrap_Struct(klass, NIO_Monitor_mark, NIO_Monitor_free, monitor);
90
+ return TypedData_Wrap_Struct(klass, &NIO_Monitor_type, monitor);
67
91
  }
68
92
 
69
- static void NIO_Monitor_mark(struct NIO_Monitor *monitor)
93
+ static void NIO_Monitor_mark(void *data)
70
94
  {
95
+ struct NIO_Monitor *monitor = (struct NIO_Monitor *)data;
71
96
  rb_gc_mark(monitor->self);
72
97
  }
73
98
 
74
- static void NIO_Monitor_free(struct NIO_Monitor *monitor)
99
+ static size_t NIO_Monitor_memsize(const void *data)
75
100
  {
76
- xfree(monitor);
101
+ const struct NIO_Monitor *monitor = (const struct NIO_Monitor *)data;
102
+ return sizeof(*monitor);
77
103
  }
78
104
 
79
105
  static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE selector_obj)
@@ -81,11 +107,10 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE
81
107
  struct NIO_Monitor *monitor;
82
108
  struct NIO_Selector *selector;
83
109
  ID interests_id;
84
- rb_io_t *fptr;
85
110
 
86
111
  interests_id = SYM2ID(interests);
87
112
 
88
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
113
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
89
114
 
90
115
  if (interests_id == rb_intern("r")) {
91
116
  monitor->interests = EV_READ;
@@ -97,16 +122,16 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE
97
122
  rb_raise(rb_eArgError, "invalid event type %s (must be :r, :w, or :rw)", RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0)));
98
123
  }
99
124
 
100
- GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
101
- ev_io_init(&monitor->ev_io, NIO_Selector_monitor_callback, FPTR_TO_FD(fptr), monitor->interests);
125
+ int descriptor = rb_io_descriptor(rb_convert_type(io, T_FILE, "IO", "to_io"));
126
+ ev_io_init(&monitor->ev_io, NIO_Selector_monitor_callback, descriptor, monitor->interests);
102
127
 
103
128
  rb_ivar_set(self, rb_intern("io"), io);
104
129
  rb_ivar_set(self, rb_intern("interests"), interests);
105
130
  rb_ivar_set(self, rb_intern("selector"), selector_obj);
106
131
 
107
- Data_Get_Struct(selector_obj, struct NIO_Selector, selector);
132
+ selector = NIO_Selector_unwrap(selector_obj);
108
133
 
109
- monitor->self = self;
134
+ RB_OBJ_WRITE(self, &monitor->self, self);
110
135
  monitor->ev_io.data = (void *)monitor;
111
136
 
112
137
  /* We can safely hang onto this as we also hang onto a reference to the
@@ -124,7 +149,7 @@ static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self)
124
149
  {
125
150
  VALUE deregister, selector;
126
151
  struct NIO_Monitor *monitor;
127
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
152
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
128
153
 
129
154
  rb_scan_args(argc, argv, "01", &deregister);
130
155
  selector = rb_ivar_get(self, rb_intern("selector"));
@@ -150,7 +175,7 @@ static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self)
150
175
  static VALUE NIO_Monitor_is_closed(VALUE self)
151
176
  {
152
177
  struct NIO_Monitor *monitor;
153
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
178
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
154
179
 
155
180
  return monitor->selector == 0 ? Qtrue : Qfalse;
156
181
  }
@@ -179,10 +204,10 @@ static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests)
179
204
  static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest)
180
205
  {
181
206
  struct NIO_Monitor *monitor;
182
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
207
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
183
208
 
184
209
  interest = monitor->interests | NIO_Monitor_symbol2interest(interest);
185
- NIO_Monitor_update_interests(self, interest);
210
+ NIO_Monitor_update_interests(self, (int)interest);
186
211
 
187
212
  return rb_ivar_get(self, rb_intern("interests"));
188
213
  }
@@ -190,10 +215,10 @@ static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest)
190
215
  static VALUE NIO_Monitor_remove_interest(VALUE self, VALUE interest)
191
216
  {
192
217
  struct NIO_Monitor *monitor;
193
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
218
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
194
219
 
195
220
  interest = monitor->interests & ~NIO_Monitor_symbol2interest(interest);
196
- NIO_Monitor_update_interests(self, interest);
221
+ NIO_Monitor_update_interests(self, (int)interest);
197
222
 
198
223
  return rb_ivar_get(self, rb_intern("interests"));
199
224
  }
@@ -216,7 +241,7 @@ static VALUE NIO_Monitor_set_value(VALUE self, VALUE obj)
216
241
  static VALUE NIO_Monitor_readiness(VALUE self)
217
242
  {
218
243
  struct NIO_Monitor *monitor;
219
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
244
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
220
245
 
221
246
  if ((monitor->revents & (EV_READ | EV_WRITE)) == (EV_READ | EV_WRITE)) {
222
247
  return ID2SYM(rb_intern("rw"));
@@ -232,7 +257,7 @@ static VALUE NIO_Monitor_readiness(VALUE self)
232
257
  static VALUE NIO_Monitor_is_readable(VALUE self)
233
258
  {
234
259
  struct NIO_Monitor *monitor;
235
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
260
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
236
261
 
237
262
  if (monitor->revents & EV_READ) {
238
263
  return Qtrue;
@@ -244,7 +269,7 @@ static VALUE NIO_Monitor_is_readable(VALUE self)
244
269
  static VALUE NIO_Monitor_is_writable(VALUE self)
245
270
  {
246
271
  struct NIO_Monitor *monitor;
247
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
272
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
248
273
 
249
274
  if (monitor->revents & EV_WRITE) {
250
275
  return Qtrue;
@@ -275,7 +300,7 @@ static void NIO_Monitor_update_interests(VALUE self, int interests)
275
300
  {
276
301
  ID interests_id;
277
302
  struct NIO_Monitor *monitor;
278
- Data_Get_Struct(self, struct NIO_Monitor, monitor);
303
+ TypedData_Get_Struct(self, struct NIO_Monitor, &NIO_Monitor_type, monitor);
279
304
 
280
305
  if (NIO_Monitor_is_closed(self) == Qtrue) {
281
306
  rb_raise(rb_eEOFError, "monitor is closed");
data/ext/nio4r/nio4r.h CHANGED
@@ -40,11 +40,7 @@ struct NIO_ByteBuffer {
40
40
  int position, limit, capacity, mark;
41
41
  };
42
42
 
43
- #ifdef GetReadFile
44
- #define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
45
- #else
46
- #define FPTR_TO_FD(fptr) fptr->fd
47
- #endif /* GetReadFile */
43
+ struct NIO_Selector *NIO_Selector_unwrap(VALUE selector);
48
44
 
49
45
  /* Thunk between libev callbacks in NIO::Monitors and NIO::Selectors */
50
46
  void NIO_Selector_monitor_callback(struct ev_loop *ev_loop, struct ev_io *io, int revents);
@@ -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
  }