36
Ruby Memory Model Hari Krishnan @harikrishnan83

Ruby memory model

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Ruby memory model

Ruby Memory ModelHari Krishnan

@harikrishnan83

Page 2: Ruby memory model

Why this talk?

Page 3: Ruby memory model

Memory model is not about

Page 4: Ruby memory model
Page 5: Ruby memory model
Page 6: Ruby memory model

A quick exercise

Page 7: Ruby memory model
Page 8: Ruby memory model
Page 9: Ruby memory model

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.

Page 10: Ruby memory model

Thread A Thread B

@count = 0Load @count = 0

Increment

Store

Load

Increment

Store

@count = 1

@count = 2

@count = 1

Page 11: Ruby memory model

Thread A Thread B

@count = 0Load @count = 0

Increment

Store

Load

Increment

Store@count = 1

Page 12: Ruby memory model

Where does reordering happen

Core 1

Cache

Core 2

Cache

Compiler

Memory

Page 13: Ruby memory model

What is a memory model?A memory model describes the interactions of threads through memory

and their shared use of the data

Page 14: Ruby memory model

Atomicity

Page 15: Ruby memory model

Visibility

Page 16: Ruby memory model

Ordering

Page 17: Ruby memory model

Should rubyists care about this?

Ruby is single threaded

Page 18: Ruby memory model

Are single threaded languages like Ruby

thread safe?

Page 19: Ruby memory model

Does single core imply thread safe?

Page 20: Ruby memory model

Concurrency is about InterleavingsThread A - Load

Thread A - Increment

Thread B - Load

Thread A - Store

Thread B - Increment

Thread B - Store

Page 21: Ruby memory model

Threading in Ruby

Page 22: Ruby memory model

How is Ruby made Single Threaded

● 1.8 - Green Threads - Global Interpreter Lock

● 1.9 - OS Threads - Global VM Lock

Page 23: Ruby memory model

Does GIL make you thread safe?

Page 24: Ruby memory model

How does GIL work?

Ruby Thread 1 Ruby Thread 2

OS Thread 1 OS 2

Timer ThreadWake up!Interrupt

Page 25: Ruby memory model

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

Page 26: Ruby memory model

Now do you still think it is thread safe?

Page 27: Ruby memory model

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

Page 28: Ruby memory model

Ruby does not have a documented Memory

Model

Page 29: Ruby memory model

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 -

Page 30: Ruby memory model

What do we do?

Page 31: Ruby memory model

Start Engineering Code for Thread Safety

Page 32: Ruby memory model
Page 33: Ruby memory model

Do not leave innocent accessors lying around

Page 34: Ruby memory model

Methods that mutate parameters are generally

dangerous

Page 36: Ruby memory model

Thank you!Hari Krishnan

@harikrishnan83