io-wait 0.3.6 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2cece766b90e4d81cd9f633898dbe10bfb4c50ad82be6f2caee138df7047ed3c
4
- data.tar.gz: 0a7bb7fad1bb08afe84a64debfcec609e4f341173ded73745e5fd72e5bae8d17
3
+ metadata.gz: e60418360dcf692b36cb2cf2ccb908dde272503ef0d15cd39ca9b2f49dbbc1df
4
+ data.tar.gz: 3eed0651def11e51177648806ca564a24a783bfa7365c78039b7ef37808cac9e
5
5
  SHA512:
6
- metadata.gz: 498ed5a2e45eb6e5201fb59d5f4bc47e4b4ed7948a0813a0f9849a858fcc4faec0574958f638afdcb881ea8e691bd8f7715f722c5d9fd0ab7d6ede15f4484d76
7
- data.tar.gz: b7ea14ccee049182560f902f722f6c263c5d8015685e5969a3c25bbea630064f6bb2d5549706272d2143e6590bd1cd814d8763fdb70f6c3b9a34d4b82d17666c
6
+ metadata.gz: 1e772560133ce1309fc71e8c27d7591fe44c6d956953b002a7bf13fc38529d4d41edde4abccebea90b27853ded7fb6727b991ea2d3439fe45359791f85025f88
7
+ data.tar.gz: 4c448bcca53322ab1a7ccb06f6dab9c5e3d59eb288a97cd74d45b3981129d899295044211a2062bcad45df57332679dfe43f64d56ca0a5df56960d179f62582a
@@ -1,21 +1,4 @@
1
1
  # frozen_string_literal: false
2
2
  require 'mkmf'
3
3
 
4
- target = "io/wait"
5
- have_func("rb_io_wait", "ruby/io.h")
6
- have_func("rb_io_descriptor", "ruby/io.h")
7
- unless macro_defined?("DOSISH", "#include <ruby.h>")
8
- have_header(ioctl_h = "sys/ioctl.h") or ioctl_h = nil
9
- fionread = %w[sys/ioctl.h sys/filio.h sys/socket.h].find do |h|
10
- have_macro("FIONREAD", [h, ioctl_h].compact)
11
- end
12
- if fionread
13
- $defs << "-DFIONREAD_HEADER=\"<#{fionread}>\""
14
- create_makefile(target)
15
- end
16
- else
17
- if have_func("rb_w32_ioctlsocket", "ruby.h")
18
- have_func("rb_w32_is_socket", "ruby.h")
19
- create_makefile(target)
20
- end
21
- end
4
+ create_makefile("io/wait")
data/ext/io/wait/wait.c CHANGED
@@ -11,429 +11,13 @@
11
11
 
12
12
  **********************************************************************/
13
13
 
14
- #include "ruby.h"
15
- #include "ruby/io.h"
16
-
17
- #include <sys/types.h>
18
- #if defined(HAVE_UNISTD_H) && (defined(__sun))
19
- #include <unistd.h>
20
- #endif
21
- #if defined(HAVE_SYS_IOCTL_H)
22
- #include <sys/ioctl.h>
23
- #endif
24
- #if defined(FIONREAD_HEADER)
25
- #include FIONREAD_HEADER
26
- #endif
27
-
28
- #ifdef HAVE_RB_W32_IOCTLSOCKET
29
- #define ioctl ioctlsocket
30
- #define ioctl_arg u_long
31
- #define ioctl_arg2num(i) ULONG2NUM(i)
32
- #else
33
- #define ioctl_arg int
34
- #define ioctl_arg2num(i) INT2NUM(i)
35
- #endif
36
-
37
- #ifdef HAVE_RB_W32_IS_SOCKET
38
- #define FIONREAD_POSSIBLE_P(fd) rb_w32_is_socket(fd)
39
- #else
40
- #define FIONREAD_POSSIBLE_P(fd) ((void)(fd),Qtrue)
41
- #endif
42
-
43
- #ifndef HAVE_RB_IO_WAIT
44
- static struct timeval *
45
- get_timeout(int argc, VALUE *argv, struct timeval *timerec)
46
- {
47
- VALUE timeout = Qnil;
48
- rb_check_arity(argc, 0, 1);
49
- if (!argc || NIL_P(timeout = argv[0])) {
50
- return NULL;
51
- }
52
- else {
53
- *timerec = rb_time_interval(timeout);
54
- return timerec;
55
- }
56
- }
57
-
58
- static int
59
- wait_for_single_fd(rb_io_t *fptr, int events, struct timeval *tv)
60
- {
61
- int i = rb_wait_for_single_fd(fptr->fd, events, tv);
62
- if (i < 0)
63
- rb_sys_fail(0);
64
- rb_io_check_closed(fptr);
65
- return (i & events);
66
- }
67
- #endif
68
-
69
- /*
70
- * call-seq:
71
- * io.nread -> int
72
- *
73
- * Returns number of bytes that can be read without blocking.
74
- * Returns zero if no information available.
75
- *
76
- * You must require 'io/wait' to use this method.
77
- */
78
-
79
- static VALUE
80
- io_nread(VALUE io)
81
- {
82
- rb_io_t *fptr;
83
- int len;
84
- ioctl_arg n;
85
-
86
- rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "IO#nread is deprecated; use wait_readable instead");
87
- GetOpenFile(io, fptr);
88
- rb_io_check_char_readable(fptr);
89
- len = rb_io_read_pending(fptr);
90
- if (len > 0) return INT2FIX(len);
91
-
92
- #ifdef HAVE_RB_IO_DESCRIPTOR
93
- int fd = rb_io_descriptor(io);
94
- #else
95
- int fd = fptr->fd;
96
- #endif
97
-
98
- if (!FIONREAD_POSSIBLE_P(fd)) return INT2FIX(0);
99
- if (ioctl(fd, FIONREAD, &n)) return INT2FIX(0);
100
- if (n > 0) return ioctl_arg2num(n);
101
- return INT2FIX(0);
102
- }
103
-
104
- #ifdef HAVE_RB_IO_WAIT
105
- static VALUE
106
- io_wait_event(VALUE io, int event, VALUE timeout, int return_io)
107
- {
108
- VALUE result = rb_io_wait(io, RB_INT2NUM(event), timeout);
109
-
110
- if (!RB_TEST(result)) {
111
- return Qnil;
112
- }
113
-
114
- int mask = RB_NUM2INT(result);
115
-
116
- if (mask & event) {
117
- if (return_io)
118
- return io;
119
- else
120
- return result;
121
- }
122
- else {
123
- return Qfalse;
124
- }
125
- }
126
- #endif
127
-
128
- /*
129
- * call-seq:
130
- * io.ready? -> truthy or falsy
131
- *
132
- * Returns a truthy value if input available without blocking, or a
133
- * falsy value.
134
- *
135
- * You must require 'io/wait' to use this method.
136
- */
137
-
138
- static VALUE
139
- io_ready_p(VALUE io)
140
- {
141
- rb_io_t *fptr;
142
- #ifndef HAVE_RB_IO_WAIT
143
- struct timeval tv = {0, 0};
144
- #endif
145
-
146
- rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "IO#ready? is deprecated; use wait_readable instead");
147
- GetOpenFile(io, fptr);
148
- rb_io_check_char_readable(fptr);
149
- if (rb_io_read_pending(fptr)) return Qtrue;
150
-
151
- #ifndef HAVE_RB_IO_WAIT
152
- return wait_for_single_fd(fptr, RB_WAITFD_IN, &tv) ? Qtrue : Qfalse;
153
- #else
154
- return io_wait_event(io, RUBY_IO_READABLE, RB_INT2NUM(0), 1);
155
- #endif
156
- }
157
-
158
- /* Ruby 3.2+ can define these methods. This macro indicates that case. */
159
- #ifndef RUBY_IO_WAIT_METHODS
160
-
161
- /*
162
- * call-seq:
163
- * io.wait_readable -> truthy or falsy
164
- * io.wait_readable(timeout) -> truthy or falsy
165
- *
166
- * Waits until IO is readable and returns a truthy value, or a falsy
167
- * value when times out. Returns a truthy value immediately when
168
- * buffered data is available.
169
- *
170
- * You must require 'io/wait' to use this method.
171
- */
172
-
173
- static VALUE
174
- io_wait_readable(int argc, VALUE *argv, VALUE io)
175
- {
176
- rb_io_t *fptr;
177
- #ifndef HAVE_RB_IO_WAIT
178
- struct timeval timerec;
179
- struct timeval *tv;
180
- #endif
181
-
182
- GetOpenFile(io, fptr);
183
- rb_io_check_char_readable(fptr);
184
-
185
- #ifndef HAVE_RB_IO_WAIT
186
- tv = get_timeout(argc, argv, &timerec);
187
- #endif
188
- if (rb_io_read_pending(fptr)) return Qtrue;
189
-
190
- #ifndef HAVE_RB_IO_WAIT
191
- if (wait_for_single_fd(fptr, RB_WAITFD_IN, tv)) {
192
- return io;
193
- }
194
- return Qnil;
195
- #else
196
- rb_check_arity(argc, 0, 1);
197
- VALUE timeout = (argc == 1 ? argv[0] : Qnil);
198
-
199
- return io_wait_event(io, RUBY_IO_READABLE, timeout, 1);
200
- #endif
201
- }
14
+ #include "ruby.h" /* abi_version */
202
15
 
203
16
  /*
204
- * call-seq:
205
- * io.wait_writable -> truthy or falsy
206
- * io.wait_writable(timeout) -> truthy or falsy
207
- *
208
- * Waits until IO is writable and returns a truthy value or a falsy
209
- * value when times out.
210
- *
211
- * You must require 'io/wait' to use this method.
212
- */
213
- static VALUE
214
- io_wait_writable(int argc, VALUE *argv, VALUE io)
215
- {
216
- rb_io_t *fptr;
217
- #ifndef HAVE_RB_IO_WAIT
218
- struct timeval timerec;
219
- struct timeval *tv;
220
- #endif
221
-
222
- GetOpenFile(io, fptr);
223
- rb_io_check_writable(fptr);
224
-
225
- #ifndef HAVE_RB_IO_WAIT
226
- tv = get_timeout(argc, argv, &timerec);
227
- if (wait_for_single_fd(fptr, RB_WAITFD_OUT, tv)) {
228
- return io;
229
- }
230
- return Qnil;
231
- #else
232
- rb_check_arity(argc, 0, 1);
233
- VALUE timeout = (argc == 1 ? argv[0] : Qnil);
234
-
235
- return io_wait_event(io, RUBY_IO_WRITABLE, timeout, 1);
236
- #endif
237
- }
238
-
239
- #ifdef HAVE_RB_IO_WAIT
240
- /*
241
- * call-seq:
242
- * io.wait_priority -> truthy or falsy
243
- * io.wait_priority(timeout) -> truthy or falsy
244
- *
245
- * Waits until IO is priority and returns a truthy value or a falsy
246
- * value when times out. Priority data is sent and received using
247
- * the Socket::MSG_OOB flag and is typically limited to streams.
248
- *
249
- * You must require 'io/wait' to use this method.
250
- */
251
- static VALUE
252
- io_wait_priority(int argc, VALUE *argv, VALUE io)
253
- {
254
- rb_io_t *fptr = NULL;
255
-
256
- RB_IO_POINTER(io, fptr);
257
- rb_io_check_char_readable(fptr);
258
-
259
- if (rb_io_read_pending(fptr)) return Qtrue;
260
-
261
- rb_check_arity(argc, 0, 1);
262
- VALUE timeout = argc == 1 ? argv[0] : Qnil;
263
-
264
- return io_wait_event(io, RUBY_IO_PRIORITY, timeout, 1);
265
- }
266
- #endif
267
-
268
- static int
269
- wait_mode_sym(VALUE mode)
270
- {
271
- if (mode == ID2SYM(rb_intern("r"))) {
272
- return RB_WAITFD_IN;
273
- }
274
- if (mode == ID2SYM(rb_intern("read"))) {
275
- return RB_WAITFD_IN;
276
- }
277
- if (mode == ID2SYM(rb_intern("readable"))) {
278
- return RB_WAITFD_IN;
279
- }
280
- if (mode == ID2SYM(rb_intern("w"))) {
281
- return RB_WAITFD_OUT;
282
- }
283
- if (mode == ID2SYM(rb_intern("write"))) {
284
- return RB_WAITFD_OUT;
285
- }
286
- if (mode == ID2SYM(rb_intern("writable"))) {
287
- return RB_WAITFD_OUT;
288
- }
289
- if (mode == ID2SYM(rb_intern("rw"))) {
290
- return RB_WAITFD_IN|RB_WAITFD_OUT;
291
- }
292
- if (mode == ID2SYM(rb_intern("read_write"))) {
293
- return RB_WAITFD_IN|RB_WAITFD_OUT;
294
- }
295
- if (mode == ID2SYM(rb_intern("readable_writable"))) {
296
- return RB_WAITFD_IN|RB_WAITFD_OUT;
297
- }
298
- rb_raise(rb_eArgError, "unsupported mode: %"PRIsVALUE, mode);
299
- return 0;
300
- }
301
-
302
- #ifdef HAVE_RB_IO_WAIT
303
- static inline rb_io_event_t
304
- io_event_from_value(VALUE value)
305
- {
306
- int events = RB_NUM2INT(value);
307
-
308
- if (events <= 0) rb_raise(rb_eArgError, "Events must be positive integer!");
309
-
310
- return events;
311
- }
312
- #endif
313
-
314
- /*
315
- * call-seq:
316
- * io.wait(events, timeout) -> event mask, false or nil
317
- * io.wait(*event_symbols[, timeout]) -> self, true, or false
318
- *
319
- * Waits until the IO becomes ready for the specified events and returns the
320
- * subset of events that become ready, or a falsy value when times out.
321
- *
322
- * The events can be a bit mask of +IO::READABLE+, +IO::WRITABLE+ or
323
- * +IO::PRIORITY+.
324
- *
325
- * Returns an event mask (truthy value) immediately when buffered data is
326
- * available.
327
- *
328
- * The second form: if one or more event symbols (+:read+, +:write+, or
329
- * +:read_write+) are passed, the event mask is the bit OR of the bitmask
330
- * corresponding to those symbols. In this form, +timeout+ is optional, the
331
- * order of the arguments is arbitrary, and returns +io+ if any of the
332
- * events is ready.
333
- *
334
- * You must require 'io/wait' to use this method.
335
- */
336
-
337
- static VALUE
338
- io_wait(int argc, VALUE *argv, VALUE io)
339
- {
340
- #ifndef HAVE_RB_IO_WAIT
341
- rb_io_t *fptr;
342
- struct timeval timerec;
343
- struct timeval *tv = NULL;
344
- int event = 0;
345
- int i;
346
-
347
- GetOpenFile(io, fptr);
348
- for (i = 0; i < argc; ++i) {
349
- if (SYMBOL_P(argv[i])) {
350
- event |= wait_mode_sym(argv[i]);
351
- }
352
- else {
353
- *(tv = &timerec) = rb_time_interval(argv[i]);
354
- }
355
- }
356
- /* rb_time_interval() and might_mode() might convert the argument */
357
- rb_io_check_closed(fptr);
358
- if (!event) event = RB_WAITFD_IN;
359
- if ((event & RB_WAITFD_IN) && rb_io_read_pending(fptr))
360
- return Qtrue;
361
- if (wait_for_single_fd(fptr, event, tv))
362
- return io;
363
- return Qnil;
364
- #else
365
- VALUE timeout = Qundef;
366
- rb_io_event_t events = 0;
367
- int i, return_io = 0;
368
-
369
- if (argc != 2 || (RB_SYMBOL_P(argv[0]) || RB_SYMBOL_P(argv[1]))) {
370
- /* We'd prefer to return the actual mask, but this form would return the io itself: */
371
- return_io = 1;
372
-
373
- /* Slow/messy path: */
374
- for (i = 0; i < argc; i += 1) {
375
- if (RB_SYMBOL_P(argv[i])) {
376
- events |= wait_mode_sym(argv[i]);
377
- }
378
- else if (timeout == Qundef) {
379
- rb_time_interval(timeout = argv[i]);
380
- }
381
- else {
382
- rb_raise(rb_eArgError, "timeout given more than once");
383
- }
384
- }
385
-
386
- if (timeout == Qundef) timeout = Qnil;
387
-
388
- if (events == 0) {
389
- events = RUBY_IO_READABLE;
390
- }
391
- }
392
- else /* argc == 2 and neither are symbols */ {
393
- /* This is the fast path: */
394
- events = io_event_from_value(argv[0]);
395
- timeout = argv[1];
396
- }
397
-
398
- if (events & RUBY_IO_READABLE) {
399
- rb_io_t *fptr = NULL;
400
- RB_IO_POINTER(io, fptr);
401
-
402
- if (rb_io_read_pending(fptr)) {
403
- /* This was the original behaviour: */
404
- if (return_io) return Qtrue;
405
- /* New behaviour always returns an event mask: */
406
- else return RB_INT2NUM(RUBY_IO_READABLE);
407
- }
408
- }
409
-
410
- return io_wait_event(io, events, timeout, return_io);
411
- #endif
412
- }
413
-
414
- #endif /* RUBY_IO_WAIT_METHODS */
415
-
416
- /*
417
- * IO wait methods
17
+ * IO wait methods are built in ruby now, just for backward compatibility.
418
18
  */
419
19
 
420
20
  void
421
21
  Init_wait(void)
422
22
  {
423
- #ifdef HAVE_RB_EXT_RACTOR_SAFE
424
- RB_EXT_RACTOR_SAFE(true);
425
- #endif
426
-
427
- rb_define_method(rb_cIO, "nread", io_nread, 0);
428
- rb_define_method(rb_cIO, "ready?", io_ready_p, 0);
429
-
430
- #ifndef RUBY_IO_WAIT_METHODS
431
- rb_define_method(rb_cIO, "wait", io_wait, -1);
432
-
433
- rb_define_method(rb_cIO, "wait_readable", io_wait_readable, -1);
434
- rb_define_method(rb_cIO, "wait_writable", io_wait_writable, -1);
435
- #ifdef HAVE_RB_IO_WAIT
436
- rb_define_method(rb_cIO, "wait_priority", io_wait_priority, -1);
437
- #endif
438
- #endif
439
23
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io-wait
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nobu Nakada
8
8
  - Charles Oliver Nutter
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-11-12 00:00:00.000000000 Z
11
+ date: 2025-12-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Waits until IO is readable or writable without blocking.
14
14
  email:
@@ -41,7 +41,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
41
41
  requirements:
42
42
  - - ">="
43
43
  - !ruby/object:Gem::Version
44
- version: '3.0'
44
+ version: '3.2'
45
45
  required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="