notes12 - How mutex locks are implemented - Most modern...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
How mutex locks are implemented - Most modern CPU's have a "test_and_set" assembly instruction (or an equivalent) that is guaranteed to be executed atomically. This is the pseudocode in "c", that is done by the CPU atomically int test_and_set(int *v){ int old_value = *v; // you receive some address in memory and the old_value is *v=1; return old_value; } This code is executed atomically by the test_and_set assembly instruction. Where he we show the equivalent of this instruction in "c". How can we build a mutex_lock using that? We are going to use spinlocks. What is a spinlock? A spinlock is an implementation of a mutex_lock that uses test_and_set. This implementation is very basic so a thread waiting to enter a critical section will be "spinning," - that is busy checking until the lock is unlocked. How is this implemented? Lets assume we have a lock int lock = 0; void spinlock(int * lock){ while (test_and_set(lock)){ thr_yield(); // gives up or yields the executiong to the CPU to another thread } } What does this accomplish? void spinunluck(int *lock){ *lock = 0; } spinlock equilvalent to mutex_lock spinunlock equivalent to mutex_unlock
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How does it work? [Diagram 1] Only 1 thread at most can be in the critical section at any time. Why is thr_yield() important? It will was the whole quantum if there is just a semicolon in there. If we remove the thr_yield() instruction the spinlocks will continue working. However the thread will waste the remaining quantum checking for the lock to be 0. This is a waste of CPU time. So thr_yield() is preferred. Spinlocks can be used as mutex locks as long as the critical section is just a few assembly instructions long. The mutex_locks definted in the pthread/thr libraries use spinlocks as building blocks. However instead of spinning they put the caller thread into waiting state until the mutex is unlocked [Diagram 2] mutex_lock spinlocks test_and_set Mutex locks are neccesary when the critical section is long since spinlocks use more CPU. Calling thr_yield() inside spinlock mitigates this problem but still threads spinning inside spinlock will consume CPU.
Background image of page 2
Semaphores - Mutex locks do not solve all the problems of synchranization in threads. That is why
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/30/2012 for the course CS 354 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 9

notes12 - How mutex locks are implemented - Most modern...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online