Ruby Memory Model
Hari Krishnan
@harikrishnan83
Why this talk?
Memory model is not
about
A quick exercise
Increment operation is not a single
instruction
● Retrieve the current value of @count.
● Increment the retrieved value by...
Thread A Thread B
@count = 0Load @count = 0
Increment
Store
Load
Increment
Store
@count = 1
@count = 2
@count = 1
Thread A Thread B
@count = 0Load @count = 0
Increment
Store
Load
Increment
Store@count = 1
Where does reordering happen
Core 1
Cache
Core 2
Cache
Compiler
Memory
What is a memory model?
A memory model describes the interactions of threads through memory
and their shared use of the da...
Atomicity
Visibility
Ordering
Should rubyists care
about this?
Ruby is single threaded
Are single threaded
languages like Ruby
thread safe?
Does single core imply
thread safe?
Concurrency is about Interleavings
Thread A - Load
Thread A - Increment
Thread B - Load
Thread A - Store
Thread B - Increm...
Threading in Ruby
How is Ruby made Single Threaded
● 1.8 - Green Threads - Global Interpreter
Lock
● 1.9 - OS Threads - Global VM Lock
Does GIL make you
thread safe?
How does GIL work?
Ruby Thread 1 Ruby Thread 2
OS Thread 1 OS 2
Timer Thread
Wake up!Interrupt
GIL Details
● GIL is acquired at start of Thread block and
released once done
● To ensure fairness a timer thread sends an...
Now do you still think it is
thread safe?
Never depend on GIL for Thread
Safety
● GIL does not have a specification
● It also ties us to MRI
● Rubinius and JRuby do...
Ruby does not have a
documented Memory
Model
Ruby right now depends on
underlying Virtual Machines
● MRI is not much of an interpreter. It is a VM.
- No specification ...
What do we do?
Start Engineering Code
for Thread Safety
Do not leave innocent
accessors lying around
Methods that mutate
parameters are generally
dangerous
Gems to the rescue
● atomic - https://github.com/headius/ruby-
atomic
● thread_safe - https://github.
com/headius/thread_s...
Thank you!
Hari Krishnan
@harikrishnan83
Ruby memory model
Ruby memory model
Ruby memory model
Ruby memory model
Ruby memory model
Upcoming SlideShare
Loading in...5
×

Ruby memory model

264

Published on

Published in: Software
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
264
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Ruby memory model"

  1. 1. Ruby Memory Model Hari Krishnan @harikrishnan83
  2. 2. Why this talk?
  3. 3. Memory model is not about
  4. 4. A quick exercise
  5. 5. Increment operation is not a single instruction ● Retrieve the current value of @count. ● Increment the retrieved value by 1. ● Store the incremented value back in @count.
  6. 6. Thread A Thread B @count = 0Load @count = 0 Increment Store Load Increment Store @count = 1 @count = 2 @count = 1
  7. 7. Thread A Thread B @count = 0Load @count = 0 Increment Store Load Increment Store@count = 1
  8. 8. Where does reordering happen Core 1 Cache Core 2 Cache Compiler Memory
  9. 9. What is a memory model? A memory model describes the interactions of threads through memory and their shared use of the data
  10. 10. Atomicity
  11. 11. Visibility
  12. 12. Ordering
  13. 13. Should rubyists care about this? Ruby is single threaded
  14. 14. Are single threaded languages like Ruby thread safe?
  15. 15. Does single core imply thread safe?
  16. 16. Concurrency is about Interleavings Thread A - Load Thread A - Increment Thread B - Load Thread A - Store Thread B - Increment Thread B - Store
  17. 17. Threading in Ruby
  18. 18. How is Ruby made Single Threaded ● 1.8 - Green Threads - Global Interpreter Lock ● 1.9 - OS Threads - Global VM Lock
  19. 19. Does GIL make you thread safe?
  20. 20. How does GIL work? Ruby Thread 1 Ruby Thread 2 OS Thread 1 OS 2 Timer Thread Wake up!Interrupt
  21. 21. GIL Details ● GIL is acquired at start of Thread block and released once done ● To ensure fairness a timer thread sends an interrupt to the Thread holding GIL when other threads are waiting ● The Thread holding GIL may choose to release it based on many parameters Refer to this excellent blog post by Jesse Storimer - http://www.jstorimer.com/blogs/workingwithcode/8085491- nobody-understands-the-gil
  22. 22. Now do you still think it is thread safe?
  23. 23. Never depend on GIL for Thread Safety ● GIL does not have a specification ● It also ties us to MRI ● Rubinius and JRuby do not have GIL
  24. 24. Ruby does not have a documented Memory Model
  25. 25. Ruby right now depends on underlying Virtual Machines ● MRI is not much of an interpreter. It is a VM. - No specification for Memory Model ● JRuby - JVM - JSR 133 ● Rubinius - LLVM for JIT -
  26. 26. What do we do?
  27. 27. Start Engineering Code for Thread Safety
  28. 28. Do not leave innocent accessors lying around
  29. 29. Methods that mutate parameters are generally dangerous
  30. 30. Gems to the rescue ● atomic - https://github.com/headius/ruby- atomic ● thread_safe - https://github. com/headius/thread_safe
  31. 31. Thank you! Hari Krishnan @harikrishnan83
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×