Skip to content

Commit 2f0565d

Browse files
committed
Move IO#{wait,wait_readable,wait_writable,wait_priority} methods to core
PullRequest: truffleruby/4207
2 parents b853dd6 + f8b48ef commit 2f0565d

File tree

3 files changed

+71
-71
lines changed

3 files changed

+71
-71
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Bug fixes:
88
* Fix repeated calling of methods `Dir#{each,each_child,children}` (#3464, @andrykonchin).
99

1010
Compatibility:
11+
* Move `IO#wait_readable`, `IO#wait_writable`, `IO#wait_priority` and `IO#wait` into core library (@andrykonchin).
1112

1213
* Change assignment evaluation order for fully qualified constant and evaluate left-hand-side before right-hand-side (#3039, @andrykonchin).
1314
* Fix evaluation order for multi-assignment and evaluate left-hand-side before right-hand-side (@andrykonchin).

lib/truffle/io/wait.rb

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -19,75 +19,4 @@ def ready?
1919
ensure_open_and_readable
2020
Truffle::IOOperations.poll(self, IO::READABLE, 0) > 0
2121
end
22-
23-
def wait_readable(timeout = nil)
24-
ensure_open_and_readable
25-
Truffle::IOOperations.poll(self, IO::READABLE, timeout) > 0 ? self : nil
26-
end
27-
28-
def wait_writable(timeout = nil)
29-
ensure_open_and_writable
30-
Truffle::IOOperations.poll(self, IO::WRITABLE, timeout) > 0 ? self : nil
31-
end
32-
33-
def wait_priority(timeout = nil)
34-
ensure_open_and_readable
35-
Truffle::IOOperations.poll(self, IO::PRIORITY, timeout) > 0 ? self : nil
36-
end
37-
38-
39-
# call-seq:
40-
# io.wait(events, timeout) -> event mask, false or nil
41-
# io.wait(timeout = nil, mode = :read) -> self, true, or false
42-
#
43-
# Waits until the IO becomes ready for the specified events and returns the
44-
# subset of events that become ready, or a falsy value when times out.
45-
#
46-
# The events can be a bit mask of +IO::READABLE+, +IO::WRITABLE+ or
47-
# +IO::PRIORITY+.
48-
#
49-
# Returns a truthy value immediately when buffered data is available.
50-
#
51-
# Optional parameter +mode+ is one of +:read+, +:write+, or
52-
# +:read_write+.
53-
def wait(*args)
54-
ensure_open
55-
56-
if args.size != 2 || Primitive.is_a?(args[0], Symbol) || Primitive.is_a?(args[1], Symbol)
57-
# Slow/messy path:
58-
timeout = :undef
59-
events = 0
60-
args.each do |arg|
61-
if Primitive.is_a?(arg, Symbol)
62-
events |= case arg
63-
when :r, :read, :readable then IO::READABLE
64-
when :w, :write, :writable then IO::WRITABLE
65-
when :rw, :read_write, :readable_writable then IO::READABLE | IO::WRITABLE
66-
else
67-
raise ArgumentError, "unsupported mode: #{arg}"
68-
end
69-
70-
elsif timeout == :undef
71-
timeout = arg
72-
else
73-
raise ArgumentError, 'timeout given more than once'
74-
end
75-
end
76-
77-
timeout = nil if timeout == :undef
78-
79-
events = IO::READABLE if events == 0
80-
81-
res = Truffle::IOOperations.poll(self, events, timeout)
82-
res == 0 ? nil : self
83-
else
84-
# args.size == 2 and neither are symbols
85-
# This is the fast path and the new interface:
86-
events, timeout = *args
87-
raise ArgumentError, 'Events must be positive integer!' if events <= 0
88-
res = Truffle::IOOperations.poll(self, events, timeout)
89-
# return events as bit mask
90-
res == 0 ? nil : res
91-
end
92-
end
9322
end

src/main/ruby/truffleruby/core/io.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,6 +2289,76 @@ def ungetc(obj)
22892289
nil
22902290
end
22912291

2292+
def wait_readable(timeout = nil)
2293+
ensure_open_and_readable
2294+
Truffle::IOOperations.poll(self, IO::READABLE, timeout) > 0 ? self : nil
2295+
end
2296+
2297+
def wait_writable(timeout = nil)
2298+
ensure_open_and_writable
2299+
Truffle::IOOperations.poll(self, IO::WRITABLE, timeout) > 0 ? self : nil
2300+
end
2301+
2302+
def wait_priority(timeout = nil)
2303+
ensure_open_and_readable
2304+
Truffle::IOOperations.poll(self, IO::PRIORITY, timeout) > 0 ? self : nil
2305+
end
2306+
2307+
# call-seq:
2308+
# io.wait(events, timeout) -> event mask, false or nil
2309+
# io.wait(timeout = nil, mode = :read) -> self, true, or false
2310+
#
2311+
# Waits until the IO becomes ready for the specified events and returns the
2312+
# subset of events that become ready, or a falsy value when times out.
2313+
#
2314+
# The events can be a bit mask of +IO::READABLE+, +IO::WRITABLE+ or
2315+
# +IO::PRIORITY+.
2316+
#
2317+
# Returns a truthy value immediately when buffered data is available.
2318+
#
2319+
# Optional parameter +mode+ is one of +:read+, +:write+, or
2320+
# +:read_write+.
2321+
def wait(*args)
2322+
ensure_open
2323+
2324+
if args.size != 2 || Primitive.is_a?(args[0], Symbol) || Primitive.is_a?(args[1], Symbol)
2325+
# Slow/messy path:
2326+
timeout = :undef
2327+
events = 0
2328+
args.each do |arg|
2329+
if Primitive.is_a?(arg, Symbol)
2330+
events |= case arg
2331+
when :r, :read, :readable then IO::READABLE
2332+
when :w, :write, :writable then IO::WRITABLE
2333+
when :rw, :read_write, :readable_writable then IO::READABLE | IO::WRITABLE
2334+
else
2335+
raise ArgumentError, "unsupported mode: #{arg}"
2336+
end
2337+
2338+
elsif timeout == :undef
2339+
timeout = arg
2340+
else
2341+
raise ArgumentError, 'timeout given more than once'
2342+
end
2343+
end
2344+
2345+
timeout = nil if timeout == :undef
2346+
2347+
events = IO::READABLE if events == 0
2348+
2349+
res = Truffle::IOOperations.poll(self, events, timeout)
2350+
res == 0 ? nil : self
2351+
else
2352+
# args.size == 2 and neither are symbols
2353+
# This is the fast path and the new interface:
2354+
events, timeout = *args
2355+
raise ArgumentError, 'Events must be positive integer!' if events <= 0
2356+
res = Truffle::IOOperations.poll(self, events, timeout)
2357+
# return events as bit mask
2358+
res == 0 ? nil : res
2359+
end
2360+
end
2361+
22922362
def write(*objects)
22932363
bytes_written = 0
22942364

0 commit comments

Comments
 (0)