io-wait 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5ea39ff5c3815f098f8c15eb5b493e27408afda30a88d11cd00564ab85438639
4
+ data.tar.gz: 1a9ba20f028fe3259153bac2aa27b70870ef8e103d00c20cb6f7a267da94a6b5
5
+ SHA512:
6
+ metadata.gz: bebdf7aba83d894c28fa9763b0048047c437c67962a939e3d29a6b4cbefe1ac690afd8ef8ed372d013204b1298f7fab0f01cd45c35ced0bb4d62e4234818f0ae
7
+ data.tar.gz: 0f9635692758b787c4dc873e449e0a3e893dee5427f122fbda17f9458d4f33916bd32c333bf76023a73285177ac70b4b576025504130423089467fdb703266b5
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.bundle
10
+ *.dll
11
+ *.so
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.8.0
6
+ before_install: gem install bundler -v 2.1.4
data/COPYING ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a. place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b. use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c. give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d. make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a. distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b. accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c. give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d. make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in etc.gemspec
4
+ gemspec
@@ -0,0 +1,29 @@
1
+ # io-wait
2
+
3
+ This gem provides the feature for waiting until IO is readable or writable without blocking.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'io-wait'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install io-wait
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/io-wait.
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test/lib"
6
+ t.ruby_opts << "-rhelper"
7
+ t.test_files = FileList["test/**/test_*.rb"]
8
+ end
9
+
10
+ require 'rake/extensiontask'
11
+ Rake::ExtensionTask.new("io/wait")
12
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "io/wait"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ # AUTOGENERATED DEPENDENCIES START
2
+ wait.o: $(RUBY_EXTCONF_H)
3
+ wait.o: $(arch_hdrdir)/ruby/config.h
4
+ wait.o: $(hdrdir)/ruby.h
5
+ wait.o: $(hdrdir)/ruby/assert.h
6
+ wait.o: $(hdrdir)/ruby/backward.h
7
+ wait.o: $(hdrdir)/ruby/defines.h
8
+ wait.o: $(hdrdir)/ruby/encoding.h
9
+ wait.o: $(hdrdir)/ruby/intern.h
10
+ wait.o: $(hdrdir)/ruby/io.h
11
+ wait.o: $(hdrdir)/ruby/missing.h
12
+ wait.o: $(hdrdir)/ruby/onigmo.h
13
+ wait.o: $(hdrdir)/ruby/oniguruma.h
14
+ wait.o: $(hdrdir)/ruby/ruby.h
15
+ wait.o: $(hdrdir)/ruby/st.h
16
+ wait.o: $(hdrdir)/ruby/subst.h
17
+ wait.o: wait.c
18
+ # AUTOGENERATED DEPENDENCIES END
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: false
2
+ require 'mkmf'
3
+ target = "io/wait"
4
+
5
+ unless macro_defined?("DOSISH", "#include <ruby.h>")
6
+ have_header(ioctl_h = "sys/ioctl.h") or ioctl_h = nil
7
+ fionread = %w[sys/ioctl.h sys/filio.h sys/socket.h].find do |h|
8
+ have_macro("FIONREAD", [h, ioctl_h].compact)
9
+ end
10
+ if fionread
11
+ $defs << "-DFIONREAD_HEADER=\"<#{fionread}>\""
12
+ create_makefile(target)
13
+ end
14
+ else
15
+ if have_func("rb_w32_ioctlsocket", "ruby.h")
16
+ have_func("rb_w32_is_socket", "ruby.h")
17
+ create_makefile(target)
18
+ end
19
+ end
@@ -0,0 +1,253 @@
1
+ /**********************************************************************
2
+
3
+ io/wait.c -
4
+
5
+ $Author$
6
+ created at: Tue Aug 28 09:08:06 JST 2001
7
+
8
+ All the files in this distribution are covered under the Ruby's
9
+ license (see the file COPYING).
10
+
11
+ **********************************************************************/
12
+
13
+ #include "ruby.h"
14
+ #include "ruby/io.h"
15
+
16
+ #include <sys/types.h>
17
+ #if defined(HAVE_UNISTD_H) && (defined(__sun))
18
+ #include <unistd.h>
19
+ #endif
20
+ #if defined(HAVE_SYS_IOCTL_H)
21
+ #include <sys/ioctl.h>
22
+ #endif
23
+ #if defined(FIONREAD_HEADER)
24
+ #include FIONREAD_HEADER
25
+ #endif
26
+
27
+ #ifdef HAVE_RB_W32_IOCTLSOCKET
28
+ #define ioctl ioctlsocket
29
+ #define ioctl_arg u_long
30
+ #define ioctl_arg2num(i) ULONG2NUM(i)
31
+ #else
32
+ #define ioctl_arg int
33
+ #define ioctl_arg2num(i) INT2NUM(i)
34
+ #endif
35
+
36
+ #ifdef HAVE_RB_W32_IS_SOCKET
37
+ #define FIONREAD_POSSIBLE_P(fd) rb_w32_is_socket(fd)
38
+ #else
39
+ #define FIONREAD_POSSIBLE_P(fd) ((void)(fd),Qtrue)
40
+ #endif
41
+
42
+ static VALUE io_ready_p _((VALUE io));
43
+ static VALUE io_wait_readable _((int argc, VALUE *argv, VALUE io));
44
+ static VALUE io_wait_writable _((int argc, VALUE *argv, VALUE io));
45
+ void Init_wait _((void));
46
+
47
+ static struct timeval *
48
+ get_timeout(int argc, VALUE *argv, struct timeval *timerec)
49
+ {
50
+ VALUE timeout = Qnil;
51
+ rb_check_arity(argc, 0, 1);
52
+ if (!argc || NIL_P(timeout = argv[0])) {
53
+ return NULL;
54
+ }
55
+ else {
56
+ *timerec = rb_time_interval(timeout);
57
+ return timerec;
58
+ }
59
+ }
60
+
61
+ static int
62
+ wait_for_single_fd(rb_io_t *fptr, int events, struct timeval *tv)
63
+ {
64
+ int i = rb_wait_for_single_fd(fptr->fd, events, tv);
65
+ if (i < 0)
66
+ rb_sys_fail(0);
67
+ rb_io_check_closed(fptr);
68
+ return (i & events);
69
+ }
70
+
71
+ /*
72
+ * call-seq:
73
+ * io.nread -> int
74
+ *
75
+ * Returns number of bytes that can be read without blocking.
76
+ * Returns zero if no information available.
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
+ GetOpenFile(io, fptr);
87
+ rb_io_check_readable(fptr);
88
+ len = rb_io_read_pending(fptr);
89
+ if (len > 0) return INT2FIX(len);
90
+ if (!FIONREAD_POSSIBLE_P(fptr->fd)) return INT2FIX(0);
91
+ if (ioctl(fptr->fd, FIONREAD, &n)) return INT2FIX(0);
92
+ if (n > 0) return ioctl_arg2num(n);
93
+ return INT2FIX(0);
94
+ }
95
+
96
+ /*
97
+ * call-seq:
98
+ * io.ready? -> true or false
99
+ *
100
+ * Returns true if input available without blocking, or false.
101
+ */
102
+
103
+ static VALUE
104
+ io_ready_p(VALUE io)
105
+ {
106
+ rb_io_t *fptr;
107
+ struct timeval tv = {0, 0};
108
+
109
+ GetOpenFile(io, fptr);
110
+ rb_io_check_readable(fptr);
111
+ if (rb_io_read_pending(fptr)) return Qtrue;
112
+ if (wait_for_single_fd(fptr, RB_WAITFD_IN, &tv))
113
+ return Qtrue;
114
+ return Qfalse;
115
+ }
116
+
117
+ /*
118
+ * call-seq:
119
+ * io.wait_readable -> IO, true or nil
120
+ * io.wait_readable(timeout) -> IO, true or nil
121
+ *
122
+ * Waits until IO is readable without blocking and returns +self+, or
123
+ * +nil+ when times out.
124
+ * Returns +true+ immediately when buffered data is available.
125
+ */
126
+
127
+ static VALUE
128
+ io_wait_readable(int argc, VALUE *argv, VALUE io)
129
+ {
130
+ rb_io_t *fptr;
131
+ struct timeval timerec;
132
+ struct timeval *tv;
133
+
134
+ GetOpenFile(io, fptr);
135
+ rb_io_check_readable(fptr);
136
+ tv = get_timeout(argc, argv, &timerec);
137
+ if (rb_io_read_pending(fptr)) return Qtrue;
138
+ if (wait_for_single_fd(fptr, RB_WAITFD_IN, tv)) {
139
+ return io;
140
+ }
141
+ return Qnil;
142
+ }
143
+
144
+ /*
145
+ * call-seq:
146
+ * io.wait_writable -> IO
147
+ * io.wait_writable(timeout) -> IO or nil
148
+ *
149
+ * Waits until IO is writable without blocking and returns +self+ or
150
+ * +nil+ when times out.
151
+ */
152
+ static VALUE
153
+ io_wait_writable(int argc, VALUE *argv, VALUE io)
154
+ {
155
+ rb_io_t *fptr;
156
+ struct timeval timerec;
157
+ struct timeval *tv;
158
+
159
+ GetOpenFile(io, fptr);
160
+ rb_io_check_writable(fptr);
161
+ tv = get_timeout(argc, argv, &timerec);
162
+ if (wait_for_single_fd(fptr, RB_WAITFD_OUT, tv)) {
163
+ return io;
164
+ }
165
+ return Qnil;
166
+ }
167
+
168
+ static int
169
+ wait_mode_sym(VALUE mode)
170
+ {
171
+ if (mode == ID2SYM(rb_intern("r"))) {
172
+ return RB_WAITFD_IN;
173
+ }
174
+ if (mode == ID2SYM(rb_intern("read"))) {
175
+ return RB_WAITFD_IN;
176
+ }
177
+ if (mode == ID2SYM(rb_intern("readable"))) {
178
+ return RB_WAITFD_IN;
179
+ }
180
+ if (mode == ID2SYM(rb_intern("w"))) {
181
+ return RB_WAITFD_OUT;
182
+ }
183
+ if (mode == ID2SYM(rb_intern("write"))) {
184
+ return RB_WAITFD_OUT;
185
+ }
186
+ if (mode == ID2SYM(rb_intern("writable"))) {
187
+ return RB_WAITFD_OUT;
188
+ }
189
+ if (mode == ID2SYM(rb_intern("rw"))) {
190
+ return RB_WAITFD_IN|RB_WAITFD_OUT;
191
+ }
192
+ if (mode == ID2SYM(rb_intern("read_write"))) {
193
+ return RB_WAITFD_IN|RB_WAITFD_OUT;
194
+ }
195
+ if (mode == ID2SYM(rb_intern("readable_writable"))) {
196
+ return RB_WAITFD_IN|RB_WAITFD_OUT;
197
+ }
198
+ rb_raise(rb_eArgError, "unsupported mode: %"PRIsVALUE, mode);
199
+ return 0;
200
+ }
201
+
202
+ /*
203
+ * call-seq:
204
+ * io.wait(timeout = nil, mode = :read) -> IO, true or nil
205
+ *
206
+ * Waits until IO is readable or writable without blocking and returns
207
+ * +self+, or +nil+ when times out.
208
+ * Returns +true+ immediately when buffered data is available.
209
+ * Optional parameter +mode+ is one of +:read+, +:write+, or
210
+ * +:read_write+.
211
+ */
212
+
213
+ static VALUE
214
+ io_wait_readwrite(int argc, VALUE *argv, VALUE io)
215
+ {
216
+ rb_io_t *fptr;
217
+ struct timeval timerec;
218
+ struct timeval *tv = NULL;
219
+ int event = 0;
220
+ int i;
221
+
222
+ GetOpenFile(io, fptr);
223
+ for (i = 0; i < argc; ++i) {
224
+ if (SYMBOL_P(argv[i])) {
225
+ event |= wait_mode_sym(argv[i]);
226
+ }
227
+ else {
228
+ *(tv = &timerec) = rb_time_interval(argv[i]);
229
+ }
230
+ }
231
+ /* rb_time_interval() and might_mode() might convert the argument */
232
+ rb_io_check_closed(fptr);
233
+ if (!event) event = RB_WAITFD_IN;
234
+ if ((event & RB_WAITFD_IN) && rb_io_read_pending(fptr))
235
+ return Qtrue;
236
+ if (wait_for_single_fd(fptr, event, tv))
237
+ return io;
238
+ return Qnil;
239
+ }
240
+
241
+ /*
242
+ * IO wait methods
243
+ */
244
+
245
+ void
246
+ Init_wait(void)
247
+ {
248
+ rb_define_method(rb_cIO, "nread", io_nread, 0);
249
+ rb_define_method(rb_cIO, "ready?", io_ready_p, 0);
250
+ rb_define_method(rb_cIO, "wait", io_wait_readwrite, -1);
251
+ rb_define_method(rb_cIO, "wait_readable", io_wait_readable, -1);
252
+ rb_define_method(rb_cIO, "wait_writable", io_wait_writable, -1);
253
+ }
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "io-wait"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["Nobu Nakada"]
5
+ spec.email = ["nobu@ruby-lang.org"]
6
+
7
+ spec.summary = %q{Waits until IO is readable or writable without blocking.}
8
+ spec.description = %q{Waits until IO is readable or writable without blocking.}
9
+ spec.homepage = "https://github.com/ruby/io-wait"
10
+ spec.licenses = ["Ruby", "BSD-2-Clause"]
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
12
+
13
+ spec.metadata["homepage_uri"] = spec.homepage
14
+ spec.metadata["source_code_uri"] = spec.homepage
15
+
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: io-wait
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nobu Nakada
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Waits until IO is readable or writable without blocking.
14
+ email:
15
+ - nobu@ruby-lang.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".travis.yml"
22
+ - COPYING
23
+ - Gemfile
24
+ - README.md
25
+ - Rakefile
26
+ - bin/console
27
+ - bin/setup
28
+ - ext/io/wait/depend
29
+ - ext/io/wait/extconf.rb
30
+ - ext/io/wait/wait.c
31
+ - io-wait.gemspec
32
+ homepage: https://github.com/ruby/io-wait
33
+ licenses:
34
+ - Ruby
35
+ - BSD-2-Clause
36
+ metadata:
37
+ homepage_uri: https://github.com/ruby/io-wait
38
+ source_code_uri: https://github.com/ruby/io-wait
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.3.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.2.0.rc.1
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Waits until IO is readable or writable without blocking.
58
+ test_files: []