operating systemssecure site  · 2020. 9. 29. · nipun batra . 2 locks cc7: mov 0x20135f,%eax ccd:...

95
Operating Systems Locks Nipun Batra

Upload: others

Post on 29-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Operating Systems Locks

Nipun Batra

Page 2: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

2

Locks

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

Thread 1 Thread 2

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

Page 3: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

2

Locks

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

Thread 1 Thread 2

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

• Thread 1 checks if lock is free

Page 4: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

2

Locks

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

Thread 1 Thread 2

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

• Thread 1 checks if lock is free• Lock is free, Thread 1 acquires the lock

Page 5: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

2

Locks

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

Thread 1 Thread 2

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

• Thread 1 checks if lock is free• Lock is free, Thread 1 acquires the lock• Thread 2 checks if lock is free

Page 6: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

2

Locks

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

Thread 1 Thread 2

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

• Thread 1 checks if lock is free• Lock is free, Thread 1 acquires the lock• Thread 2 checks if lock is free• Is not free; does not execute till lock free

Page 7: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

2

Locks

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

Thread 1 Thread 2

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

• Thread 1 checks if lock is free• Lock is free, Thread 1 acquires the lock• Thread 2 checks if lock is free• Is not free; does not execute till lock free

• Thread 1 executes

Page 8: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

2

Locks

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

Thread 1 Thread 2

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

• Thread 1 checks if lock is free• Lock is free, Thread 1 acquires the lock• Thread 2 checks if lock is free• Is not free; does not execute till lock free

• Thread 1 executes• Thread 1 Unlocks

Page 9: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

2

Locks

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

Thread 1 Thread 2

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

• Thread 1 checks if lock is free• Lock is free, Thread 1 acquires the lock• Thread 2 checks if lock is free• Is not free; does not execute till lock free

• Thread 1 executes• Thread 1 Unlocks• Thread 2 checks (keeps on doing so) for lock

being free

Page 10: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

2

Locks

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

Thread 1 Thread 2

cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f

• Thread 1 checks if lock is free• Lock is free, Thread 1 acquires the lock• Thread 2 checks if lock is free• Is not free; does not execute till lock free

• Thread 1 executes• Thread 1 Unlocks• Thread 2 checks (keeps on doing so) for lock

being free• Thread 2 executes and unlocks

Page 11: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

3

Page 12: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

3

• Mutual exclusion: Only a single thread can run the critical section at a time

Page 13: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

3

• Mutual exclusion: Only a single thread can run the critical section at a time

• Fairness: Each thread should get a fair chance of running the critical section. No starvation.

Page 14: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

3

• Mutual exclusion: Only a single thread can run the critical section at a time

• Fairness: Each thread should get a fair chance of running the critical section. No starvation.

• Performance: Low time overhead

Page 15: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

3

• Mutual exclusion: Only a single thread can run the critical section at a time

• Fairness: Each thread should get a fair chance of running the critical section. No starvation.

• Performance: Low time overhead• Performance overhead when:

Page 16: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

3

• Mutual exclusion: Only a single thread can run the critical section at a time

• Fairness: Each thread should get a fair chance of running the critical section. No starvation.

• Performance: Low time overhead• Performance overhead when:

• Single thread, no contention

Page 17: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

3

• Mutual exclusion: Only a single thread can run the critical section at a time

• Fairness: Each thread should get a fair chance of running the critical section. No starvation.

• Performance: Low time overhead• Performance overhead when:

• Single thread, no contention• Multiple threads, single CPU

Page 18: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

3

• Mutual exclusion: Only a single thread can run the critical section at a time

• Fairness: Each thread should get a fair chance of running the critical section. No starvation.

• Performance: Low time overhead• Performance overhead when:

• Single thread, no contention• Multiple threads, single CPU• Multiple threads, multiple CPU

Page 19: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Disable Interrupts

4

Void lock() { Disable Interrupts}

Void unlock() { Enable Interrupts}

Critical Section

Page 20: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Disable Interrupts

5

Page 21: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Disable Interrupts

5

Pros

Page 22: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Disable Interrupts

5

Pros1. Simple and works!

Page 23: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Disable Interrupts

5

Pros1. Simple and works!

Cons

Page 24: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Disable Interrupts

5

Pros1. Simple and works!

Cons1. Threads are given a lot of trust

Page 25: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Disable Interrupts

5

Pros1. Simple and works!

Cons1. Threads are given a lot of trust

1. Call lock() at starting of program and run infinitely

Page 26: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Disable Interrupts

5

Pros1. Simple and works!

Cons1. Threads are given a lot of trust

1. Call lock() at starting of program and run infinitely 2. Does not work on multiprocessors

Page 27: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Disable Interrupts

5

Pros1. Simple and works!

Cons1. Threads are given a lot of trust

1. Call lock() at starting of program and run infinitely 2. Does not work on multiprocessors

1. Each processor will have own interrupts?!

Page 28: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Disable Interrupts

5

Pros1. Simple and works!

Cons1. Threads are given a lot of trust

1. Call lock() at starting of program and run infinitely 2. Does not work on multiprocessors

1. Each processor will have own interrupts?!3. Loss of interrupts

Page 29: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Disable Interrupts

5

Pros1. Simple and works!

Cons1. Threads are given a lot of trust

1. Call lock() at starting of program and run infinitely 2. Does not work on multiprocessors

1. Each processor will have own interrupts?!3. Loss of interrupts4. Inefficient - Interrupt routines can be slow

Page 30: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

6

Page 31: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

6

• Use a single flag to indicate if a thread has possession of critical section

Page 32: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

6

• Use a single flag to indicate if a thread has possession of critical section

• Thread calls lock before entering critical section

Page 33: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

6

• Use a single flag to indicate if a thread has possession of critical section

• Thread calls lock before entering critical section• Is flag set? (some other thread has critical section

control)

Page 34: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

6

• Use a single flag to indicate if a thread has possession of critical section

• Thread calls lock before entering critical section• Is flag set? (some other thread has critical section

control)• Yes - Spin waiting

Page 35: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

6

• Use a single flag to indicate if a thread has possession of critical section

• Thread calls lock before entering critical section• Is flag set? (some other thread has critical section

control)• Yes - Spin waiting• No

Page 36: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

6

• Use a single flag to indicate if a thread has possession of critical section

• Thread calls lock before entering critical section• Is flag set? (some other thread has critical section

control)• Yes - Spin waiting• No

• set flag, execute critical section

Page 37: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

6

• Use a single flag to indicate if a thread has possession of critical section

• Thread calls lock before entering critical section• Is flag set? (some other thread has critical section

control)• Yes - Spin waiting• No

• set flag, execute critical section• After completion of critical section, unset flag

Page 38: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

7

Page 39: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

7

typedef struct __lock_t { int flag; } lock_t;

Page 40: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

7

typedef struct __lock_t { int flag; } lock_t;

void init(lock_t *mutex) { // 0 -> lock is available, 1 -> held mutex->flag = 0; }

Page 41: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

7

typedef struct __lock_t { int flag; } lock_t;

void init(lock_t *mutex) { // 0 -> lock is available, 1 -> held mutex->flag = 0; }

void lock(lock_t *mutex) { while (mutex->flag == 1); // spin-wait (do nothing) mutex->flag = 1; // now SET it! }

Page 42: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

7

typedef struct __lock_t { int flag; } lock_t;

void unlock(lock_t *mutex) { mutex->flag = 0; }

void init(lock_t *mutex) { // 0 -> lock is available, 1 -> held mutex->flag = 0; }

void lock(lock_t *mutex) { while (mutex->flag == 1); // spin-wait (do nothing) mutex->flag = 1; // now SET it! }

Page 43: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

8

Thread 1 Thread 2

Page 44: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

8

Thread 1 Thread 2

Call Lock()

Page 45: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

8

Thread 1 Thread 2

Call Lock()Lock held by some other thread

Page 46: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

8

Thread 1 Thread 2

Call Lock()Lock held by some other threadwhile(flag ==1) // Busy spinning

Page 47: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

8

Thread 1 Thread 2

Call Lock()Lock held by some other threadwhile(flag ==1) // Busy spinningOther thread unlocks —> flag = 0

Page 48: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

8

Thread 1 Thread 2

Call Lock()Lock held by some other threadwhile(flag ==1) // Busy spinningOther thread unlocks —> flag = 0

Context Switch

Page 49: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

8

Thread 1 Thread 2

Call Lock()Lock held by some other threadwhile(flag ==1) // Busy spinningOther thread unlocks —> flag = 0

Context SwitchCall Lock()

Page 50: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

8

Thread 1 Thread 2

Call Lock()Lock held by some other threadwhile(flag ==1) // Busy spinningOther thread unlocks —> flag = 0

Context SwitchCall Lock()while(flag ==1)

Page 51: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

8

Thread 1 Thread 2

Call Lock()Lock held by some other threadwhile(flag ==1) // Busy spinningOther thread unlocks —> flag = 0

Context SwitchCall Lock()while(flag ==1)flag = 1

Page 52: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

8

Thread 1 Thread 2

Call Lock()Lock held by some other threadwhile(flag ==1) // Busy spinningOther thread unlocks —> flag = 0

Context SwitchCall Lock()while(flag ==1)flag = 1Context Switch

Page 53: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Building a Lock - Load/Store or Flag

8

Thread 1 Thread 2

Call Lock()Lock held by some other threadwhile(flag ==1) // Busy spinningOther thread unlocks —> flag = 0

Context SwitchCall Lock()while(flag ==1)flag = 1Context Switch

flag = 1

Page 54: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

9

Page 55: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

9

• Mutual exclusion: X

Page 56: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

9

• Mutual exclusion: X• Fairness: X

Page 57: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

9

• Mutual exclusion: X• Fairness: X• Performance: Spin Waiting is bad!

Page 58: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

9

• Mutual exclusion: X• Fairness: X• Performance: Spin Waiting is bad!

Page 59: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

9

• Mutual exclusion: X• Fairness: X• Performance: Spin Waiting is bad!

Page 60: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Goals of a Lock

9

• Mutual exclusion: X• Fairness: X• Performance: Spin Waiting is bad!

Need Hardware Support!

Page 61: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

10

1 int TestAndSet(int *ptr, int new) { 2   int old = *ptr;  // fetch old value at ptr 3   *ptr = new;  // store ‘new’ into ptr 4   return old;  // return the old value 5 }

Page 62: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

10

1 int TestAndSet(int *ptr, int new) { 2   int old = *ptr;  // fetch old value at ptr 3   *ptr = new;  // store ‘new’ into ptr 4   return old;  // return the old value 5 }

• Return old value pointed by ptr

Page 63: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

10

1 int TestAndSet(int *ptr, int new) { 2   int old = *ptr;  // fetch old value at ptr 3   *ptr = new;  // store ‘new’ into ptr 4   return old;  // return the old value 5 }

• Return old value pointed by ptr• Simultaneously update to new

Page 64: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

10

1 int TestAndSet(int *ptr, int new) { 2   int old = *ptr;  // fetch old value at ptr 3   *ptr = new;  // store ‘new’ into ptr 4   return old;  // return the old value 5 }

• Return old value pointed by ptr• Simultaneously update to new • Performed Atomically and by Hardware!

Page 65: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

10

1 int TestAndSet(int *ptr, int new) { 2   int old = *ptr;  // fetch old value at ptr 3   *ptr = new;  // store ‘new’ into ptr 4   return old;  // return the old value 5 }

• Return old value pointed by ptr• Simultaneously update to new • Performed Atomically and by Hardware!

• The above is just a software depiction

Page 66: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

11

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Page 67: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

12

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Define lock structure

Page 68: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

13

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Init by setting flag to 0

Page 69: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

14

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Page 70: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

14

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Case 1: Lock not held by any thread

Page 71: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

14

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Case 1: Lock not held by any thread

Page 72: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

14

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Case 1: Lock not held by any thread

• old value of flag = 0

Page 73: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

14

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Case 1: Lock not held by any thread

• old value of flag = 0

• Set flag to 1 and return 0 from test and set —> Current thread acquires lock

Page 74: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

14

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Case 1: Lock not held by any thread

• old value of flag = 0

• Set flag to 1 and return 0 from test and set —> Current thread acquires lock

• No spin waiting for current thread

Page 75: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

15

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Page 76: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

15

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Case 2: Lock held by some other thread

Page 77: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

15

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Case 2: Lock held by some other thread

Page 78: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

15

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Case 2: Lock held by some other thread

• old value of flag = 1

Page 79: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

15

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Case 2: Lock held by some other thread

• old value of flag = 1

• Set flag to 1 and return 1 from test and set

Page 80: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

15

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Case 2: Lock held by some other thread

• old value of flag = 1

• Set flag to 1 and return 1 from test and set

• Spin waiting for current thread since it goes in while loop

Page 81: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Test & Set

16

1 typedef struct __lock_t { 2   int flag; 3 } lock_t; 4 5 void init(lock_t *lock) { 6   // 0 indicates that lock is available, 7   // 1 that it is held 8   lock->flag = 0; 9 } 10 void lock(lock_t *lock) { 11   while (TestAndSet(&lock->flag, 1) == 1) 12   ;  // spin-wait 13 } 14 15 void unlock(lock_t *lock) { 16   lock->flag = 0; 18 }

Once out of critical section, unset flag

Page 82: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Test & Set Evaluation

17

Page 83: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Test & Set Evaluation

17

• Mutual exclusion: Yes

Page 84: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Test & Set Evaluation

17

• Mutual exclusion: Yes• Fairness: X

Page 85: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Test & Set Evaluation

17

• Mutual exclusion: Yes• Fairness: X• Performance: Spin Waiting is bad!

Page 86: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Test & Set Evaluation

17

• Mutual exclusion: Yes• Fairness: X• Performance: Spin Waiting is bad!

• Single core: Each thread spins away its allotted time slot, eating away the time for the thread holding the critical section

Page 87: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Test & Set Evaluation

17

• Mutual exclusion: Yes• Fairness: X• Performance: Spin Waiting is bad!

• Single core: Each thread spins away its allotted time slot, eating away the time for the thread holding the critical section

• Multi core: If num threads ~ num cores

Page 88: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Test & Set Evaluation

17

• Mutual exclusion: Yes• Fairness: X• Performance: Spin Waiting is bad!

• Single core: Each thread spins away its allotted time slot, eating away the time for the thread holding the critical section

• Multi core: If num threads ~ num cores• Each thread waiting to acquire lock can spin on

its core, not eating up the time needed (quick) for the critical section to execute on other

Page 89: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Compare & Swap

18

1 int CompareAndSwap(int *ptr, int expected, int new) { 2   int actual = *ptr; 3   if (actual == expected) 4   *ptr = new; 5   return actual; 6 }

Page 90: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Compare & Swap

18

• Test whether value at address (ptr) is equal to expected

1 int CompareAndSwap(int *ptr, int expected, int new) { 2   int actual = *ptr; 3   if (actual == expected) 4   *ptr = new; 5   return actual; 6 }

Page 91: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Compare & Swap

18

• Test whether value at address (ptr) is equal to expected• Yes

1 int CompareAndSwap(int *ptr, int expected, int new) { 2   int actual = *ptr; 3   if (actual == expected) 4   *ptr = new; 5   return actual; 6 }

Page 92: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Compare & Swap

18

• Test whether value at address (ptr) is equal to expected• Yes

• Set new value at address

1 int CompareAndSwap(int *ptr, int expected, int new) { 2   int actual = *ptr; 3   if (actual == expected) 4   *ptr = new; 5   return actual; 6 }

Page 93: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Compare & Swap

18

• Test whether value at address (ptr) is equal to expected• Yes

• Set new value at address• Return old value at address

1 int CompareAndSwap(int *ptr, int expected, int new) { 2   int actual = *ptr; 3   if (actual == expected) 4   *ptr = new; 5   return actual; 6 }

Page 94: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Compare & Swap

18

• Test whether value at address (ptr) is equal to expected• Yes

• Set new value at address• Return old value at address

• No

1 int CompareAndSwap(int *ptr, int expected, int new) { 2   int actual = *ptr; 3   if (actual == expected) 4   *ptr = new; 5   return actual; 6 }

Page 95: Operating SystemsSecure Site  · 2020. 9. 29. · Nipun Batra . 2 Locks cc7: mov 0x20135f,%eax ccd: add $0x1,%eax cd0: mov %eax,0x20135f Thread 1 Thread 2 ... Call lock() at starting

Atomic Instructions - Compare & Swap

18

• Test whether value at address (ptr) is equal to expected• Yes

• Set new value at address• Return old value at address

• No• Return old value at address

1 int CompareAndSwap(int *ptr, int expected, int new) { 2   int actual = *ptr; 3   if (actual == expected) 4   *ptr = new; 5   return actual; 6 }