SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Downloaden Sie, um offline zu lesen
Mutex & Cooperation in Dalvik
Understanding Monitor
Haifeng Li
2014-1-6
Outline
• Background
• How To: Implement in Dalvik
• Optimization: Thin Lock & Fat Lock

2
Why Need Mutex?
• A snippets about Xiao ming

3
Why Need Cooperation
• A snippets about writer & reader

4
The Graphical Depiction of Monitor
Owner
Entry
1

Enter

Wait
release

acquire

3

2
5

Acquire

4

Release & exit

A Waiting Thread
An Active Thread
∙ The picture is from inside the java machine, Figure 20-1.
5
How to Implement them?
• Mutex -- Synchronized
• Cooperation -- wait() & notify()

6
Key Structure
struct Monitor {
Thread* owner;
int
lockCount;
Object* obj;
Thread*

/* which thread currently owns the lock? */
/* owner's recursive lock depth */
/* what object are we part of [debug only] */

waitSet;

/* threads currently waiting on this monitor */

pthread_mutex_t lock;
Monitor* next;

…
};

7
Locking Algorithm (1)
• Lock Monitor without Contention
Thread A will lock a unlocked object
– Call pthread_mutex_lock to Lock monitor->lock
– Set monitor->owner = Thread A

8
Locking Algorithm (2)
• Locking with Contention
Thread B tries to acquire a lock held by thread
A.
• B ’s check that B owns the lock will fail
• B call pthread_mutex_lock() to acquire monitor->lock. If
fail, B sleep on monitor->lock.

9
Cooperation Algorithm (1)
• Thread A grasp the monitor and will wait.
–
–
–
–
–
–
–
–

Append self to monitor->waitSet
Store monitor->lockcount
Clear monitor->owner
Update thread status to THREAD_WAIT
Call pthread_mutex_lock to lock thread->waitmutex
Set thread->waitMonitor = monitor
Call pthread_mutex_unlock to unlock the monitor
Call pthread_cond_wait to be scheduled.

P.S.: pthread_cond_wait will call pthread_mutex_unlock

10
Cooperation Algorithm (2)
• Thread B grasp the monitor and notify a
thread.
– Thread B traverse monitor->waitSet, if there is a
thread, call pthread_cond_signal to wakeup.

11
Cooperation Algorithm (3)
• Thread A was notified by one Thread.
– Pthread_mutex_lock thread->waitMutex
– Clear thread->waitMonitor
– Pthread_mutex_unlock thread->waitMutex
– Reacquire the monitor
– Set monitor->owner
– Restore monitor->lockCount
– Update thread status to THREAD_RUNNING
12
Optimization: Thin Lock & Fat Lock
• Every object should maintenance a monitor. The
monitor occupy object->lock and itself.
• But someone found that median of 80% of all
lock operations are on unlocked objects, or
nesting is very shallow.
– Locking an unlocked object
– Locking an object already locked by current thread a
small number of times (shallow nested locking)
– Locking an object already locked by the current thread
many times (deeply nested locking)
13
•

Result is from paper, David F.Bacon etc., Thin locks: feather weight Synchronization for Java.
14
Thin Lock
• Thin lock reuse object->lock, and the layout is
as follows.
31

19
Count

3
Thread id

0
Hash state 0

– Count: Nested lock count
– Thread id: is a identifier. Not System thread id.
– LSB: 0 indicates thin lock.

15
Fat Lock
• Object->lock layout is as below.
31

3
Monitor Pointer

0
Hash state 1

Monitor

– LSB: 1 indicates fat lock.

16
Locking Algorithm (1)
• Locking without Contention
– Initially - lock field is 0, thread A wishes to lock.
– If succeeds, object won’t be locked by another
thread and we now own lock

17
Locking Algorithm (2)
• Locking with Contention(Lock is thin)
Thread B tries to acquire a lock held by thread
A.
– B ’s check that B owns the lock will fail
– B needs to force a transition from thin to fat
• B enters a spin-locking loop
• Once A unlocks, B will obtain
• B creates a fat lock, assigns object->lock to new
monitor
• B changes object->lock to 1
18
Locking Algorithm (3)
• Locking with Contention(Lock is fat)
Thread B tries to acquire a lock held by thread
A.
• B ’s check that B owns the lock will fail
• B call pthread_mutex_lock() to acquire monitor->lock. If
fail, B sleep on monitor->lock.

19
Cooperation Algorithm
• Thread A grasp the thin lock and will wait.
– Fatten the lock firstly
–…

20

Weitere ähnliche Inhalte

Was ist angesagt?

Delphi - Howto Threads
Delphi - Howto ThreadsDelphi - Howto Threads
Delphi - Howto Threads
Niall Munro
 
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Subhajit Sahu
 

Was ist angesagt? (20)

.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
Threading in C#
Threading in C#Threading in C#
Threading in C#
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
 
Core Java Programming Language (JSE) : Chapter XII - Threads
Core Java Programming Language (JSE) : Chapter XII -  ThreadsCore Java Programming Language (JSE) : Chapter XII -  Threads
Core Java Programming Language (JSE) : Chapter XII - Threads
 
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
 
Java Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsJava Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and Trends
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Delphi - Howto Threads
Delphi - Howto ThreadsDelphi - Howto Threads
Delphi - Howto Threads
 
[Java concurrency]01.thread management
[Java concurrency]01.thread management[Java concurrency]01.thread management
[Java concurrency]01.thread management
 
Effective java - concurrency
Effective java - concurrencyEffective java - concurrency
Effective java - concurrency
 
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
 
Synchronization problem with threads
Synchronization problem with threadsSynchronization problem with threads
Synchronization problem with threads
 
Inter thread communication & runnable interface
Inter thread communication & runnable interfaceInter thread communication & runnable interface
Inter thread communication & runnable interface
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Introduction+To+Java+Concurrency
Introduction+To+Java+ConcurrencyIntroduction+To+Java+Concurrency
Introduction+To+Java+Concurrency
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Wait for your fortune without Blocking!
Wait for your fortune without Blocking!Wait for your fortune without Blocking!
Wait for your fortune without Blocking!
 
.Net Threading
.Net Threading.Net Threading
.Net Threading
 

Andere mochten auch (6)

Understanding Semi-Space Garbage Collector in ART
Understanding Semi-Space Garbage Collector in ARTUnderstanding Semi-Space Garbage Collector in ART
Understanding Semi-Space Garbage Collector in ART
 
Understanding DLmalloc
Understanding DLmallocUnderstanding DLmalloc
Understanding DLmalloc
 
Understanding binder in android
Understanding binder in androidUnderstanding binder in android
Understanding binder in android
 
Process Scheduler and Balancer in Linux Kernel
Process Scheduler and Balancer in Linux KernelProcess Scheduler and Balancer in Linux Kernel
Process Scheduler and Balancer in Linux Kernel
 
Understanding SLAB in Linux Kernel
Understanding SLAB in Linux KernelUnderstanding SLAB in Linux Kernel
Understanding SLAB in Linux Kernel
 
AARCH64 VMSA Under Linux Kernel
AARCH64 VMSA Under Linux KernelAARCH64 VMSA Under Linux Kernel
AARCH64 VMSA Under Linux Kernel
 

Ähnlich wie Understanding Monitor in Dalvik

Inter Thread Communicationn.pptx
Inter Thread Communicationn.pptxInter Thread Communicationn.pptx
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
Kuba Břečka
 
You didnt see it’s coming? "Dawn of hardened Windows Kernel"
You didnt see it’s coming? "Dawn of hardened Windows Kernel" You didnt see it’s coming? "Dawn of hardened Windows Kernel"
You didnt see it’s coming? "Dawn of hardened Windows Kernel"
Peter Hlavaty
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
HKShab
 
AOS Lab 4: If you liked it, then you should have put a “lock” on it
AOS Lab 4: If you liked it, then you should have put a “lock” on itAOS Lab 4: If you liked it, then you should have put a “lock” on it
AOS Lab 4: If you liked it, then you should have put a “lock” on it
Zubair Nabi
 
Thread dump troubleshooting
Thread dump troubleshootingThread dump troubleshooting
Thread dump troubleshooting
Jerry Chan
 
Slot03 concurrency2
Slot03 concurrency2Slot03 concurrency2
Slot03 concurrency2
Viên Mai
 

Ähnlich wie Understanding Monitor in Dalvik (20)

Inter Thread Communicationn.pptx
Inter Thread Communicationn.pptxInter Thread Communicationn.pptx
Inter Thread Communicationn.pptx
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
 
Java threading
Java threadingJava threading
Java threading
 
You didnt see it’s coming? "Dawn of hardened Windows Kernel"
You didnt see it’s coming? "Dawn of hardened Windows Kernel" You didnt see it’s coming? "Dawn of hardened Windows Kernel"
You didnt see it’s coming? "Dawn of hardened Windows Kernel"
 
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)
 
Lect04
Lect04Lect04
Lect04
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practice
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
 
Where destructors meet threads
Where destructors meet threadsWhere destructors meet threads
Where destructors meet threads
 
AOS Lab 4: If you liked it, then you should have put a “lock” on it
AOS Lab 4: If you liked it, then you should have put a “lock” on itAOS Lab 4: If you liked it, then you should have put a “lock” on it
AOS Lab 4: If you liked it, then you should have put a “lock” on it
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
MULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxMULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptx
 
Thread dump troubleshooting
Thread dump troubleshootingThread dump troubleshooting
Thread dump troubleshooting
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Java Thread
Java ThreadJava Thread
Java Thread
 
concurrency_c#_public
concurrency_c#_publicconcurrency_c#_public
concurrency_c#_public
 
Lock Interface in Java
Lock Interface in JavaLock Interface in Java
Lock Interface in Java
 
Slot03 concurrency2
Slot03 concurrency2Slot03 concurrency2
Slot03 concurrency2
 
Cansecwest_16_Dont_Trust_Your_Eye_Apple_Graphics_Is_Compromised
Cansecwest_16_Dont_Trust_Your_Eye_Apple_Graphics_Is_CompromisedCansecwest_16_Dont_Trust_Your_Eye_Apple_Graphics_Is_Compromised
Cansecwest_16_Dont_Trust_Your_Eye_Apple_Graphics_Is_Compromised
 
Multi core programming 2
Multi core programming 2Multi core programming 2
Multi core programming 2
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 

Understanding Monitor in Dalvik

  • 1. Mutex & Cooperation in Dalvik Understanding Monitor Haifeng Li 2014-1-6
  • 2. Outline • Background • How To: Implement in Dalvik • Optimization: Thin Lock & Fat Lock 2
  • 3. Why Need Mutex? • A snippets about Xiao ming 3
  • 4. Why Need Cooperation • A snippets about writer & reader 4
  • 5. The Graphical Depiction of Monitor Owner Entry 1 Enter Wait release acquire 3 2 5 Acquire 4 Release & exit A Waiting Thread An Active Thread ∙ The picture is from inside the java machine, Figure 20-1. 5
  • 6. How to Implement them? • Mutex -- Synchronized • Cooperation -- wait() & notify() 6
  • 7. Key Structure struct Monitor { Thread* owner; int lockCount; Object* obj; Thread* /* which thread currently owns the lock? */ /* owner's recursive lock depth */ /* what object are we part of [debug only] */ waitSet; /* threads currently waiting on this monitor */ pthread_mutex_t lock; Monitor* next; … }; 7
  • 8. Locking Algorithm (1) • Lock Monitor without Contention Thread A will lock a unlocked object – Call pthread_mutex_lock to Lock monitor->lock – Set monitor->owner = Thread A 8
  • 9. Locking Algorithm (2) • Locking with Contention Thread B tries to acquire a lock held by thread A. • B ’s check that B owns the lock will fail • B call pthread_mutex_lock() to acquire monitor->lock. If fail, B sleep on monitor->lock. 9
  • 10. Cooperation Algorithm (1) • Thread A grasp the monitor and will wait. – – – – – – – – Append self to monitor->waitSet Store monitor->lockcount Clear monitor->owner Update thread status to THREAD_WAIT Call pthread_mutex_lock to lock thread->waitmutex Set thread->waitMonitor = monitor Call pthread_mutex_unlock to unlock the monitor Call pthread_cond_wait to be scheduled. P.S.: pthread_cond_wait will call pthread_mutex_unlock 10
  • 11. Cooperation Algorithm (2) • Thread B grasp the monitor and notify a thread. – Thread B traverse monitor->waitSet, if there is a thread, call pthread_cond_signal to wakeup. 11
  • 12. Cooperation Algorithm (3) • Thread A was notified by one Thread. – Pthread_mutex_lock thread->waitMutex – Clear thread->waitMonitor – Pthread_mutex_unlock thread->waitMutex – Reacquire the monitor – Set monitor->owner – Restore monitor->lockCount – Update thread status to THREAD_RUNNING 12
  • 13. Optimization: Thin Lock & Fat Lock • Every object should maintenance a monitor. The monitor occupy object->lock and itself. • But someone found that median of 80% of all lock operations are on unlocked objects, or nesting is very shallow. – Locking an unlocked object – Locking an object already locked by current thread a small number of times (shallow nested locking) – Locking an object already locked by the current thread many times (deeply nested locking) 13
  • 14. • Result is from paper, David F.Bacon etc., Thin locks: feather weight Synchronization for Java. 14
  • 15. Thin Lock • Thin lock reuse object->lock, and the layout is as follows. 31 19 Count 3 Thread id 0 Hash state 0 – Count: Nested lock count – Thread id: is a identifier. Not System thread id. – LSB: 0 indicates thin lock. 15
  • 16. Fat Lock • Object->lock layout is as below. 31 3 Monitor Pointer 0 Hash state 1 Monitor – LSB: 1 indicates fat lock. 16
  • 17. Locking Algorithm (1) • Locking without Contention – Initially - lock field is 0, thread A wishes to lock. – If succeeds, object won’t be locked by another thread and we now own lock 17
  • 18. Locking Algorithm (2) • Locking with Contention(Lock is thin) Thread B tries to acquire a lock held by thread A. – B ’s check that B owns the lock will fail – B needs to force a transition from thin to fat • B enters a spin-locking loop • Once A unlocks, B will obtain • B creates a fat lock, assigns object->lock to new monitor • B changes object->lock to 1 18
  • 19. Locking Algorithm (3) • Locking with Contention(Lock is fat) Thread B tries to acquire a lock held by thread A. • B ’s check that B owns the lock will fail • B call pthread_mutex_lock() to acquire monitor->lock. If fail, B sleep on monitor->lock. 19
  • 20. Cooperation Algorithm • Thread A grasp the thin lock and will wait. – Fatten the lock firstly –… 20