+%Concurrency
+%Thadeu Cascardo
+
+# Races
+
+# Classical increment examples
+
+* Threads are incrementing a counter
+* Thread A reads the counter from memory
+* Thread A is interrupted
+* Thread B reads the counter from memory
+* Thread B increments the counter
+* Thread B saves the counter to memory
+* Thread A executes and now has the wrong value in its registers
+
+# Critical section
+
+In the last example, the section of code that increments the *global*
+counter is a critical section.
+
+# Shared data
+
+The problem in here is sharing data with multiple threads. When sharing,
+we need to serialize the access, that is, only one thread may execute
+the critical section at a time.
+
+# Classical problems
+
+* The Philosophers' Dinner
+* The Barber Shop
+* Consumer and Producer
+
+# Linux historical race handling
+
+* cli/sti
+* BKL
+* semaphores/old mutexes
+* spinlocks
+* RCU
+* new mutexes
+
+# Semaphores and mutexes
+
+# Semaphores
+
+* include linux/semaphore.h
+* kernel/semaphore.c
+* down(sem)
+* up(sem)
+* sema\\_init(sem, val)
+* init\\_MUTEX(sem)
+* init\\_MUTEX\\_LOCKED(sem)
+* DECLARE\\_MUTEX(varname)
+* down\\_interruptible(sem)
+
+# New mutex
+
+* Ingo Molnar's design
+* Documented at Documentation/mutex-design.txt
+
+# How to use the new mutex
+
+* struct mutex
+* mutex\\_init(mtx)
+* DEFINE\\_MUTEX(varname)
+* mutex\\_lock
+* mutex\\_unlock
+
+# When you should or should not use them
+
+* They both sleep, so take care: no atomic contexts
+* When the critical section may sleep
+
+# Spinlocks
+
+* include linux/spinlock.h
+* Documentation/spinlocks.txt
+
+# How to declare them
+
+* DEFINE\\_SPINLOCK
+* spinlock\\_t
+* spin\\_lock\\_init
+
+# How to use them
+
+* spin\\_lock
+* spin\\_unlock
+* spin\\_lock\\_irqsave
+* spin\\_unlock\\_irqrestore
+
+# When you should and should not use them
+
+* They can be used in atomic contexts
+* They lock the processor when "waiting", so use for fast critical
+ sections
+* They create an atomic context themselves, so *do not* sleep while
+ using them
+
+