Solution: a lock (a/k/a mutex) public: virtual void unlock() =0;

Similar documents
Lecture 7: Mutual Exclusion 2/16/12. slides adapted from The Art of Multiprocessor Programming, Herlihy and Shavit

Mutual Exclusion. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Programming Paradigms for Concurrency Lecture 2 - Mutual Exclusion

Peterson s Algorithm

What We'll Cover Today

Mutual Exclusion. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrent Skip Lists. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Mutual Exclusion: Classical Algorithms for Locks

Programming Paradigms for Concurrency Lecture 3 Concurrent Objects

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Sequen&al Consistency and Linearizability

Mutual Exclusion. 1 Formal problem definitions. Time notion CSE /17/2015. Outline of this lecture:

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Shared-Memory Computability

Spin Locks and Contention

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

DD2451 Parallel and Distributed Computing --- FDD3008 Distributed Algorithms

Linked Lists: Locking, Lock- Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Coarse-grained and fine-grained locking Niklas Fors

Concurrent Computing

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit. Art of Multiprocessor Programming

Linked Lists: Locking, Lock-Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Universality of Consensus. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Agenda. Lecture. Next discussion papers. Bottom-up motivation Shared memory primitives Shared memory synchronization Barriers and locks

Introduction to Concurrency and Multicore Programming. Slides adapted from Art of Multicore Programming by Herlihy and Shavit

Synchronization API of Pthread Mutex: lock, unlock, try_lock CondVar: wait, signal, signal_broadcast. Synchronization

Programming Paradigms for Concurrency Introduction

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Process Synchronization

Part III Synchronization Software and Hardware Solutions

Unit 6: Indeterminate Computation

Part II Process Management Chapter 6: Process Synchronization

Advanced Multiprocessor Programming

Concurrent Queues, Monitors, and the ABA problem. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

CS370 Operating Systems

Spin Locks and Contention. Companion slides for Chapter 7 The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

CS 361 Concurrent programming Drexel University Fall 2004 Lecture 8. Proof by contradiction. Proof of correctness. Proof of mutual exclusion property

Concurrent Objects. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Models of concurrency & synchronization algorithms

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Distributed Computing through Combinatorial Topology MITRO207, P4, 2017

Process Synchronization (Part I)

Chapter 6: Process Synchronization

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

SPIN, PETERSON AND BAKERY LOCKS

Lecture Topics. Announcements. Today: Concurrency: Mutual Exclusion (Stallings, chapter , 5.7)

Modern High-Performance Locking

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

10/27/11. Is The Queue Correct? Concurrent Computaton. Objects. A Concurrent FIFO Queue head. A Concurrent FIFO Queue head

Last Class: Synchronization

Thread Synchronization: Foundations. Properties. Safety properties. Edsger s perspective. Nothing bad happens

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

CS 450 Exam 2 Mon. 4/11/2016

CS370 Operating Systems

Introduction to OS Synchronization MOS 2.3

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Locking Granularity. CS 475, Spring 2019 Concurrent & Distributed Systems. With material from Herlihy & Shavit, Art of Multiprocessor Programming

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Process Synchronization

Today: Synchronization. Recap: Synchronization

Announcements. Office hours. Reading. W office hour will be not starting next week. Chapter 7 (this whole week) CMSC 412 S02 (lect 7)

DD2451 Parallel and Distributed Computing --- FDD3008 Distributed Algorithms

Chapter 7: Process Synchronization!

10/28/11. DD2451 Overview. DD2451 Overview. FDD 3008 Overview. Course Material

Pre- and post- CS protocols. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 7. Other requirements for a mutual exclusion algorithm

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker!

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Concurrency: Mutual Exclusion and Synchronization

Chapter 6: Process Synchronization

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Chapter 6: Process Synchronization. Module 6: Process Synchronization

CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages

Introduction to Multiprocessor Synchronization

Operating Systems. Synchronization

Synchronization Principles

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

COMP6771 Advanced C++ Programming

Lecture 5 Threads and Pthreads II

PESIT Bangalore South Campus

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Parallel Programming

Hashing and Natural Parallism. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Process Synchronization

Locks. Dongkun Shin, SKKU

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Chapter 7: Process Synchronization. Background

Operating Systems. Synchronisation Part I

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Lecture 5: Synchronization w/locks

Atomicity and Virtualization. Atomicity. Critical section. Edsger s perspective. Virtualizing a resource requires managing concurrent accesses

COMP6771 Advanced C++ Programming

Chapter 7: Process Synchronization. Background. Illustration

Concurrency: Mutual Exclusion and

Transcription:

1 Solution: a lock (a/k/a mutex) class BasicLock { public: virtual void lock() =0; virtual void unlock() =0; ;

2 Using a lock class Counter { public: int get_and_inc() { lock_.lock(); int old = count_; count_ = old + 1; lock_.unlock(); return old; private: int count_ = 0; Lock lock_; ;

2 Using a lockraai-style class Counter { public: int get_and_inc() { lock_.lock(); int old = count_; count_ = old + 1; lock_.unlock(); return old; private: int count_ = 0; Lock lock_; ; class Counter { public: int get_and_inc() { auto guard = lock_.acquire(); int old = count_; count_ = old + 1; return old; // Guard() unlocks lock_ here private: int count_ = 0; Lock lock_; ;

3 Base class for RAII-style lock class GuardedLockBase : public BasicLock { public: Guard acquire() { return Guard{ this;. ; class Guard { BasicLock& lock_; public: Guard(BasicLock& lock) : lock_{lock { lock_.lock(); virtual Guard() { lock_.unlock(); ;

4 How to implement the lock? Two-thread solutions first, then n-thread solutions

5 Base class for RAII-style lock class GuardedLockBase : public BasicLock {. ;. // i() is this thread: thread::id i() const { return this_thread::get_id(); // j() is the other thread: thread::id j() const { return i().other_thread();

6 An attempt class LockOne : public GuardedLockBase { bool flag_[2] = {; public: virtual void lock() override { flag_[i()] = true; while (flag_[j()]) { virtual void unlock() override { ;

7 Theorem LockOne satisfies mutual exclusion.

7 Theorem LockOne satisfies mutual exclusion. Proof by contradiction: Assume CS A overlaps CS B

7 Theorem LockOne satisfies mutual exclusion. Proof by contradiction: Assume CS A overlaps CS B Consider each thread s last read and write in lock() before entering its CS. For A to enter, it first writes true to its flag, and then needs to read false from the other s: write A (flag[a] = true) read A (flag[b] == false) CS A

7 Theorem LockOne satisfies mutual exclusion. Proof by contradiction: Assume CS A overlaps CS B Consider each thread s last read and write in lock() before entering its CS. For A to enter, it first writes true to its flag, and then needs to read false from the other s: write A (flag[a] = true) read A (flag[b] == false) CS A And by symmetry: write B (flag[b] = true) read B (flag[a] == false) CS B

7 Theorem LockOne satisfies mutual exclusion. Proof by contradiction: Assume CS A overlaps CS B Consider each thread s last read and write in lock() before entering its CS. For A to enter, it first writes true to its flag, and then needs to read false from the other s: write A (flag[a] = true) read A (flag[b] == false) CS A And by symmetry: write B (flag[b] = true) read B (flag[a] == false) CS B Note, also, that if A sees B s flag as false, that must happen before B writes its flag, and by symmetry for B seeing A s flag: read A (flag[b] == false) write B (flag[b] = true) read B (flag[a] == false) write A (flag[a] = true)

7 Theorem LockOne satisfies mutual exclusion. Proof by contradiction: Assume CS A overlaps CS B Consider each thread s last read and write in lock() before entering its CS. For A to enter, it first writes true to its flag, and then needs to read false from the other s: write A (flag[a] = true) read A (flag[b] == false) CS A And by symmetry: write B (flag[b] = true) read B (flag[a] == false) CS B Note, also, that if A sees B s flag as false, that must happen before B writes its flag, and by symmetry for B seeing A s flag: read A (flag[b] == false) write B (flag[b] = true) read B (flag[a] == false) write A (flag[a] = true) These events form a cycle, which is a contradiction.

8 Two other properties Deadlock-free: One ill-behaved thread does not prevent other threads from locking other locks System as a whole makes progress

8 Two other properties Deadlock-free: One ill-behaved thread does not prevent other threads from locking other locks System as a whole makes progress Starvation-free Every locking thread eventually returns Every thread makes progress

8 Two other properties Deadlock-free: One ill-behaved thread does not prevent other threads from locking other locks System as a whole makes progress Does LockOne enjoy deadlock freedom? Starvation-free Every locking thread eventually returns Every thread makes progress Does LockOne enjoy starvation freedom?

9 Deadlock case for LockOne flag_[0] = true; while (flag_[1]) { flag_[1] = true; while (flag_[0]) { (But sequentially it s fine.)

10 Another attempt class LockTwo : public GuardedLockBase { int waiting_; public: virtual void lock() override { waiting_ = i(); while (waiting_ == i()) { virtual void unlock() override {

11 LockTwo claims Satisfies mutual exclusion Not deadlock-free Sequential execution deadlocks Concurrent execution does not (Why?)

12 Peterson s algorithm class PetersonLock : public GuardedLockBase { bool flag_[2]; int waiting_; public: virtual void lock() override { flag_[i()] = true; waiting_ = i(); while (flag_[j()] && waiting_ == i()) { virtual void unlock() override { flag_[i()] = false; ;

13 Peterson s Lock properties Mutual exclusion By contradiction Deadlock freedom Only one thread at a time can be waiting Starvation freedom If A finishes and tries to re-enter while B is waiting, B gets in first

14 Filter algorithm for n threads template <int N> class FilterLock : public GuardedLockBase { int level_[n] = {0; int waiting_[n];. ; bool exists_competition(int level) { for (auto k : thread::all_ids()) if (k!= i() && level_[k] >= level) return true; return false;

15 template <int N> class FilterLock : public GuardedLockBase {. public: virtual void lock() override for (int level = 1; level < N; ++level) { level_[i()] = level; waiting_[level] = i(); while (exists_competition(level) && waiting_[level] == i()) { virtual void unlock() override level_[i()] = 0; ;

16 Filter lock properties Mutual exclusion By induction, one thread gets stuck in each level Deadlock freedom Like Peterson only one thread can wait per level Starvation freedom Like Peterson every thread advances if any does

17 cba This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported license. These slides are derived from the companion slides for The Art of Multiprocessor Programming, by Maurice Herlihy and Nir Shavit. Its original license reads: This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. You are free: to Share to copy, distribute and transmit the work to Remix to adapt the work Under the following conditions: Attribution. You must attribute the work to The Art of Multiprocessor Programming (but not in any way that suggests that the authors of that work or this endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work onnly under the same, similar or a compatible licese. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to http://creativecommons.org/licenses/by-sa/3.0/. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author s moral rights.