1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

doc/ractor.md: Fix indentation in code blocks

Remove unnecessary indentation of code in code blocks
(it is also not rendered properly in the generated HTML).

Also remove an empty line.
This commit is contained in:
Marcus Stollsteimer 2020-12-26 22:51:48 +01:00
parent 0a867315e8
commit ab6adf2772

View file

@ -248,18 +248,17 @@ Connection example: Ractor.yield(obj) on r1 and r2,
``` ```
```ruby ```ruby
r = Ractor.new do r = Ractor.new do
msg = Ractor.receive # Receive from r's incoming queue msg = Ractor.receive # Receive from r's incoming queue
msg # send back msg as block return value msg # send back msg as block return value
end end
r.send 'ok' # Send 'ok' to r's incoming port -> incoming queue r.send 'ok' # Send 'ok' to r's incoming port -> incoming queue
r.take # Receive from r's outgoing port r.take # Receive from r's outgoing port
``` ```
The last example shows the following ractor network. The last example shows the following ractor network.
``` ```
+------+ +---+ +------+ +---+
* main |------> * r *---+ * main |------> * r *---+
+-----+ +---+ | +-----+ +---+ |
@ -270,16 +269,16 @@ The last example shows the following ractor network.
And this code can be rewrite more simple way by using an argument for `Ractor.new`. And this code can be rewrite more simple way by using an argument for `Ractor.new`.
```ruby ```ruby
# Actual argument 'ok' for `Ractor.new()` will be send to created Ractor. # Actual argument 'ok' for `Ractor.new()` will be send to created Ractor.
r = Ractor.new 'ok' do |msg| r = Ractor.new 'ok' do |msg|
# Values for formal parameters will be received from incoming queue. # Values for formal parameters will be received from incoming queue.
# Similar to: msg = Ractor.receive # Similar to: msg = Ractor.receive
msg # Return value of the given block will be sent via outgoing port msg # Return value of the given block will be sent via outgoing port
end end
# receive from the r's outgoing port. # receive from the r's outgoing port.
r.take #=> `ok` r.take #=> `ok`
``` ```
### Return value of a block for `Ractor.new` ### Return value of a block for `Ractor.new`
@ -338,27 +337,27 @@ as.sort == ['r1', 'r2'] #=> true
Complex example: Complex example:
```ruby ```ruby
pipe = Ractor.new do pipe = Ractor.new do
loop do loop do
Ractor.yield Ractor.receive Ractor.yield Ractor.receive
end
end end
end
RN = 10 RN = 10
rs = RN.times.map{|i| rs = RN.times.map{|i|
Ractor.new pipe, i do |pipe, i| Ractor.new pipe, i do |pipe, i|
msg = pipe.take msg = pipe.take
msg # ping-pong msg # ping-pong
end end
} }
RN.times{|i| RN.times{|i|
pipe << i pipe << i
} }
RN.times.map{ RN.times.map{
r, n = Ractor.select(*rs) r, n = Ractor.select(*rs)
rs.delete r rs.delete r
n n
}.sort #=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] }.sort #=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
``` ```
Multiple Ractors can send to one Ractor. Multiple Ractors can send to one Ractor.
@ -367,22 +366,22 @@ Multiple Ractors can send to one Ractor.
# Create 10 ractors and they send objects to pipe ractor. # Create 10 ractors and they send objects to pipe ractor.
# pipe ractor yield received objects # pipe ractor yield received objects
pipe = Ractor.new do pipe = Ractor.new do
loop do loop do
Ractor.yield Ractor.receive Ractor.yield Ractor.receive
end
end end
end
RN = 10 RN = 10
rs = RN.times.map{|i| rs = RN.times.map{|i|
Ractor.new pipe, i do |pipe, i| Ractor.new pipe, i do |pipe, i|
pipe << i pipe << i
end end
} }
RN.times.map{ RN.times.map{
pipe.take pipe.take
}.sort #=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] }.sort #=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
``` ```
TODO: Current `Ractor.select()` has the same issue of `select(2)`, so this interface should be refined. TODO: Current `Ractor.select()` has the same issue of `select(2)`, so this interface should be refined.
@ -404,34 +403,34 @@ TODO: `select` syntax of go-language uses round-robin technique to make fair sch
Example (try to take from closed Ractor): Example (try to take from closed Ractor):
```ruby ```ruby
r = Ractor.new do r = Ractor.new do
'finish' 'finish'
end end
r.take # success (will return 'finish') r.take # success (will return 'finish')
begin begin
o = r.take # try to take from closed Ractor o = r.take # try to take from closed Ractor
rescue Ractor::ClosedError rescue Ractor::ClosedError
'ok' 'ok'
else else
"ng: #{o}" "ng: #{o}"
end end
``` ```
Example (try to send to closed (terminated) Ractor): Example (try to send to closed (terminated) Ractor):
```ruby ```ruby
r = Ractor.new do r = Ractor.new do
end end
r.take # wait terminate r.take # wait terminate
begin begin
r.send(1) r.send(1)
rescue Ractor::ClosedError rescue Ractor::ClosedError
'ok' 'ok'
else else
'ng' 'ng'
end end
``` ```
When multiple Ractors waiting for `Ractor.yield()`, `Ractor#close_outgoing` will cancel all blocking by raise an exception (`ClosedError`). When multiple Ractors waiting for `Ractor.yield()`, `Ractor#close_outgoing` will cancel all blocking by raise an exception (`ClosedError`).
@ -494,19 +493,19 @@ end
``` ```
```ruby ```ruby
# move with Ractor.yield # move with Ractor.yield
r = Ractor.new do r = Ractor.new do
obj = 'hello' obj = 'hello'
Ractor.yield obj, move: true Ractor.yield obj, move: true
obj << 'world' # raise Ractor::MovedError obj << 'world' # raise Ractor::MovedError
end end
str = r.take str = r.take
begin begin
r.take r.take
rescue Ractor::RemoteError rescue Ractor::RemoteError
p str #=> "hello" p str #=> "hello"
end end
``` ```
Some objects are not supported to move, and an exception will be raise. Some objects are not supported to move, and an exception will be raise.
@ -549,16 +548,16 @@ Note that without using Ractors, these additional semantics is not needed (100%
Only the main Ractor (a Ractor created at starting of interpreter) can access global variables. Only the main Ractor (a Ractor created at starting of interpreter) can access global variables.
```ruby ```ruby
$gv = 1 $gv = 1
r = Ractor.new do r = Ractor.new do
$gv $gv
end end
begin begin
r.take r.take
rescue Ractor::RemoteError => e rescue Ractor::RemoteError => e
e.cause.message #=> 'can not access global variables from non-main Ractors' e.cause.message #=> 'can not access global variables from non-main Ractors'
end end
``` ```
Note that some special global variables are ractor-local, like `$stdin`, `$stdout`, `$stderr`. See [[Bug #17268]](https://bugs.ruby-lang.org/issues/17268) for more details. Note that some special global variables are ractor-local, like `$stdin`, `$stdout`, `$stderr`. See [[Bug #17268]](https://bugs.ruby-lang.org/issues/17268) for more details.
@ -568,37 +567,37 @@ Note that some special global variables are ractor-local, like `$stdin`, `$stdou
Only the main Ractor can access instance variables of shareable objects. Only the main Ractor can access instance variables of shareable objects.
```ruby ```ruby
class C
@iv = 'str'
end
r = Ractor.new do
class C class C
@iv = 'str' p @iv
end
r = Ractor.new do
class C
p @iv
end
end end
end
begin begin
r.take r.take
rescue => e rescue => e
e.class #=> Ractor::IsolationError e.class #=> Ractor::IsolationError
end end
``` ```
```ruby ```ruby
shared = Ractor.new{} shared = Ractor.new{}
shared.instance_variable_set(:@iv, 'str') shared.instance_variable_set(:@iv, 'str')
r = Ractor.new shared do |shared| r = Ractor.new shared do |shared|
p shared.instance_variable_get(:@iv) p shared.instance_variable_get(:@iv)
end end
begin begin
r.take r.take
rescue Ractor::RemoteError => e rescue Ractor::RemoteError => e
e.cause.message #=> can not access instance variables of shareable objects from non-main Ractors (Ractor::IsolationError) e.cause.message #=> can not access instance variables of shareable objects from non-main Ractors (Ractor::IsolationError)
end end
``` ```
Note that instance variables for class/module objects are also prohibited on Ractors. Note that instance variables for class/module objects are also prohibited on Ractors.
@ -608,22 +607,22 @@ Note that instance variables for class/module objects are also prohibited on Rac
Only the main Ractor can access class variables. Only the main Ractor can access class variables.
```ruby ```ruby
class C
@@cv = 'str'
end
r = Ractor.new do
class C class C
@@cv = 'str' p @@cv
end
r = Ractor.new do
class C
p @@cv
end
end end
end
begin begin
r.take r.take
rescue => e rescue => e
e.class #=> Ractor::IsolationError e.class #=> Ractor::IsolationError
end end
``` ```
### Constants ### Constants
@ -631,32 +630,32 @@ Only the main Ractor can access class variables.
Only the main Ractor can read constants which refer to the unshareable object. Only the main Ractor can read constants which refer to the unshareable object.
```ruby ```ruby
class C class C
CONST = 'str' CONST = 'str'
end end
r = Ractor.new do r = Ractor.new do
C::CONST C::CONST
end end
begin begin
r.take r.take
rescue => e rescue => e
e.class #=> Ractor::IsolationError e.class #=> Ractor::IsolationError
end end
``` ```
Only the main Ractor can define constants which refer to the unshareable object. Only the main Ractor can define constants which refer to the unshareable object.
```ruby ```ruby
class C class C
end end
r = Ractor.new do r = Ractor.new do
C::CONST = 'str' C::CONST = 'str'
end end
begin begin
r.take r.take
rescue => e rescue => e
e.class #=> Ractor::IsolationError e.class #=> Ractor::IsolationError
end end
``` ```
To make multi-ractor supported library, the constants should only refer sharable objects. To make multi-ractor supported library, the constants should only refer sharable objects.