Skip to content

Commit 6bca0ff

Browse files
committed
Add Ractor::Port (without test)
1 parent b951eb9 commit 6bca0ff

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed

core/ractor.rbs

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,166 @@ class Ractor
621621
def method_missing: (*untyped) -> untyped
622622
end
623623

624+
# <!-- rdoc-file=ractor.rb -->
625+
# Port objects transmit messages between Ractors.
626+
#
627+
class Port[T = untyped]
628+
# <!--
629+
# rdoc-file=ractor.rb
630+
# - <<(obj, move: false)
631+
# -->
632+
#
633+
alias << send
634+
635+
# <!--
636+
# rdoc-file=ractor.rb
637+
# - port.close
638+
# -->
639+
# Close the port. On the closed port, sending is not prohibited. Receiving is
640+
# also not allowed if there is no sent messages arrived before closing.
641+
#
642+
# port = Ractor::Port.new
643+
# Ractor.new port do |port|
644+
# port.send 1 # OK
645+
# port.send 2 # OK
646+
# port.close
647+
# port.send 3 # raise Ractor::ClosedError
648+
# end
649+
#
650+
# port.receive #=> 1
651+
# port.receive #=> 2
652+
# port.receive #=> raise Ractor::ClosedError
653+
#
654+
# Now, only a Ractor which creates the port is allowed to close ports.
655+
#
656+
# port = Ractor::Port.new
657+
# Ractor.new port do |port|
658+
# port.close #=> closing port by other ractors is not allowed (Ractor::Error)
659+
# end.join
660+
#
661+
def close: () -> void
662+
663+
# <!--
664+
# rdoc-file=ractor.rb
665+
# - port.closed? -> true/false
666+
# -->
667+
# Return the port is closed or not.
668+
#
669+
def closed?: () -> bool
670+
671+
# <!--
672+
# rdoc-file=ractor.rb
673+
# - port.inspect -> string
674+
# -->
675+
#
676+
def inspect: () -> String
677+
678+
# <!--
679+
# rdoc-file=ractor.rb
680+
# - port.receive -> msg
681+
# -->
682+
# Receive a message to the port (which was sent there by Port#send).
683+
#
684+
# port = Ractor::Port.new
685+
# r = Ractor.new port do |port|
686+
# port.send('message1')
687+
# end
688+
#
689+
# v1 = port.receive
690+
# puts "Received: #{v1}"
691+
# r.join
692+
# # Here will be printed: "Received: message1"
693+
#
694+
# The method blocks if the message queue is empty.
695+
#
696+
# port = Ractor::Port.new
697+
# r = Ractor.new port do |port|
698+
# wait
699+
# puts "Still not received"
700+
# port.send('message1')
701+
# wait
702+
# puts "Still received only one"
703+
# port.send('message2')
704+
# end
705+
# puts "Before first receive"
706+
# v1 = port.receive
707+
# puts "Received: #{v1}"
708+
# v2 = port.receive
709+
# puts "Received: #{v2}"
710+
# r.join
711+
#
712+
# Output:
713+
#
714+
# Before first receive
715+
# Still not received
716+
# Received: message1
717+
# Still received only one
718+
# Received: message2
719+
#
720+
# If close_incoming was called on the ractor, the method raises
721+
# Ractor::ClosedError if there are no more messages in the message queue:
722+
#
723+
# port = Ractor::Port.new
724+
# port.close
725+
# port.receive #=> raise Ractor::ClosedError
726+
#
727+
def receive: () -> T
728+
729+
# <!--
730+
# rdoc-file=ractor.rb
731+
# - port.send(msg, move: false) -> self
732+
# -->
733+
# Send a message to a port to be accepted by port.receive.
734+
#
735+
# port = Ractor::Port.new
736+
# r = Ractor.new do
737+
# r.send 'message'
738+
# end
739+
# value = port.receive
740+
# puts "Received #{value}"
741+
# # Prints: "Received: message"
742+
#
743+
# The method is non-blocking (will return immediately even if the ractor is not
744+
# ready to receive anything):
745+
#
746+
# port = Ractor::Port.new
747+
# r = Ractor.new(port) do |port|
748+
# port.send 'test'}
749+
# puts "Sent successfully"
750+
# # Prints: "Sent successfully" immediately
751+
# end
752+
#
753+
# An attempt to send to a port which already closed its execution will raise
754+
# Ractor::ClosedError.
755+
#
756+
# r = Ractor.new {Ractor::Port.new}
757+
# r.join
758+
# p r
759+
# # "#<Ractor:#6 (irb):23 terminated>"
760+
# port = r.value
761+
# port.send('test') # raise Ractor::ClosedError
762+
#
763+
# If the `obj` is unshareable, by default it will be copied into the receiving
764+
# ractor by deep cloning.
765+
#
766+
# If the object is shareable, it only send a reference to the object without
767+
# cloning.
768+
#
769+
def send: (T obj, ?move: boolish) -> self
770+
771+
private
772+
773+
# <!--
774+
# rdoc-file=ractor_sync.c
775+
# - Ractor::Port.new -> new_port
776+
# -->
777+
# Returns a new Ractor::Port object.
778+
#
779+
def initialize: () -> void
780+
781+
def initialize_copy: (untyped) -> untyped
782+
end
783+
624784
# <!-- rdoc-file=ractor.c -->
625785
# Raised on attempt to Ractor#take if there was an uncaught exception in the
626786
# Ractor. Its `cause` will contain the original exception, and `ractor` is the

0 commit comments

Comments
 (0)