SlideShare a Scribd company logo
1 of 25
Download to read offline
An Introduction to Locks in Go
Cherie Hsieh

Golang Taiwan, Gopher Taipei
Source Code Versions
Go version 1.12

glibc version 2.29
Mutex in Go
Before discuss this topic, we briefly review the basic concept of some
fundamental locks.
Spin Lock
• Appropriate for multi-core processors
In uniprocessor, a waiting thread spend all its time
slice on spinning
• The most common lock in linux kernel
Spin lock is used in interrupt handlers which cannot
be rescheduled
• Hold a spin lock for a short time
To prevent heavy CPU consumption
Spin Lock
Question: Why is glibc spin lock unfair ?

int pthread_spin_lock (pthread_spinlock_t *lock)
{
  int val = 0;
  if (__glibc_likely(atomic_compare_exchange_weak_acquire (lock, &val, 1)))
    return 0;
  do
    {
      do
        {
          atomic_spin_nop(); // Important!! CPU pause
          val = atomic_load_relaxed(lock);
        }
      while (val != 0);
    }
  while (!atomic_compare_exchange_weak_acquire (lock, &val, 1));
  return 0;
}
Difference from ticket spin lock, the lock is not fair as it doesn’t guarantee FIFO
ordering amongst the threads competing for the lock. [source code]
Semaphore
• Sleeping locks
• Obtained only in process context which is schedulable.
• Allow for an arbitrary number of simultaneous lock holder.
// Example: glibc semaphore struct
// partial code from internaltypes.h
struct new_sem
{
  unsigned int value; // Max to 2^31-1
  unsigned int nwaiter; // 決定是否要執⾏行行 futex wake system call
}
Mutex
• Sleeping lock that implements mutual exclusion
• For Linux kernel, whoever locked a mutex must unlock it
• Difference between semaphore and mutex - mutex introduces thread
ownership
// partial code from thread-shared-types.h
struct struct __pthread_mutex_s
{
  int __lock;
  unsigned int __count;
  int __owner; // ppid
}
Mutex
The __owner structure member helps us to debug mutex.
$2 = {__data = {__lock = 1, __count = 0, __owner = 4060, __nusers = 1, __kind = 0, __spins = 0, __elision = 0,
 __list = {__prev = 0x0, __next = 0x0}},
  __size = "001000000000000000000000334017000000001", '000' <repeats 26 times>, __align = 1}
(gdb) info thread
  Id   Target Id         Frame
* 2    Thread 0x7ffff77f1700 (LWP 4061) "mutex.o" unlock_thread (args=0x7fffffffe330) at mutex.c:8
  1    Thread 0x7ffff7fee740 (LWP 4060) "mutex.o" 0x00007ffff7bc7f47 in pthread_join () from /lib64/
libpthread.so.0
Mutex
• glibc pthread_mutex_lock

The mutex of gnu C library does not define a behavior when it is unlocked by
other threads. The _owner member variable will be reset to zero without any
owner validation as mutex is unlocking.
Mutex Type Relock Unlock When Not Owner
DEFAULT deadlock undefined behavior
Mutex in Go sync.Mutex
• Hybrid Mutex - Combination of spin lock and mutex

• Two modes: starvation, normal that make it fair
Mutex in Go sync.Mutex
Normal Mode
Can spin?
1. Only if running on a multicore machine
2. Spin only few times( < 4 )
3. At least one other running P and local runq
is empty
Mutex in Go sync.Mutex
Normal Mode
Problems
A woken up waiter does not own the mutex
and competes with new arriving goroutines
over the ownership. New arriving goroutines
have a greater chance to get the lock.
Mutex in Go sync.Mutex
Starvation Mode

New arriving goroutine will be pushed to the waiting queue to guarantee FIFO
ordering.
Mutex in Go sync.Mutex
Switch from starvation to normal

• The duration of waiting for locks is less than 1ms
• No other goroutine is waiting for this lock
Switch from normal to starvation

• The duration of waiting for locks is longer than 1ms
Comparison pthread_mutex, sync.Mutex
1. No owner member in Go mutex struct

It means goroutines can unlock the mutex locked by another goroutine
struct {
  state int32
  sema uint32
}
2. pthread_mutex: Hybrid Mutex (Type: PTHREAD_MUTEX_ADAPTIVE_NP)
sync.Mutex: Hybrid Mutex & normal / starvation mode
Comparison pthread_mutex, sync.Mutex
3. pthread_mutex: Other threads can get the lock when the lock owner is
terminated.
static pthread_mutex_t mtx;
static void * original_owner_thread(void *ptr)
{
  pthread_mutex_lock(&mtx);
  pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
  pthread_t thr;
  pthread_mutexattr_t attr;
  int s;
  pthread_mutexattr_init(&attr);
  pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
  pthread_mutex_init(&mtx, &attr);
  pthread_create(&thr, NULL, original_owner_thread, NULL);
  sleep(2);
  s = pthread_mutex_lock(&mtx);
  if (s == EOWNERDEAD) {
    pthread_mutex_consistent(&mtx);
    pthread_mutex_unlock(&mtx);
    exit(EXIT_SUCCESS);
  }
}
Reader-Writer Locks
1. Reader preferred
2. Shared(read)–Exclusive(write) Locks
3. Use reader/writer or consumer/producer usage patterns.
Reader-Writer Locks
This reader-writer lock allow for maximum concurrency, but can lead to
write-starvation if contention is high. This is because writer threads will
not be able to acquire the lock as long as at least one reading thread
holds it.
Reader-Writer Locks in Go RWMutex
So, common implementation of reader–writer locks usually block
additional readers if a lock is already held in read mode and a thread is
blocked trying to acquire the lock in write mode.
This prevents a constant stream of readers from starving waiting writers.
Go sync.RWMutex use this implementation
as well.
Comparison pthread_rwlock, sync.RWMutex
pthread_rwlock

Different modes:
• PTHREAD_RWLOCK_PREFER_READER_NP (may lead write-starvation)
• PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
sync.RWMutex

1. In particular, it prohibits recursive read locking.
2. Prevent starving waiting writers.
Scalability Issues
type RWMutex struct {
  w           Mutex  // held if there are pending writers
  writerSem   uint32 // semaphore for writers to wait for completing readers
  readerSem   uint32 // semaphore for readers to wait for completing writers
  readerCount int32  // number of pending readers
  readerWait  int32  // number of departing readers
}
The default Go implementation of sync.RWMutex does not scale well to
multiple cores, as all readers contend on the same memory location
when they all try to atomically increment it.
Data Race Detection
ThreadSanitizer is a data race detector for C/C++
Data races are one of the most common and hardest to debug types of bugs in
concurrent systems. Go integrates ThreadSanitizer into CLI tools so that you can
trigger it by adding a race flag.
Tools: ThreadSanitizer
Go Command Option -race
go test -race
go run -race main.go
Data Race Detection
The Difference of Data Race and Race Condition
Race Condition
The system's substantive behavior is dependent on the sequence or timing of other
uncontrollable events.
Data race
occurs when two threads access the same variable concurrently and at least one of
the accesses is write.
ThreadSanitizer can not detect race condition without data race.
Data Race Detection
// Race condition without data race
var from int32 = 10
var to int32 = 20
func change(g *sync.WaitGroup, number int32, from, to *int32) error {
  defer g.Done()
  n := atomic.LoadInt32(from)
  for i := 0; i < 100; i++ {} // busy business logic
  if n < number {
    return fmt.Errorf("not enough")
  }
  atomic.AddInt32(to, number)
  atomic.AddInt32(from, -number)
  return nil
}
func Test(t *testing.T) {
  var group sync.WaitGroup
  group.Add(3)
  go change(&group, 7, &from, &to)
  go change(&group, 4, &from, &to)
  go change(&group, 1, &from, &to)
  group.Wait()
}
References
Books

1. Linux Kernel Development

2. Perfbook

Posts

1. Race Condition vs. Data Race

More Related Content

What's hot (20)

Coding standard and coding guideline
Coding standard and coding guidelineCoding standard and coding guideline
Coding standard and coding guideline
 
Shell programming
Shell programmingShell programming
Shell programming
 
Java threads
Java threadsJava threads
Java threads
 
Coding standard
Coding standardCoding standard
Coding standard
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
 
Process scheduling linux
Process scheduling linuxProcess scheduling linux
Process scheduling linux
 
Inter process communication using Linux System Calls
Inter process communication using Linux System CallsInter process communication using Linux System Calls
Inter process communication using Linux System Calls
 
OS multiprocessing -.pptx
OS multiprocessing -.pptxOS multiprocessing -.pptx
OS multiprocessing -.pptx
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
 
Go. Why it goes
Go. Why it goesGo. Why it goes
Go. Why it goes
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Coding conventions
Coding conventionsCoding conventions
Coding conventions
 
Ipc in linux
Ipc in linuxIpc in linux
Ipc in linux
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
Chpt7
Chpt7Chpt7
Chpt7
 
Process Scheduling
Process SchedulingProcess Scheduling
Process Scheduling
 
Golang Template
Golang TemplateGolang Template
Golang Template
 
Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 

Similar to An Introduction to Locks in Go

Dead Lock Analysis of spin_lock() in Linux Kernel (english)
Dead Lock Analysis of spin_lock() in Linux Kernel (english)Dead Lock Analysis of spin_lock() in Linux Kernel (english)
Dead Lock Analysis of spin_lock() in Linux Kernel (english)Sneeker Yeh
 
Linux Locking Mechanisms
Linux Locking MechanismsLinux Locking Mechanisms
Linux Locking MechanismsKernel TLV
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdfAdrian Huang
 
Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410huangachou
 
Linux kernel development chapter 10
Linux kernel development chapter 10Linux kernel development chapter 10
Linux kernel development chapter 10huangachou
 
Synchronization problem with threads
Synchronization problem with threadsSynchronization problem with threads
Synchronization problem with threadsSyed Zaid Irshad
 
Linux synchronization tools
Linux synchronization toolsLinux synchronization tools
Linux synchronization toolsmukul bhardwaj
 
Userspace adaptive spinlocks with rseq
Userspace adaptive spinlocks with rseqUserspace adaptive spinlocks with rseq
Userspace adaptive spinlocks with rseqIgalia
 
Multi threading
Multi threadingMulti threading
Multi threadinggndu
 
The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!Boris Hristov
 
The nightmare of locking, blocking and isolation levels
The nightmare of locking, blocking and isolation levelsThe nightmare of locking, blocking and isolation levels
The nightmare of locking, blocking and isolation levelsBoris Hristov
 
Concurrent/ parallel programming
Concurrent/ parallel programmingConcurrent/ parallel programming
Concurrent/ parallel programmingTausun Akhtary
 
Describe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdfDescribe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdfexcellentmobiles
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Javakoji lin
 
The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!Boris Hristov
 
Storm Real Time Computation
Storm Real Time ComputationStorm Real Time Computation
Storm Real Time ComputationSonal Raj
 

Similar to An Introduction to Locks in Go (20)

Dead Lock Analysis of spin_lock() in Linux Kernel (english)
Dead Lock Analysis of spin_lock() in Linux Kernel (english)Dead Lock Analysis of spin_lock() in Linux Kernel (english)
Dead Lock Analysis of spin_lock() in Linux Kernel (english)
 
Linux Locking Mechanisms
Linux Locking MechanismsLinux Locking Mechanisms
Linux Locking Mechanisms
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdf
 
Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410
 
Linux kernel development chapter 10
Linux kernel development chapter 10Linux kernel development chapter 10
Linux kernel development chapter 10
 
Synchronization problem with threads
Synchronization problem with threadsSynchronization problem with threads
Synchronization problem with threads
 
Locks
LocksLocks
Locks
 
System Programming - Threading
System Programming - ThreadingSystem Programming - Threading
System Programming - Threading
 
Linux synchronization tools
Linux synchronization toolsLinux synchronization tools
Linux synchronization tools
 
Kernel
KernelKernel
Kernel
 
Userspace adaptive spinlocks with rseq
Userspace adaptive spinlocks with rseqUserspace adaptive spinlocks with rseq
Userspace adaptive spinlocks with rseq
 
Java threading
Java threadingJava threading
Java threading
 
Multi threading
Multi threadingMulti threading
Multi threading
 
The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!
 
The nightmare of locking, blocking and isolation levels
The nightmare of locking, blocking and isolation levelsThe nightmare of locking, blocking and isolation levels
The nightmare of locking, blocking and isolation levels
 
Concurrent/ parallel programming
Concurrent/ parallel programmingConcurrent/ parallel programming
Concurrent/ parallel programming
 
Describe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdfDescribe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdf
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
 
The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!
 
Storm Real Time Computation
Storm Real Time ComputationStorm Real Time Computation
Storm Real Time Computation
 

Recently uploaded

Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 

Recently uploaded (20)

Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 

An Introduction to Locks in Go

  • 1. An Introduction to Locks in Go Cherie Hsieh Golang Taiwan, Gopher Taipei
  • 2. Source Code Versions Go version 1.12 glibc version 2.29
  • 3. Mutex in Go Before discuss this topic, we briefly review the basic concept of some fundamental locks.
  • 4. Spin Lock • Appropriate for multi-core processors In uniprocessor, a waiting thread spend all its time slice on spinning • The most common lock in linux kernel Spin lock is used in interrupt handlers which cannot be rescheduled • Hold a spin lock for a short time To prevent heavy CPU consumption
  • 5. Spin Lock Question: Why is glibc spin lock unfair ? int pthread_spin_lock (pthread_spinlock_t *lock) {   int val = 0;   if (__glibc_likely(atomic_compare_exchange_weak_acquire (lock, &val, 1)))     return 0;   do     {       do         {           atomic_spin_nop(); // Important!! CPU pause           val = atomic_load_relaxed(lock);         }       while (val != 0);     }   while (!atomic_compare_exchange_weak_acquire (lock, &val, 1));   return 0; } Difference from ticket spin lock, the lock is not fair as it doesn’t guarantee FIFO ordering amongst the threads competing for the lock. [source code]
  • 6. Semaphore • Sleeping locks • Obtained only in process context which is schedulable. • Allow for an arbitrary number of simultaneous lock holder. // Example: glibc semaphore struct // partial code from internaltypes.h struct new_sem {   unsigned int value; // Max to 2^31-1   unsigned int nwaiter; // 決定是否要執⾏行行 futex wake system call }
  • 7. Mutex • Sleeping lock that implements mutual exclusion • For Linux kernel, whoever locked a mutex must unlock it • Difference between semaphore and mutex - mutex introduces thread ownership // partial code from thread-shared-types.h struct struct __pthread_mutex_s {   int __lock;   unsigned int __count;   int __owner; // ppid }
  • 8. Mutex The __owner structure member helps us to debug mutex. $2 = {__data = {__lock = 1, __count = 0, __owner = 4060, __nusers = 1, __kind = 0, __spins = 0, __elision = 0,  __list = {__prev = 0x0, __next = 0x0}},   __size = "001000000000000000000000334017000000001", '000' <repeats 26 times>, __align = 1} (gdb) info thread   Id   Target Id         Frame * 2    Thread 0x7ffff77f1700 (LWP 4061) "mutex.o" unlock_thread (args=0x7fffffffe330) at mutex.c:8   1    Thread 0x7ffff7fee740 (LWP 4060) "mutex.o" 0x00007ffff7bc7f47 in pthread_join () from /lib64/ libpthread.so.0
  • 9. Mutex • glibc pthread_mutex_lock The mutex of gnu C library does not define a behavior when it is unlocked by other threads. The _owner member variable will be reset to zero without any owner validation as mutex is unlocking. Mutex Type Relock Unlock When Not Owner DEFAULT deadlock undefined behavior
  • 10. Mutex in Go sync.Mutex • Hybrid Mutex - Combination of spin lock and mutex • Two modes: starvation, normal that make it fair
  • 11. Mutex in Go sync.Mutex Normal Mode Can spin? 1. Only if running on a multicore machine 2. Spin only few times( < 4 ) 3. At least one other running P and local runq is empty
  • 12. Mutex in Go sync.Mutex Normal Mode Problems A woken up waiter does not own the mutex and competes with new arriving goroutines over the ownership. New arriving goroutines have a greater chance to get the lock.
  • 13. Mutex in Go sync.Mutex Starvation Mode New arriving goroutine will be pushed to the waiting queue to guarantee FIFO ordering.
  • 14. Mutex in Go sync.Mutex Switch from starvation to normal • The duration of waiting for locks is less than 1ms • No other goroutine is waiting for this lock Switch from normal to starvation • The duration of waiting for locks is longer than 1ms
  • 15. Comparison pthread_mutex, sync.Mutex 1. No owner member in Go mutex struct It means goroutines can unlock the mutex locked by another goroutine struct {   state int32   sema uint32 } 2. pthread_mutex: Hybrid Mutex (Type: PTHREAD_MUTEX_ADAPTIVE_NP) sync.Mutex: Hybrid Mutex & normal / starvation mode
  • 16. Comparison pthread_mutex, sync.Mutex 3. pthread_mutex: Other threads can get the lock when the lock owner is terminated. static pthread_mutex_t mtx; static void * original_owner_thread(void *ptr) {   pthread_mutex_lock(&mtx);   pthread_exit(NULL); } int main(int argc, char *argv[]) {   pthread_t thr;   pthread_mutexattr_t attr;   int s;   pthread_mutexattr_init(&attr);   pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);   pthread_mutex_init(&mtx, &attr);   pthread_create(&thr, NULL, original_owner_thread, NULL);   sleep(2);   s = pthread_mutex_lock(&mtx);   if (s == EOWNERDEAD) {     pthread_mutex_consistent(&mtx);     pthread_mutex_unlock(&mtx);     exit(EXIT_SUCCESS);   } }
  • 17. Reader-Writer Locks 1. Reader preferred 2. Shared(read)–Exclusive(write) Locks 3. Use reader/writer or consumer/producer usage patterns.
  • 18. Reader-Writer Locks This reader-writer lock allow for maximum concurrency, but can lead to write-starvation if contention is high. This is because writer threads will not be able to acquire the lock as long as at least one reading thread holds it.
  • 19. Reader-Writer Locks in Go RWMutex So, common implementation of reader–writer locks usually block additional readers if a lock is already held in read mode and a thread is blocked trying to acquire the lock in write mode. This prevents a constant stream of readers from starving waiting writers. Go sync.RWMutex use this implementation as well.
  • 20. Comparison pthread_rwlock, sync.RWMutex pthread_rwlock Different modes: • PTHREAD_RWLOCK_PREFER_READER_NP (may lead write-starvation) • PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP sync.RWMutex 1. In particular, it prohibits recursive read locking. 2. Prevent starving waiting writers.
  • 22. Data Race Detection ThreadSanitizer is a data race detector for C/C++ Data races are one of the most common and hardest to debug types of bugs in concurrent systems. Go integrates ThreadSanitizer into CLI tools so that you can trigger it by adding a race flag. Tools: ThreadSanitizer Go Command Option -race go test -race go run -race main.go
  • 23. Data Race Detection The Difference of Data Race and Race Condition Race Condition The system's substantive behavior is dependent on the sequence or timing of other uncontrollable events. Data race occurs when two threads access the same variable concurrently and at least one of the accesses is write. ThreadSanitizer can not detect race condition without data race.
  • 24. Data Race Detection // Race condition without data race var from int32 = 10 var to int32 = 20 func change(g *sync.WaitGroup, number int32, from, to *int32) error {   defer g.Done()   n := atomic.LoadInt32(from)   for i := 0; i < 100; i++ {} // busy business logic   if n < number {     return fmt.Errorf("not enough")   }   atomic.AddInt32(to, number)   atomic.AddInt32(from, -number)   return nil } func Test(t *testing.T) {   var group sync.WaitGroup   group.Add(3)   go change(&group, 7, &from, &to)   go change(&group, 4, &from, &to)   go change(&group, 1, &from, &to)   group.Wait() }
  • 25. References Books 1. Linux Kernel Development 2. Perfbook Posts 1. Race Condition vs. Data Race