v5stanford.edu-Implementing_Locks

v5stanford.edu-Implementing_Locks - Implementing Locks...

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

View Full Document Right Arrow Icon
http://w w w .stanford.edu/~ouster/cgi-bin/cs140-w inter12/lecture.php?topic=lockImpl January 11, 2012 Implementing Locks Implementing Locks Lecture Notes for CS 140 Winter 2012 John Ousterhout Readings for this topic from Operating System Concepts : Section 6.5.2. Uniprocessor solution: can just disable interrupts. struct lock { int locked; }; void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked) { l->locked = 1; Enable interrupts; return; } Enable interrupts; } } void lock_release(struct lock *l) { Disable interrupts; l->locked = 0; Enable interrupts; } A better uniprocessor implementation: put thread to sleep if lock not available: struct lock { int locked; struct queue q; }; void lock_acquire(struct lock *l) { Disable interrupts; if (!l->locked) { l->locked = 1; } else { queue_add(&l->q, thread_current()); thread_block(); } Enable interrupts; } void lock_release(struct lock *l) {
Background image of page 1

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

View Full DocumentRight Arrow Icon
Disable interrupts; l->locked = 0; } else { thread_unblock(queue_remove(&l->q));
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/02/2012 for the course CS 140 taught by Professor Rosenblum during the Winter '08 term at Stanford.

Page1 / 4

v5stanford.edu-Implementing_Locks - Implementing Locks...

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

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