{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Mutex Lock implementation

Mutex Lock implementation - Mutex Lock implementation Two...

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

View Full Document Right Arrow Icon
Mutex Lock implementation Two approaches: Disable Interrupts Do not allow context switches to happen. Available only in Kernel mode Other interrupts may be lost. Only used in kernel programming Slow if used in a machine with more than 1 processor Spin Locks Spin locks use an instruction test_and_set assembly instruction that is guaranteed to be atomic. Most architectures provide this instruction. Spin locks can be implemented in user space
Background image of page 1

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

View Full Document Right Arrow Icon
Spin Locks There is an instruction test_and_set that is guaranteed to be atomic Pseudocode: int test_and_set(int *v){ int oldval = *v; *v = 1; return oldval; } This instruction is implemented by the CPU. You don’t need to implement it.
Background image of page 2
Spin Locks Mutex locks are implemented on top of spinlocks. Spinlocks make thread “spin” busy waiting until lock is released, instead of putting thread in waiting state.
Background image of page 3

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

View Full Document Right Arrow Icon
Spin Locks int lock = 0; void spinlock(int * lock) { while (test_and_set(&lock) != 0) { // Give up CPU thr_yield(); } } void spinunlock(int*lock){ *lock = 0; }
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}