![Page 2: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/2.jpg)
Today’s talk
•Supported features•Process•Thread•Fiber
•Features under consideration•Guild•Auto-Fiber
![Page 3: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/3.jpg)
Today’s talkProcess Guild Thread Auto-Fiber Fiber
Available Yes No Yes No Yes
Switch on time Yes Yes Yes No No
Switch on I/O Auto Auto Auto Auto No
Next target Auto Auto Auto Auto Specify
Parallel run Yes Yes No (on MRI) No No
Shared data N/A (mostly) N/A Everything Everything Everything
Comm. Hard Maybe Easy Easy Easy Easy
Programming difficulty
Hard Easy Difficult Easy Easy
Debugging difficulty
Easy? Maybe Easy Hard Maybe hard Easy
![Page 4: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/4.jpg)
Koichi Sasadahttp://atdot.net/~ko1/
•A programmer•2006-2012 Faculty•2012-2017 Heroku, Inc.•2017- Cookpad Inc.
•Job: MRI development•MRI: Matz Ruby Interpreter•Core parts• VM, Threads, GC, etc
![Page 5: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/5.jpg)
Ruby interpreter
Ruby (Rails) app
RubyGems/Bundler
So many gemssuch as Rails, pry, thin, … and so on.
Normal Ruby developer’s viewi gigantum umeris insidentesStanding on the shoulders of giants
![Page 6: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/6.jpg)
Interpret on RubyVM
Normal MRI developer’s view
6
Rubyscript
Parse
Compile(codegen)
RubyBytecode
Object managementGarbage collectorThreading
Embeddedclasses and methods
BundledLibraries
Evaluator
GemLibraries
ASTAbstract Syntax Tree
![Page 7: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/7.jpg)
Ruby interpreter
Ruby (Rails) app
RubyGems/Bundler
So many gemssuch as Rails, pry, thin, … and so on.
Koichi’s job
Koichi
![Page 8: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/8.jpg)
Ruby3: Ruby3 has 3 goals
•Static type checking
•Just-in-Time (JIT) compilation
•Parallel execution w/ highly abstract concurrent model
![Page 9: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/9.jpg)
Ruby3: Ruby3 has 3 goals
•For productivity•Static checking
•For performance• Just-in-Time (JIT) compilation•Parallel execution w/ highly abstract concurrent model
![Page 10: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/10.jpg)
Concurrency
“In computer science, concurrency is the decomposability property of a program, algorithm, or problem into order-independent or partially-ordered components or units.[1] This means that even if the concurrent units of the program, algorithm, or problem are executed out-of-order or in partial order, the final outcome will remain the same. This allows for parallel execution of the concurrent units, which can significantly improve overall speed of the execution in multi-processor and multi-core systems.”
https://en.wikipedia.org/wiki/Concurrency_(computer_science)
![Page 11: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/11.jpg)
Concurrent and Parallel execution
Concurrentexecution
Logical concept
Parallel(and concurrent)
executionPhysical concept
Ruby (MRI) support only concurrency
Task A
Task B
Task A
Task B
![Page 12: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/12.jpg)
ConcurrencyWhy needed?
•Easy to write some kind of programs•Download files simultaneously•Process web requests simultaneously•Agent simulation (assume computer games)•Each agent has its own logics•Run agents simultaneously
![Page 13: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/13.jpg)
ConcurrencyExample: Downloader
Download A
Download B
Download C
wait for receving
wait for receving
wait for receving
post process
post process
post process
We can write this kind of program w/o concurrency support,but not simple, not easy
![Page 14: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/14.jpg)
Downloader exampleWith concurrency support (Thread)ts = URLs.map do |url|
Thread.new(url) do |u|
data = download(u)
File.write(u.to_fname, data)
end
end.each{|th| th.join} # wait
![Page 15: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/15.jpg)
Downloader exampleWithout concurrency support
# Serial execution
URLs.each do |u|
data = download(u)
File.write(u.to_fname, data)
end
![Page 16: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/16.jpg)
ConcurrencyNot concurrent case
Download A
Download B
Download C
wait for receving
wait for receving
post process
post process
… and download C after that
![Page 17: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/17.jpg)
Downloader exampleWithout concurrency support # Use select. Not so SIMPLE!!
fds = URLs.map do |u|
download_fd(u)
end
while ready_fds = select(fds)
ready_fds.each{|fd|
File.write(…, read(fd))}
end
![Page 18: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/18.jpg)
Existing concurrency supports on Ruby (MRI)
![Page 19: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/19.jpg)
Supported features by Ruby/MRI
•Process
•Thread
•Fiber
![Page 20: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/20.jpg)
ProcessTraditional concurrency support
![Page 21: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/21.jpg)
Process
•Use OS multi-process•Use fork on Unix-like systems
•Shared-nothing• Communicate with IPC (pipe, etc) such as IO.pipe
•Programming•Difficult to manage processes and IPC
•Debugging• Easy because a few synchronization bugs
![Page 22: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/22.jpg)
Inter-process communication
Ruby process Ruby process
pipe
file
obj
string
Serialize
obj
string
Deserialize
![Page 23: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/23.jpg)
Inter-process communicationExample code# Traditional multi-process example
r, w = IO.pipe
fork do
result_str = work_something.to_s
w.write result_str
w.close
end
puts r.read # wait for a result
![Page 24: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/24.jpg)
Sophisticated libraries/frameworks for process programming
•dRuby: Distributed object for Ruby
•parallel gem: Parallel programming with processes
•unicorn: Process based web application server (master – worker model w/ processes)
![Page 25: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/25.jpg)
ThreadRuby’s native concurrency support
![Page 26: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/26.jpg)
Thread
•Use Ruby managed threads• Thread.new do … end
•Shared-everything• Communication is very easy
•Programming• Easy to make, easy to communicate (at a glance)•Difficult to make completely safe program
•Debugging•Hard because of synchronization
![Page 27: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/27.jpg)
MRI: Thread with Giant Lock (GIL)
•Only a thread keeping the GIL can run (can’t run in parallel)
Thread 1
Thread 2
Thread 1
CPU 1
CPU 2IDLE
OS Thread 1
OS Thread 2
LockLock
![Page 28: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/28.jpg)
Inter-thread communicationEasy to share objects
Ruby process
objThread Thread
We can share objects directlybetween threads very easily
![Page 29: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/29.jpg)
Inter-thread communication
v = Object.new
$g = Object.new
Thread.new do
p [v, $g]
end
p [v, $g]
![Page 30: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/30.jpg)
Thread programmingSynchronization is required
•Reading/writing data simultaneously w/o synchronization will cause serious problem•Race condition•Data race
![Page 31: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/31.jpg)
Mutate shared objectsLucky case
Ruby process
Thread
AThread
BSpeakerObject
@name = ‘ko1’
@gender = ‘male’
(1) Thread A tries to change the Speakerto “Yuki” (female)
Note: Yuki is my wife.
![Page 32: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/32.jpg)
Mutate shared objectsLucky case
Ruby process
Thread
AThread
BSpeakerObject
@name = ‘Yuki’
@gender = ‘male’
(2) A changes the name to “Yuki”
write
![Page 33: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/33.jpg)
Mutate shared objectsLucky case
Ruby process
Thread
AThread
BSpeakerObject
@name = ‘Yuki’
@gender = ‘female’
(3) A changes the gender to “female”
write
![Page 34: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/34.jpg)
Mutate shared objectsLucky case
Ruby process
Thread
AThread
BSpeakerObject
@name = ‘Yuki’
@gender = ‘female’
(4) Complete.A and B can read correct speaker.
read read
![Page 35: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/35.jpg)
Mutate shared objectsProblematic case
Ruby process
Thread
AThread
BSpeakerObject
@name = ‘ko1’
@gender = ‘male’
(1) Thread A tries to change the Speakerto “Yuki” (female)
![Page 36: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/36.jpg)
Mutate shared objectsProblematic case
Ruby process
Thread
AThread
BSpeakerObject
@name = ‘Yuki’
@gender = ‘male’
(2) A changes the name to “Yuki”
write
![Page 37: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/37.jpg)
Mutate shared objectsProblematic case
Ruby process
Thread
AThread
BSpeakerObject
@name = ‘Yuki’
@gender = ‘male’
(3) Before the changing,B read incorrect data!!
Note: Yuki should be female.
read
![Page 38: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/38.jpg)
Inter-thread communicationSynchronization
•Require synchronization for shared data•Mutex, Queue and so on•Usually Queue is enough
•To prohibit simultaneous mutation•We need to keep consistency for each objects
![Page 39: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/39.jpg)
Mutate shared objectsWith lock
Ruby process
Thread
AThread
BSpeakerObject
@name = ‘ko1’
@gender = ‘male’
(1) Thread A tries to change the Speakerto “Yuki” (female). Lock an obj.
Locked by A
![Page 40: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/40.jpg)
Mutate shared objectsWith lock
Ruby process
Thread
AThread
BSpeakerObject
@name = ‘Yuki’
@gender = ‘male’
(2) A changes the name to “Yuki”
Locked by A
![Page 41: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/41.jpg)
Mutate shared objectsWith lock
Ruby process
Thread
AThread
BSpeakerObject
@name = ‘Yuki’
@gender = ‘male’
(3) Before complete the changing,B tries to read, but prohibited by a lock
Locked by A
read
![Page 42: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/42.jpg)
Thread programmingEasy to share data: Good and Bad•Good: Easy to communicate with threads•Bad: Too easy. Difficult to manage all of them•Mutation for shared data requires correct synchronization•Sometimes objects are shared implicitly•Otherwise, it causes serious problems
![Page 43: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/43.jpg)
“Why Threads Are A Bad Idea(for most purposes)”
• Quoted from John Ousterhout, 1995 (about 20 years ago ☺)
![Page 44: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/44.jpg)
Compare Process with Thread
Process Thread
Available Yes Yes
Switch on time Yes Yes
Switch on I/O Auto Auto
Next target Auto Auto
Parallel run Yes No (on MRI)
Shared data N/A Everything
Communication Hard (high-overhead) Easy (lightweight)
Programming difficulty Hard Difficult
Debugging difficulty Easy? Hard
![Page 45: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/45.jpg)
FiberUser-defined context switching
![Page 46: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/46.jpg)
Fiber exampleInfinite generator
fib = Fiber.new do
Fiber.yield a = b = 1
loop{ a, b = b, a+b
Fiber.yield a }
end
10.times{ p fib.resume }
![Page 47: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/47.jpg)
Fiber exampleInfinite generator
fib = Fiber.new do
Fiber.yield a = b = 1
loop{ a, b = b, a+b
Fiber.yield a }
end
10.times{ p fib.resume }
1. Fiber creation
2. Resume Fiber
3. Return to the parent fiber
4. Resume fiber (again)
5. Return to the parent fiber
6. Resume fiber (again2)
![Page 48: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/48.jpg)
Fiber exampleInfinite generator
fib = Fiber.new do
Fiber.yield a = b = 1
loop{ a, b = b, a+b
Fiber.yield a }
end
10.times{ p fib.resume }
1. Fiber creation
2. Resume Fiber
3. Return to the parent fiber
4. Resume fiber (again)
5. Return to the parent fiber
6. Resume fiber (again2)
![Page 49: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/49.jpg)
Not a Proc?
a = 0; b = 1
fib = Proc.new{
a, b = b, a+b
a
}
p fib.call #=> 1
p fib.call #=> 1
p fib.call #=> 2
p fib.call #=> 3
p fib.call #=> 5
Proc can’t restart fromthe middle of block
![Page 50: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/50.jpg)
Proc (method) v.s. FiberProc (method) Fiber
Start OK: call OK: Fiber#resume
Parameters OK: block (method) parameters OK: block parameters
Return OK: exit Proc/method OK: exit Proc/method
Suspend NG: N/A OK: Fiber.yield
Continue NG: N/A OK: Fiber#resume
![Page 51: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/51.jpg)
Fiber exampleInner iterator to external iteratorf1 = Fiber.new do
2.times{|i| Fiber.yield i}
end
p f1.resume #=> 0
p f1.resume #=> 1
p f1.resume #=> 2 # return value of #times
p f1.resume #=> dead fiber called
(FiberError)
![Page 52: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/52.jpg)
Fiber exampleInner iterator to external iteratoretc_passwd_ex_iter = Fiber.new do
open('/etc/passwd').each_line{|line|
Fiber.yield line
}
end
p etc_passwd_ex_iter.resume #=> 1st line
p etc_passwd_ex_iter.resume #=> 2nd line
…
![Page 53: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/53.jpg)
Fiber exampleInner iterator to external iterator# make Enumerator
iter = open('/etc/passwd').each_line
# Enumerator#next use Fiber implicitly
p iter.next #=> 1st line
p iter.next #=> 2nd line
…
![Page 54: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/54.jpg)
Fiber exampleAgent simulationcharacters << Fiber.new{
loop{cat.move_up; Fiber.yield}}
characters << Fiber.new{
loop{dog.move_left; Fiber.yield}}
…
loop{cs.each{|e| e.resume}; redraw}
![Page 55: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/55.jpg)
Fiber exampleAgent simulationcharacters << Fiber.new{
# you can specify complex rule for chars
loop{
cow.move_up; Fiber.yield
cow.move_right; Fiber.yield
cow.move_down; Fiber.yield
cow.move_left; Fiber.yield
}
}
![Page 56: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/56.jpg)
Fiber exampleNon-blocking IO scheduler
Wait multiple IO ops withtraditional “select” or
modern “poll”, “epoll” interface
![Page 57: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/57.jpg)
FiberProgramming difficulty•Good•Synchronization for shared data is not required
because of no unexpected switching• Lightweight than Processes and Threads
•Bad•We need to switch explicitly. For example,
“Blocking operations” (I/O blocking, etc) stop all fibers
![Page 58: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/58.jpg)
Comparison of existing supports
Process Thread Fiber
Available Yes Yes Yes
Switch on time Yes Yes No
Switch on I/O Auto Auto No
Next target Auto Auto Specify
Parallel run Yes No (on MRI) No
Shared data N/A Everything Everything
Comm. Hard Easy Easy
Programming difficulty Hard Difficult Easy
Debugging difficulty Easy? Hard Easy
![Page 59: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/59.jpg)
Fiber: Brief history
•2007/05/23 cont.c (for callcc)
•2007/05/25 Fiber impl. [ruby-dev:30827]
•2007/05/28 Fiber introduced into cont.c
•2007/08/25 Fix Fiber spec
•2017 is 10th anniversary I introduced ☺
![Page 60: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/60.jpg)
Proposed concurrency featuresGuildAuto-Fiber
![Page 61: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/61.jpg)
GuildProposed concurrency support for Ruby 3
![Page 62: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/62.jpg)
Key idea
Problem of multi-thread programming:
Easy to share mutable objects
Idea:
Prohibit sharing mutable objects
![Page 63: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/63.jpg)
Our goal for Ruby 3
•We need to keep compatibility with Ruby 2.•We can make parallel program.•We shouldn’t consider locks any more.•We can share objects with copy, but copy operation should be fast.•We should share immutable objects if we can.•We can provide special objects to share mutable objects like Clojure if we really need speed.
![Page 64: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/64.jpg)
Guild: New concurrency abstraction
•Guild has at least one thread (and a thread has at least one fiber)
Guild
Thread
Fiber
Guild
Thread
Fiber
Guild
Thread
Fiber
Fiber
Thread
Fiber
![Page 65: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/65.jpg)
Threads in different guilds can run in Parallel•Threads in different guilds can run in parallel•Threads in a same guild can not run in parallel because of GVL (or GGL: Giant Guild Lock)
G1:T1
G1:T2
G2:T3
Acquire GGL
Acquire GGL
![Page 66: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/66.jpg)
Important rule:Mutable Objects have a membership
•All of mutable objects should belong to only one Guild exclusively
•Guild can not touch objects belong to other guildsGuild 1 Guild 2
objobj
obj
obj
objCan’t access(read/write)
NG!!
![Page 67: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/67.jpg)
Object membership
Only one guild can access mutable object
→ We don’t need to consider locks(if Guild has only one thread)
![Page 68: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/68.jpg)
Inter-guild communication
•“Guild::Channel” to communicate each guilds
•Two communication methods1. Copy2. Move (transfer_membership)
![Page 69: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/69.jpg)
Copy using Channel
Guild1 Guild2
o2o3
o1channel
o2o3
o1
COPY
channel.transfer(o1) o1 = channel.receive
O2:Data
O3:Data
O2:Data
O3:Data
![Page 70: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/70.jpg)
Move using Channel
Guild1 Guild2
o2o3
o1channel
MOVE
channel.transfer_membership(o1) o1 = channel.receive
O2:Data
O3:Data
![Page 71: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/71.jpg)
Move using Channel
Guild1 Guild2
channel
o2o3
o1
MOVE
channel.transfer_membership(o1) o1 = channel.receive
O2:Data
O3:Data
--
-
From Guild1 perspective, transferred objects are invalidated
![Page 72: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/72.jpg)
Sharing immutable objectsWe can share reference to immutable objects
Guild1 Guild2
o2o3
o1
channel
channel.transfer(o1) o1 = channel.receive
O2:Data O3:Data
Ref to o1
If o1 is immutable, any Guild can read o1
read
Ref to o1
read
![Page 73: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/73.jpg)
Use-case 1: master – worker typedef fib(n) ... endg_fib = Guild.new(script: %q{
ch = Guild.default_channelwhile n, return_ch = ch.receive
return_ch.transfer fib(n)end
})
ch = Guild::Channel.newg_fib.transfer([3, ch])p ch.receive
Main Guild
Fibonacci Guild
ch
return_ch
n, return_ch
Answer of fib(n)
NOTE: Making other Fibonacci guilds,you can compute fib(n) in parallel
![Page 74: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/74.jpg)
Use-case 2: pipelineresult_ch = Guild::Channel.newg_pipe3 = Guild.new(script: %q{while obj = Guild.default_channel.receiveobj = modify_obj3(obj)Guild.argv[0].transfer_membership(obj)
end}, argv: [result_ch])g_pipe2 = Guild.new(script: %q{while obj = Guild.default_channel.receiveobj = modify_obj2(obj)Guild.argv[0].transfer_membership(obj)
end}, argv: [g_pipe3])g_pipe1 = Guild.new(script: %q{while obj = Guild.default_channel.receiveobj = modify_obj1(obj)Guild.argv[0].transfer_membership(obj)
end}, argv: [g_pipe2])
obj = SomeClass.new
g_pipe1.transfer_membership(obj)obj = result_ch.receive
Main Guild
Pipe 1Guild
obj
Obj’Moveand modify
Pipe 2Guild
Obj’’
Moveand modify
Pipe 3Guild
Obj’’’
Obj’’’
Moveand modify
Move
![Page 75: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/75.jpg)
Compare with Process, Guild, ThreadProcess Guild Thread
Available Yes No Yes
Switch on time Yes Yes Yes
Switch on I/O Auto Auto Auto
Next target Auto Auto Auto
Parallel run Yes Yes No (on MRI)
Shared data N/A (mostly) N/A Everything
Comm. Hard Maybe Easy Easy
Programming difficulty Hard Easy Difficult
Debugging difficulty Easy? Maybe Easy Hard
![Page 76: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/76.jpg)
Auto FiberAnother proposed concurrency support for Ruby 3
![Page 77: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/77.jpg)
Problem of FiberRequires explicit switching
•“Fiber” enables writing scheduler by programmer
→ Programmers need to write own scheduler•We need to manage blocking operations like I/O blocking
![Page 79: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/79.jpg)
Auto Fiber proposalAutomatic schedule on I/O blocking
• Support Fiber scheduler natively• Don’t need to return
scheduler
• Switch Fibers on all blocking I/O (and other ops)• No need to change existing
programs
![Page 80: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/80.jpg)
Advantage and Disadvantage
•Advantage•Don’t need to modify existing programs• Lightweight as a Fiber•Safer than Threads (no preemption)
•Disadvantage• Introduce “non-deterministic” dangers same as
Thread programs• Non atomic operations can intercept accidentally.
Change the name…?
![Page 81: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/81.jpg)
Compare w/ Thread and (auto-)FiberThread Auto-Fiber Fiber
Available Yes No Yes
Switch on time Yes No No
Switch on I/O Auto Auto No
Next target Auto Auto Specify
Parallel run No (on MRI) No No
Shared data Everything Everything Everything
Comm. Easy Easy Easy
Programming difficulty Difficult Easy Easy
Debugging difficulty Hard Maybe hard Easy
![Page 82: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/82.jpg)
Today’s talk
•Supported features•Process•Thread•Fiber
•Features under consideration•Guild•Auto-Fiber
![Page 83: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/83.jpg)
Today’s talkProcess Guild Thread Auto-Fiber Fiber
Available Yes No Yes No Yes
Switch on time Yes Yes Yes No No
Switch on I/O Auto Auto Auto Auto No
Next target Auto Auto Auto Auto Specify
Parallel run Yes Yes No (on MRI) No No
Shared data N/A (mostly) N/A Everything Everything Everything
Comm. Hard Maybe Easy Easy Easy Easy
Programming difficulty
Hard Easy Difficult Easy Easy
Debugging difficulty
Easy? Maybe Easy Hard Maybe hard Easy
![Page 84: Ruby's Concurrency Management: Now and Futureko1/activities/2017_RubyConfJakarta.pdf · Concurrency In computer science, concurrency is the decomposability property of a program,](https://reader033.vdocument.in/reader033/viewer/2022050606/5fad2ee576b3c44b6c7924dd/html5/thumbnails/84.jpg)
References
•Fiber: RubyKaigi 2017 http://rubykaigi.org/2017/presentations/ko1.html
•Guild: RubyConf 2016 https://www.youtube.com/watch?v=mjzmUUQWqco
•Auto-fiber: Feature #13618 https://bugs.ruby-lang.org/issues/13618