9
CS 140 Lecture Notes: Lock Implementation Slide 1 Uniprocessor Locks, v1 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; } struct lock { int locked; };

CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)

Embed Size (px)

Citation preview

Page 1: CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)

CS 140 Lecture Notes: Lock Implementation Slide 1

Uniprocessor Locks, v1void 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;}

struct lock { int locked;};

Page 2: CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)

CS 140 Lecture Notes: Lock Implementation Slide 2

Uniprocessor Locks, v2void 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) { Disable interrupts; if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock(queue_remove(&l->q)); } Enable interrupts;}

struct lock { int locked; struct queue q;};

Page 3: CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)

CS 140 Lecture Notes: Lock Implementation Slide 3

Multiprocessor Locks, v1struct lock { int locked;};

void lock_acquire( struct lock *l) { while (aswap(&l->locked, 1)) { /* Do nothing */ }}

void lock_release( struct lock *l) { l->locked = 0;}

Page 4: CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)

CS 140 Lecture Notes: Lock Implementation Slide 4

Multiprocessor Locks, v2struct lock { int locked; struct queue q;};

void lock_acquire( struct lock *l) { if (aswap(&l->locked, 1) { queue_add(&l->q, thread_current()); thread_block(); }}

void lock_release( struct lock *l) { if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock( queue_remove(&l->q)); }}

Page 5: CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)

CS 140 Lecture Notes: Lock Implementation Slide 5

Multiprocessor Locks, v3struct lock { int locked; struct queue q; int sync;};

void lock_acquire( struct lock *l) { while (aswap(&l->sync, 1) { /* Do nothing */ } if (!l->locked) { l->locked = 1; l->sync = 0; } else { queue_add(&l->q, thread_current()); l->sync = 0; thread_block(); }}

void lock_release( struct lock *l) { while (aswap(&l->sync, 1) { /* Do nothing */ } if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock( queue_remove(&l->q)); } l->sync = 0;}

Page 6: CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)

CS 140 Lecture Notes: Lock Implementation Slide 6

Multiprocessor Locks, v4struct lock { int locked; struct queue q; int sync;};

void lock_acquire( struct lock *l) { Disable interrupts; while (aswap(&l->sync, 1) { /* Do nothing */ } if (!l->locked) { l->locked = 1; l->sync = 0; } else { queue_add(&l->q, thread_current()); l->sync = 0; thread_block(); } Enable interrupts;}

void lock_release( struct lock *l) { Disable interrupts; while (aswap(&l->sync, 1) { /* Do nothing */ } if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock( queue_remove(&l->q)); } l->sync = 0; Enable interrupts;}

Page 7: CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)

CS 140 Lecture Notes: Lock Implementation Slide 7

Page 8: CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)

CS 140 Lecture Notes: Lock Implementation Slide 8

Multiprocessor Locks, v3struct lock { int locked; struct queue q;};

void lock_acquire( struct lock *l) { Disable interrupts; if (aswap(&l->locked, 1) { queue_add(&l->q, thread_current()); thread_block(); } Enable interrupts;}

void lock_release( struct lock *l) { Disable interrupts; if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock( queue_remove(&l->q)); } Enable interrupts;}

Page 9: CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)

CS 140 Lecture Notes: Lock Implementation Slide 9

Multiprocessor Locks, v4struct lock { int locked; struct queue q; int sync;};

void lock_acquire( struct lock *l) { Disable interrupts; while (aswap(&l->sync, 1) { /* Do nothing */ } if (!l->locked) { l->locked = 1; l->sync = 0; } else { queue_add(&l->q, thread_current()); l->sync = 0; thread_block(); } Enable interrupts;}

void lock_release( struct lock *l) { Disable interrupts; while (aswap(&l->sync, 1) { /* Do nothing */ } if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock( queue_remove(&l->q)); } l->sync = 0; Enable interrupts;}