Slide 1

Slide 1 text

Ruby Memory Model Hari Krishnan @harikrishnan83

Slide 2

Slide 2 text

Why this talk?

Slide 3

Slide 3 text

Memory model is not about

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

A quick exercise

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

Thread A Thread B @count = 0 Load @count = 0 Increment Store Load Increment Store @count = 1 @count = 2 @count = 1

Slide 11

Slide 11 text

Thread A Thread B @count = 0 Load @count = 0 Increment Store Load Increment Store @count = 1

Slide 12

Slide 12 text

Where does reordering happen Core 1 Cache Core 2 Cache Compiler Memory

Slide 13

Slide 13 text

What is a memory model? A memory model describes the interactions of threads through memory and their shared use of the data

Slide 14

Slide 14 text

Atomicity

Slide 15

Slide 15 text

Visibility

Slide 16

Slide 16 text

Ordering

Slide 17

Slide 17 text

Should rubyists care about this? Ruby is single threaded

Slide 18

Slide 18 text

Are single threaded languages like Ruby thread safe?

Slide 19

Slide 19 text

Does single core imply thread safe?

Slide 20

Slide 20 text

Concurrency is about Interleavings Thread A - Load Thread A - Increment Thread B - Load Thread A - Store Thread B - Increment Thread B - Store

Slide 21

Slide 21 text

Threading in Ruby

Slide 22

Slide 22 text

How is Ruby made Single Threaded ● 1.8 - Green Threads - Global Interpreter Lock ● 1.9 - OS Threads - Global VM Lock

Slide 23

Slide 23 text

Does GIL make you thread safe?

Slide 24

Slide 24 text

How does GIL work? Ruby Thread 1 Ruby Thread 2 OS Thread 1 OS 2 Timer Thread Wake up! Interrupt

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Now do you still think it is thread safe?

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Ruby does not have a documented Memory Model

Slide 29

Slide 29 text

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 -

Slide 30

Slide 30 text

What do we do?

Slide 31

Slide 31 text

Start Engineering Code for Thread Safety

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Do not leave innocent accessors lying around

Slide 34

Slide 34 text

Methods that mutate parameters are generally dangerous

Slide 35

Slide 35 text

Gems to the rescue ● atomic - https://github.com/headius/ruby- atomic ● thread_safe - https://github. com/headius/thread_safe

Slide 36

Slide 36 text

Thank you! Hari Krishnan @harikrishnan83