Skip to content

Commit 8e7f02e

Browse files
committed
Add Ractor#monitor and Ractor#unmonitor
1 parent 8c70579 commit 8e7f02e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

core/ractor.rbs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,25 @@ class Ractor
453453
#
454454
def name: () -> String?
455455

456+
# <!--
457+
# rdoc-file=ractor.rb
458+
# - ractor.monitor(port) -> self
459+
# -->
460+
# Register port as a monitoring port. If the ractor terminated, the port
461+
# received a Symbol object. :exited will be sent if the ractor terminated
462+
# without an exception. :aborted will be sent if the ractor terminated with a
463+
# exception.
464+
#
465+
# r = Ractor.new{ some_task() }
466+
# r.monitor(port = Ractor::Port.new)
467+
# port.receive #=> :exited and r is terminated
468+
#
469+
# r = Ractor.new{ raise "foo" }
470+
# r.monitor(port = Ractor::Port.new)
471+
# port.receive #=> :terminated and r is terminated with an exception "foo"
472+
#
473+
def monitor: [T < Symbol] (Port[T]) -> untyped
474+
456475
# <!--
457476
# rdoc-file=ractor.rb
458477
# - ractor.send(msg) -> self
@@ -468,6 +487,14 @@ class Ractor
468487
#
469488
alias to_s inspect
470489

490+
# <!--
491+
# rdoc-file=ractor.rb
492+
# - ractor.unmonitor(port) -> self
493+
# -->
494+
# Unregister port from the monitoring ports.
495+
#
496+
def unmonitor: (Port[untyped]) -> self
497+
471498
# <!--
472499
# rdoc-file=ractor.rb
473500
# - ractor.value -> obj

test/stdlib/Ractor_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ def test_name
116116
named, :name
117117
end
118118

119+
def test_monitor
120+
ractor = Ractor.new { sleep(0.1) }
121+
122+
assert_send_type(
123+
"(::Ractor::Port[untyped]) -> untyped",
124+
ractor, :monitor, Ractor::Port.new
125+
)
126+
end
127+
119128
def test_send
120129
r = Ractor.new { sleep }
121130

@@ -132,6 +141,19 @@ def test_to_s
132141
Ractor.current, :to_s
133142
end
134143

144+
def test_unmonitor
145+
ractor = Ractor.new { sleep(0.1) }
146+
147+
port = Ractor::Port.new
148+
149+
ractor.monitor(port)
150+
151+
assert_send_type(
152+
"(::Ractor::Port[untyped]) -> ::Ractor",
153+
ractor, :unmonitor, port
154+
)
155+
end
156+
135157
def test_value
136158
ractor = Ractor.new { 123 }
137159

0 commit comments

Comments
 (0)