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

Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

  • Upload
    others

  • View
    27

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Introduction to Concurrency and Multicore Programming

Slides adapted fromArt of Multicore Programmingby Herlihy and Shavit

Page 2: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Overview

• Introduction

• Mutual Exclusion

• Linearizability

• Concurrent Data Structure – Linked-List Set

– Lock-free Stack

• Summary

Page 3: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

What is Concurrency?

A property of systems in which several processes or threads are executing at the same time.

Page 4: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Moore’s Law

Clock speed flattening

sharply

Transistor count still

rising

Page 5: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

The Uniprocessor is Vanishing!

memory

cpu

Page 6: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

The Shared Memory Multiprocessor (SMP)

cache

Bus Bus

shared memory

cache cache

Page 7: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Your New Desktop: The Multicore Processor

(CMP)

cache

Bus Bus

shared memory

cache cache All on the same chip

Sun T2000 Niagara

Page 8: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Why do we care?

• Time no longer cures software bloat – The “free ride” is over

• When you double your program’s path length – You can’t just wait 6 months

– Your software must somehow exploit twice as much concurrency

Page 9: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Traditional Scaling Process

User code

Traditional Uniprocessor

Speedup 1.8x

7x

3.6x

Time: Moore’s law

Page 10: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Multicore Scaling Process

User code

Multicore

Speedup 1.8x

7x

3.6x

Unfortunately, not so simple…

Page 11: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Real-World Scaling Process

1.8x 2x 2.9x

User code

Multicore

Speedup

Parallelization and Synchronization require great care…

Page 12: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential Computation

memory

object object

thread

Page 13: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Concurrent Computation

memory

object object

Page 14: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Asynchrony

• Sudden unpredictable delays – Cache misses (short)

– Page faults (long)

– Scheduling quantum used up (really long)

Page 15: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Model Summary

• Multiple threads

• Single shared memory

• Objects live in memory

• Unpredictable asynchronous delays

Page 16: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Multithread Programming

• Java, C#, Pthreads

• Windows Thread API

• OpenMP

• Intel Parallel Studio Tool Kits

Page 17: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Java Thread

Page 18: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Concurrency Idea

• Challenge – Print primes from 1 to 1010

• Given – Ten-processor multiprocessor

– One thread per processor

• Goal – Get ten-fold speedup (or close)

Page 19: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Load Balancing

• Split the work evenly

• Each thread tests range of 109

… 109 1010 2·109 1

P0 P1 P9

Page 20: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Procedure for Thread i

void primePrint { int i = ThreadID.get(); // IDs in {0..9} for (j = i*109+1, j<(i+1)*109; j++) { if (isPrime(j)) print(j); } }

Page 21: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Issues

• Higher ranges have fewer primes

• Yet larger numbers harder to test

• Thread workloads – Uneven

– Hard to predict

Page 22: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Issues

• Higher ranges have fewer primes

• Yet larger numbers harder to test

• Thread workloads – Uneven

– Hard to predict

• Need dynamic load balancing

Page 23: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Shared Counter

17

18

19

each thread takes a number

Page 24: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Procedure for Thread i

int counter = new Counter(1); void primePrint { long j = 0; while (j < 1010) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); } }

Page 25: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Procedure for Thread i

Counter counter = new Counter(1); void primePrint { long j = 0; while (j < 1010) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); } }

Shared counter object

Page 26: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Where Things Reside

cache

Bus Bus

cache cache

1

shared counter

shared memory

void primePrint { int i = ThreadID.get(); // IDs in {0..9} for (j = i*109+1, j<(i+1)*109; j++) { if (isPrime(j)) print(j); } }

code

Local variables

Page 27: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Counter Implementation

public class Counter { private long value; public long getAndIncrement() { return value++; } }

Page 28: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Counter Implementation

public class Counter { private long value; public long getAndIncrement() { return value++; } }

Page 29: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

What It Means

public class Counter { private long value; public long getAndIncrement() { return value++; } }

Page 30: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

What It Means

public class Counter { private long value; public long getAndIncrement() { return value++; } }

temp = value;

value = value + 1;

return temp;

Page 31: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

Not so good…

Value… 1

read 1

read 1

write 2

read 2

write 3

write 2

2 3 2

Page 32: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Is this problem inherent?

If we could only glue reads and writes…

read

write read

write

Page 33: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Challenge

public class Counter { private long value; public long getAndIncrement() { temp = value; value = temp + 1; return temp; } }

Page 34: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Challenge

public class Counter { private long value; public long getAndIncrement() { temp = value; value = temp + 1; return temp; } }

Make these steps atomic (indivisible)

Page 35: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Hardware Solution

public class Counter { private long value; public long getAndIncrement() { temp = value; value = temp + 1; return temp; } } ReadModifyWrite()

instruction

Page 36: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

An Aside: Java™

public class Counter { private long value; public long getAndIncrement() { synchronized { temp = value; value = temp + 1; } return temp; } }

Page 37: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

An Aside: Java™

public class Counter { private long value; public long getAndIncrement() { synchronized { temp = value; value = temp + 1; } return temp; } }

Synchronized block

Page 38: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

An Aside: Java™

public class Counter { private long value; public long getAndIncrement() { synchronized { temp = value; value = temp + 1; } return temp; } }

Mutual Exclusion

Page 39: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Mutual Exclusion

The problem of ensuring that no two processes or threads can be in their critical section at the same time.

Page 40: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

• An event a0 of thread A is – Instantaneous

– No simultaneous events (break ties)

a0

Events

Page 41: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

• A thread A is (formally) a sequence a0, a1, ... of events – “Trace” model

– Notation: a0 a1 indicates order

a0

Threads

a1 a2 …

Page 42: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• Assign to shared variable

• Assign to local variable

• Invoke method

• Return from method

• Lots of other things …

Example Thread Events

Page 43: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Threads are State Machines

Events are transitions

a0

a1 a2

a3

Page 44: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

States

• Thread State – Program counter

– Local variables

• System state – Object fields (shared variables)

– Union of thread states

Page 45: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

• Thread A

Concurrency

Page 46: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

time

• Thread A

• Thread B

Concurrency

Page 47: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

Interleavings

• Events of two or more threads – Interleaved

– Not necessarily independent (why?)

Page 48: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

• An interval A0 =(a0,a1) is – Time between events a0 and a1

a0 a1

Intervals

A0

Page 49: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

Intervals may Overlap

a0 a1 A0

b0 b1 B0

Page 50: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

Intervals may be Disjoint

a0 a1 A0

b0 b1 B0

Page 51: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

Precedence

a0 a1 A0

b0 b1 B0

Interval A0 precedes interval B0

Page 52: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Precedence

• Notation: A0 B0

• Formally, – End event of A0 before start event of B0

– Also called “happens before” or “precedes”

Page 53: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Precedence Ordering

• Never true that A A

• If A B then not true that B A

• If A B & B C then A C

• Funny thing: A B & B A might both be false!

Page 54: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Partial Orders (you may know this already)

• Irreflexive: – Never true that A A

• Antisymmetric: – If A B then not true that B A

• Transitive: – If A B & B C then A C

Page 55: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Total Orders (you may know this already)

• Also – Irreflexive

– Antisymmetric

– Transitive

• Except that for every distinct A, B, – Either A B or B A

Page 56: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Implementing a Counter

public class Counter { private long value; public long getAndIncrement() { temp = value; value = temp + 1; return temp; } }

Make these steps indivisible using

locks

Page 57: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Locks (Mutual Exclusion)

public interface Lock { public void lock(); public void unlock(); }

Page 58: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Locks (Mutual Exclusion)

public interface Lock {

public void lock(); public void unlock(); }

acquire lock

Page 59: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Locks (Mutual Exclusion)

public interface Lock { public void lock(); public void unlock(); }

release lock

acquire lock

Page 60: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp;

}}

Page 61: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; }}

acquire Lock

Page 62: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp;

}}

Release lock (no matter what)

Page 63: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp;

}}

Critical section

Page 64: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Mutual Exclusion

• Let CSik be thread i’s k-th critical

section execution

Page 65: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Mutual Exclusion

• Let CSik be thread i’s k-th critical

section execution

• And CSjm be thread j’s m-th critical

section execution

Page 66: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Mutual Exclusion

• Let CSik be thread i’s k-th critical

section execution

• And CSjm be j’s m-th execution

• Then either – or

Page 67: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Mutual Exclusion

• Let CSik be thread i’s k-th critical

section execution

• And CSjm be j’s m-th execution

• Then either – or

CSik CSj

m

Page 68: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Mutual Exclusion

• Let CSik be thread i’s k-th critical

section execution

• And CSjm be j’s m-th execution

• Then either – or

CSik CSj

m

CSjm CSi

k

Page 69: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Deadlock-Free

• If some thread calls lock() – And never returns

– Then other threads must complete lock() and unlock() calls infinitely often

• System as a whole makes progress – Even if individuals starve

Page 70: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Starvation-Free

• If some thread calls lock() – It will eventually return

• Individual threads make progress

Page 71: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

class … implements Lock { … // thread-local index, 0 or 1 public void lock() { int i = ThreadID.get(); int j = 1 - i; …

} }

Two-Thread Conventions

Page 72: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

class … implements Lock { … // thread-local index, 0 or 1 public void lock() { int i = ThreadID.get(); int j = 1 - i; …

} }

Two-Thread Conventions

Henceforth: i is current thread, j is other thread

Page 73: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

LockOne

class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) {} }

Page 74: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

LockOne

class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) {} }

Set my flag

Page 75: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) {} }

LockOne

Wait for other flag to go false

Set my flag

Page 76: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• Assume CSAj overlaps CSB

k

• Consider each thread's last (j-th and k-th) read and write in the lock() method before entering

• Derive a contradiction

LockOne Satisfies Mutual Exclusion

Page 77: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• writeA(flag[A]=true) readA(flag[B]==false) CSA

• writeB(flag[B]=true) readB(flag[A]==false) CSB

From the Code

class LockOne implements Lock { … public void lock() { flag[i] = true; while (flag[j]) {} }

Page 78: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• readA(flag[B]==false) writeB(flag[B]=true)

• readB(flag[A]==false) writeA(flag[B]=true)

From the Assumption

Page 79: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• Assumptions: – readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code – writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 80: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• Assumptions: – readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code – writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 81: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• Assumptions: – readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code – writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 82: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• Assumptions: – readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code – writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 83: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• Assumptions: – readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code – writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 84: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• Assumptions: – readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code – writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 85: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Cycle!

Page 86: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Deadlock Freedom

• LockOne Fails deadlock-freedom – Concurrent execution can deadlock

– Sequential executions OK

flag[i] = true; flag[j] = true; while (flag[j]){} while (flag[i]){}

Page 87: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

LockTwo public class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; } public void unlock() {} }

Page 88: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

LockTwo public class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; } public void unlock() {} }

Let other go first

Page 89: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

LockTwo public class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; } public void unlock() {} }

Wait for permission

Page 90: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

LockTwo public class Lock2 implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; } public void unlock() {} }

Nothing to do

Page 91: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

public void LockTwo() { victim = i; while (victim == i) {}; }

LockTwo Claims

• Satisfies mutual exclusion – If thread i in CS

– Then victim == j

– Cannot be both 0 and 1

• Not deadlock free – Sequential execution deadlocks

– Concurrent execution does not

Page 92: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Peterson’s Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; }

Page 93: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Peterson’s Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; }

Announce I’m interested

Page 94: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Peterson’s Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; }

Announce I’m interested

Defer to other

Page 95: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Peterson’s Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; }

Announce I’m interested

Defer to other

Wait while other interested & I’m

the victim

Page 96: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Peterson’s Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; }

Announce I’m interested

Defer to other

Wait while other interested & I’m

the victim No longer interested

Page 97: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};

Mutual Exclusion

• If thread 1 in critical section, – flag[1]=true,

– victim = 0

• If thread 0 in critical section, – flag[0]=true,

– victim = 1

Cannot both be true

Page 98: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Deadlock Free

• Thread blocked – only at while loop

– only if it is the victim

• One or the other must not be the victim

public void lock() { … while (flag[j] && victim == i) {};

Page 99: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Starvation Free

• Thread i blocked only if j repeatedly re-enters so that

flag[j] == true and victim == i

• When j re-enters – it sets victim to j. – So i gets in

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; } public void unlock() { flag[i] = false; }

Page 100: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Other Lock Algorithms

• The Filter Algorithm for n Threads

• Bakery Algorithm

Theorem: At least N MRSW (multi-reader/single-writer) registers are needed to solve deadlock-free mutual exclusion.

N registers like Flag[]…

Page 101: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

FIFO Queue: Enqueue Method

q.enq( )

Page 102: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

FIFO Queue: Dequeue Method

q.deq()/

Page 103: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

A Lock-Based Queue

class LockBasedQueue<T> { int head, tail; T[] items; Lock lock; public LockBasedQueue(int capacity) { head = 0; tail = 0; lock = new ReentrantLock(); items = (T[]) new Object[capacity]; }

Page 104: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

A Lock-Based Queue

class LockBasedQueue<T> { int head, tail; T[] items; Lock lock; public LockBasedQueue(int capacity) { head = 0; tail = 0; lock = new ReentrantLock(); items = (T[]) new Object[capacity]; }

0 1

capacity-1 2

head tail

y z

Queue fields protected by single shared lock

Page 105: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

A Lock-Based Queue

class LockBasedQueue<T> { int head, tail; T[] items; Lock lock; public LockBasedQueue(int capacity) { head = 0; tail = 0; lock = new ReentrantLock(); items = (T[]) new Object[capacity]; }

0 1

capacity-1 2

head tail

y z

Initially head = tail

Page 106: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Implementation: Deq

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

0 1

capacity-1 2

head tail

y z

Page 107: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Implementation: Deq

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Method calls mutually exclusive

0 1

capacity-1 2

head tail

y z

Page 108: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Implementation: Deq

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

If queue empty throw exception

0 1

capacity-1 2

head tail

y z

Page 109: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Implementation: Deq

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Queue not empty: remove item and update

head

0 1

capacity-1 2

head tail

y z

Page 110: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Implementation: Deq

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Return result

0 1

capacity-1 2

head tail

y z

Page 111: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Implementation: Deq

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } } Release lock no matter

what!

0 1

capacity-1 2

head tail

y z

Page 112: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Implementation: Deq

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Page 113: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Now consider the following implementation

• The same thing without mutual exclusion

• For simplicity, only two threads – One thread enq only

– The other deq only

Page 114: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming

114

Wait-free 2-Thread Queue public class WaitFreeQueue { int head = 0, tail = 0; items = (T[]) new Object[capacity]; public void enq(Item x) { while (tail-head == capacity); // busy-wait items[tail % capacity] = x; tail++; } public Item deq() { while (tail == head); // busy-wait Item item = items[head % capacity]; head++; return item; }}

Page 115: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Wait-free 2-Thread Queue public class LockFreeQueue { int head = 0, tail = 0; items = (T[]) new Object[capacity]; public void enq(Item x) { while (tail-head == capacity); // busy-wait items[tail % capacity] = x; tail++; } public Item deq() { while (tail == head); // busy-wait Item item = items[head % capacity]; head++; return item; }}

0 1

capacity-1 2

head tail

y z

Page 116: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Lock-free 2-Thread Queue public class LockFreeQueue { int head = 0, tail = 0; items = (T[])new Object[capacity]; public void enq(Item x) { while (tail-head == capacity); // busy-wait items[tail % capacity] = x; tail++; } public Item deq() { while (tail == head); // busy-wait Item item = items[head % capacity]; head++; return item; }}

0 1

capacity-1 2

head tail

y z

Queue is updated without a lock!

Page 117: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Defining concurrent queue implementations

• Need a way to specify a concurrent queue object

• Need a way to prove that an algorithm implements the object’s specification

• Lets talk about object specifications …

Page 118: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential Objects

• Each object has a state – Usually given by a set of fields

– Queue example: sequence of items

• Each object has a set of methods – Only way to manipulate state

– Queue example: enq and deq methods

Page 119: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential Specifications • If (precondition)

– the object is in such-and-such a state

– before you call the method,

• Then (postcondition)

– the method will return a particular value

– or throw a particular exception.

• and (postcondition, con’t)

– the object will be in some other state

– when the method returns,

Page 120: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Pre and PostConditions for Dequeue

• Precondition: – Queue is non-empty

• Postcondition: – Returns first item in queue

• Postcondition: – Removes first item in queue

Page 121: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Pre and PostConditions for Dequeue

• Precondition: – Queue is empty

• Postcondition: – Throws Empty exception

• Postcondition: – Queue state unchanged

Page 122: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

What About Concurrent Specifications ?

• Methods?

• Documentation?

• Adding new methods?

Page 123: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Methods Take Time

time time

Page 124: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Methods Take Time

time

invocation 12:00

q.enq(...)

time

Page 125: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming

125

Methods Take Time

time

Method call

invocation 12:00

q.enq(...)

time

Page 126: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Methods Take Time

time

Method call

invocation 12:00

q.enq(...)

time

Page 127: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Methods Take Time

time

Method call

invocation 12:00

q.enq(...)

time

void

response 12:01

Page 128: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential vs Concurrent

• Sequential – Methods take time? Who knew?

• Concurrent – Method call is not an event

– Method call is an interval.

Page 129: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

Concurrent Methods Take Overlapping Time

time

Page 130: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

Concurrent Methods Take Overlapping Time

time

Method call

Page 131: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

Concurrent Methods Take Overlapping Time

time

Method call

Method call

Page 132: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

Concurrent Methods Take Overlapping Time

time

Method call Method call

Method call

Page 133: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential vs Concurrent

• Sequential: – Object needs meaningful state only

between method calls

• Concurrent – Because method calls overlap, object

might never be between method calls

Page 134: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential vs Concurrent

• Sequential: – Each method described in isolation

• Concurrent – Must characterize all possible

interactions with concurrent calls • What if two enqs overlap?

• Two deqs? enq and deq? …

Page 135: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential vs Concurrent

• Sequential: – Can add new methods without affecting

older methods

• Concurrent: – Everything can potentially interact with

everything else

Page 136: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential vs Concurrent

• Sequential: – Can add new methods without affecting

older methods

• Concurrent: – Everything can potentially interact with

everything else

Page 137: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Intuitively…

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Page 138: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Intuitively…

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

All modifications of queue are done mutually exclusive

Page 139: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

time

Intuitively

q.deq

q.enq

enq deq

lock() unlock()

lock() unlock() Behavior is “Sequential”

enq

deq

Lets capture the idea of describing the concurrent via the sequential

Page 140: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Is it really about the object?

• Each method should – “take effect”

– Instantaneously

– Between invocation and response events

• Object is correct if this “sequential” behavior is correct

• A linearizable object: one all of whose possible executions are linearizable

Page 141: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time time

Page 142: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

time

Page 143: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.enq(y)

time

Page 144: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.enq(y) q.deq(x)

time

Page 145: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.enq(y) q.deq(x)

q.deq(y)

time

Page 146: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.enq(y) q.deq(x)

q.deq(y) q.enq(x)

q.enq(y) q.deq(x)

q.deq(y)

time

Page 147: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.enq(y) q.deq(x)

q.deq(y) q.enq(x)

q.enq(y) q.deq(x)

q.deq(y)

time

Page 148: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

Page 149: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

Page 150: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x) q.deq(y)

Page 151: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.enq(y)

q.deq(y)

Page 152: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.enq(y)

q.deq(y) q.enq(x)

q.enq(y)

Page 153: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.enq(y)

q.deq(y) q.enq(x)

q.enq(y)

Page 154: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time time

Page 155: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

time

Page 156: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.deq(x)

time

Page 157: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.deq(x)

q.enq(x)

q.deq(x)

time

Page 158: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.deq(x)

q.enq(x)

q.deq(x)

time

Page 159: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

time

Page 160: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.enq(y)

time

Page 161: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.enq(y)

q.deq(y)

time

Page 162: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

q.enq(x)

q.enq(y)

q.deq(y)

q.deq(x)

time

Page 163: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

q.enq(x)

q.enq(y)

q.deq(y)

q.deq(x)

Comme ci Example

time

Comme ça

Page 164: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(0)

Page 165: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(0)

write(1) already happened

Page 166: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(0) write(1)

write(1) already happened

Page 167: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(0) write(1)

write(1) already happened

Page 168: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(1)

write(1) already happened

Page 169: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(1) write(1)

write(2)

write(1) already happened

Page 170: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(1) write(1)

write(2)

write(1) already happened

Page 171: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

write(0)

write(1)

write(2)

time

read(1)

Page 172: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

write(0)

write(1)

write(2)

time

read(1) write(1)

write(2)

Page 173: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

write(0)

write(1)

write(2)

time

read(1) write(1)

write(2)

Page 174: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(1)

Page 175: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(1) write(1)

Page 176: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(1) write(1)

write(2)

Page 177: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(2) write(1)

write(2)

Page 178: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Talking About Executions

• Why? – Can’t we specify the linearization point

of each operation without describing an execution?

• Not Always – In some cases, linearization point

depends on the execution

Page 179: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Formal Model of Executions

• Define precisely what we mean – Ambiguity is bad when intuition is weak

• Allow reasoning – Formal

– But mostly informal

Page 180: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Split Method Calls into Two Events

• Invocation – method name & args – q.enq(x)

• Response – result or exception – q.enq(x) returns void – q.deq() returns x – q.deq() throws empty

Page 181: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Invocation Notation

A q.enq(x)

Page 182: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Invocation Notation

A q.enq(x)

thread

Page 183: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Invocation Notation

A q.enq(x)

thread method

Page 184: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Invocation Notation

A q.enq(x)

thread

object

method

Page 185: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Invocation Notation

A q.enq(x)

thread

object

method

arguments

Page 186: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Response Notation

A q: void

Page 187: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Response Notation

A q: void

thread

Page 188: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Response Notation

A q: void

thread result

Page 189: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Response Notation

A q: void

thread

object

result

Page 190: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

History - Describing an Execution

A q.enq(3) A q:void A q.enq(5) B p.enq(4) B p:void B q.deq() B q:3

Sequence of invocations and

responses

H =

Page 191: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Definition

• Invocation & response match if

A q.enq(3)

A q:void

Thread names agree

Object names agree

Method call

Page 192: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Object Projections

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3

H =

Page 193: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Object Projections

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3

H|q =

Page 194: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Thread Projections

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3

H =

Page 195: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Thread Projections

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3

H|B =

Page 196: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Complete Subhistory

A q.enq(3) A q:void A q.enq(5) B p.enq(4) B p:void B q.deq() B q:3

An invocation is pending if it has no matching respnse

H =

Page 197: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Complete Subhistory

A q.enq(3) A q:void A q.enq(5) B p.enq(4) B p:void B q.deq() B q:3

May or may not have taken effect

H =

Page 198: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Complete Subhistory

A q.enq(3) A q:void A q.enq(5) B p.enq(4) B p:void B q.deq() B q:3

discard pending invocations

H =

Page 199: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Complete Subhistory

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3

Complete(H) =

Page 200: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential Histories

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5)

Page 201: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential Histories

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5)

match

Page 202: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential Histories

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5)

match

match

Page 203: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential Histories

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5)

match

match

match

Page 204: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential Histories

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5)

match

match

match

Final pending invocation OK

Page 205: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential Histories

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5)

match

match

match

Final pending invocation OK

Page 206: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Well-Formed Histories

H=

A q.enq(3) B p.enq(4) B p:void B q.deq() A q:void B q:3

Page 207: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Well-Formed Histories

H=

A q.enq(3) B p.enq(4) B p:void B q.deq() A q:void B q:3

H|B= B p.enq(4) B p:void B q.deq() B q:3

Per-thread projections sequential

Page 208: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Well-Formed Histories

H=

A q.enq(3) B p.enq(4) B p:void B q.deq() A q:void B q:3

H|B= B p.enq(4) B p:void B q.deq() B q:3

A q.enq(3) A q:void

H|A=

Per-thread projections sequential

Page 209: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Equivalent Histories

H=

A q.enq(3) B p.enq(4) B p:void B q.deq() A q:void B q:3

Threads see the same thing in both

A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3

G=

H|A = G|A H|B = G|B

Page 210: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Sequential Specifications

• A sequential specification is some way of telling whether a – Single-thread, single-object history

– Is legal

• For example: – Pre and post-conditions

– But plenty of other techniques exist …

Page 211: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Legal Histories

• A sequential (multi-object) history H is legal if – For every object x

– H|x is in the sequential spec for x

Page 212: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Precedence

A q.enq(3) B p.enq(4) B p.void A q:void B q.deq() B q:3

A method call precedes another if response event precedes invocation event

Method call Method call

Page 213: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Non-Precedence

A q.enq(3) B p.enq(4) B p.void B q.deq() A q:void B q:3

Some method calls overlap one another

Method call

Method call

Page 214: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Notation

• Given – History H – method executions m0 and m1 in H

• We say m0 H m1, if

– m0 precedes m1

• Relation m0 H m1 is a – Partial order – Total order if H is sequential

m0 m1

Page 215: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Linearizability

• History H is linearizable if it can be extended to G by – Appending zero or more responses to

pending invocations – Discarding other pending invocations

• So that G is equivalent to – Legal sequential history S – where G S

Page 216: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

What is G S

time

a

b

time S

c G

G = {ac,bc}

S = {ab,ac,bc}

Page 217: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Remarks

• Some pending invocations – Took effect, so keep them

– Discard the rest

• Condition G S – Means that S respects “real-time order”

of G

Page 218: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 B q:enq(6)

Example

time

B.q.enq(4)

A. q.enq(3)

B.q.deq(4) B. q.enq(6)

Page 219: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming

219

Example

Complete this pending invocation

time

B.q.enq(4) B.q.deq(3) B. q.enq(6)

A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 B q:enq(6)

A. q.enq(3)

Page 220: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

Complete this pending invocation

time

B.q.enq(4) B.q.deq(4) B. q.enq(6)

A.q.enq(3)

A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 B q:enq(6) A q:void

Page 221: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Example

time

B.q.enq(4) B.q.deq(4) B. q.enq(6)

A.q.enq(3)

A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 B q:enq(6) A q:void

discard this one

Page 222: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming

222

Example

time

B.q.enq(4) B.q.deq(4)

A.q.enq(3)

A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 A q:void

discard this one

Page 223: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 A q:void

Example

time

B.q.enq(4) B.q.deq(4)

A.q.enq(3)

Page 224: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 A q:void

Example

time

B q.enq(4) B q:void A q.enq(3) A q:void B q.deq() B q:4

B.q.enq(4) B.q.deq(4)

A.q.enq(3)

Page 225: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

B.q.enq(4) B.q.deq(4)

A.q.enq(3)

A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 A q:void

Example

time

B q.enq(4) B q:void A q.enq(3) A q:void B q.deq() B q:4

Equivalent sequential history

Page 226: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Reasoning About Linearizability: Locking

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

0 1 capacity-1

2

head tail

y z

Page 227: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Reasoning About Linearizability: Locking

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Linearization points are when locks are

released

Page 228: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

228

More Reasoning: Wait-free 0 1

capacity-1 2

head tail

y z

public class WaitFreeQueue { int head = 0, tail = 0; items = (T[]) new Object[capacity]; public void enq(Item x) { if (tail-head == capacity) throw new FullException(); items[tail % capacity] = x; tail++; } public Item deq() { if (tail == head) throw new EmptyException(); Item item = items[head % capacity]; head++; return item; }}

0 1 capacity-1

2

head tail

y z

Page 229: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

public class WaitFreeQueue { int head = 0, tail = 0; items = (T[]) new Object[capacity]; public void enq(Item x) { if (tail-head == capacity) throw new FullException(); items[tail % capacity] = x; tail++; } public Item deq() { if (tail == head) throw new EmptyException(); Item item = items[head % capacity]; head++; return item; }}

More Reasoning: Wait-free

0 1 capacity-1

2

head tail

y z Linearization order is order head and tail

fields modified

Page 230: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Linearizability: Summary

• Powerful specification tool for shared objects

• Allows us to capture the notion of objects being “atomic”

• Don’t leave home without it

Page 231: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Ordered linked list implementation of a set

a b c -∞ +∞

Page 232: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c

Sorted with Sentinel nodes (min & max possible keys)

-∞

+∞

Defining the linked list

Page 233: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Defining concurrent methods properties

• Invariant: – Property that always holds.

– Established because

– True when object is created.

– Truth preserved by each method • Each step of each method.

Page 234: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Defining concurrent methods properties

• Rep-Invariant: – The invariant on our concrete

Representation = on the list.

– Preserved by methods.

– Relied on by methods.

– Allows us to reason about each method in isolation without considering how they interact.

Page 235: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Defining concurrent methods properties

• Our Rep-invariant: – Sentinel nodes

• tail reachable from head.

– Sorted

– No duplicates

• Depends on the implementation.

Page 236: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Defining concurrent methods properties

• Abstraction Map:

• S(List) = – { x | there exists a such that

• a reachable from head and

• a.item = x

– }

• Depends on the implementation.

Page 237: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Abstract Data Types

• Example:

– S( ) = {a,b} a b

a b

• Concrete representation:

• Abstract Type: – {a, b}

Page 238: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Defining concurrent methods properties

• Wait-free: Every call to the function finishes in a finite number of steps.

Supposing the Scheduler is fair:

• Starvation-free: every thread calling the method eventually returns.

Page 239: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Algorithms

• Next: going throw each algorithm. • 1. Describing the algorithm.

• 2. Explaining why every step of the algorithm is needed.

• 3. Code review.

• 4. Analyzing each method properties.

• 5. Advantages / Disadvantages.

• 6. Presenting running times for the implementation of the algorithm.

• + Example of proving correctness for Remove(x) in FineGrained.

Page 240: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

240

0.Sequential List Based Set

a c d

a b c

Add()

Remove()

Page 241: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a c d

b

a b c

Add()

Remove()

0.Sequential List Based Set

Page 242: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

1.Course Grained

a b d

1. Describing the algorithm:

• Most common implementation

today.

• Add(x) / Remove(x) / Contains(x): - Lock the entire list then perform the operation.

Page 243: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

1.Course Grained

a b d

c

• Most common implementation today

1. Describing the algorithm:

• All methods perform operations on the list while holding the lock, so the execution is essentially sequential.

Page 244: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Code review:

Add:

public boolean add(T item) {

Node pred, curr;

int key = item.hashCode();

lock.lock();

try {

pred = head;

curr = pred.next;

while (curr.key < key) {

pred = curr;

curr = curr.next;

}

if (key == curr.key) {

return false;

} else {

Node node = new Node(item);

node.next = curr;

pred.next = node;

return true;

}

} finally {

lock.unlock();

}

}

Finding the place to add the item

Adding the item if it wasn’t already in the list

1.Course Grained

Page 245: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 245

3. Code review:

Remove:

public boolean remove(T item) {

Node pred, curr;

int key = item.hashCode();

lock.lock();

try {

pred = this.head;

curr = pred.next;

while (curr.key < key) {

pred = curr;

curr = curr.next;

}

if (key == curr.key) {

pred.next = curr.next;

return true;

} else {

return false;

}

} finally {

lock.unlock();

}

}

Finding the item

Removing the item

1.Course Grained

Page 246: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

246

3. Code review:

Contains:

public boolean contains(T item) {

Node pred, curr;

int key = item.hashCode();

lock.lock();

try {

pred = head;

curr = pred.next;

while (curr.key < key) {

pred = curr;

curr = curr.next;

}

return (key == curr.key);

} finally {lock.unlock();

}

}

Finding the item

Returning true if found

1.Course Grained

Page 247: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Methods properties:

• The implementation inherits its progress

conditions from those of the Lock, and so assuming fair Scheduler:

- If the Lock implementation is Starvation free

Every thread will eventually get the lock and eventually the call to the function will return.

• So our implementation of Insert, Remove and Contains is Starvation-free

1.Course Grained

Page 248: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

5. Advantages / Disadvantages:

Advantages:

- Simple.

- Obviously correct.

Disadvantages:

- High Contention.

- Bottleneck!

1.Course Grained

Page 249: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

6. Running times:

1.Course Grained

• The tests were run on Aries – Supports 32 running threads. UltraSPARC T1 - Sun Fire T2000.

• Total of 200000 operations.

• 10% adds, 2% removes, 88% contains – normal work load percentages on a set.

• Each time the list was initialized with 100 elements.

• One run with a max of 20000 items in the list. Another with only 2000.

Page 250: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

6. Running times:

1.Course Grained

Speed up

0

5

10

15

20

25

30

4 8 12 16 20 24 28 32

No. Threads

Seco

nd

s 2000 maxitems in list

20000 maxitems in list

Page 251: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

2.Fine Grained

1. Describing the algorithm:

• Split object into pieces – Each piece has own lock.

– Methods that work on disjoint pieces need not exclude each other.

Page 252: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

2.Fine Grained

• Add(x) / Remove(x) / Contains(x):

– Go throw the list, lock each node and release only after the lock of the next element has been acquired.

– Once you have reached the right point of the list perform the Add / Remove / Contains operation.

1. Describing the algorithm:

Page 253: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b)

2.Fine Grained

1. Describing the algorithm: illustrated Remove.

Page 254: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b)

2.Fine Grained

1. Describing the algorithm: illustrated Remove.

Page 255: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b)

2.Fine Grained

1. Describing the algorithm: illustrated Remove.

Page 256: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b)

2.Fine Grained

1. Describing the algorithm: illustrated Remove.

Page 257: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b)

2.Fine Grained

1. Describing the algorithm: illustrated Remove.

Page 258: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a c d

remove(b)

2.Fine Grained

1. Describing the algorithm: illustrated Remove.

Page 259: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Why do we need to always hold 2 locks?

2.Fine Grained

2. Explaining why every step is needed.

Page 260: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(c) remove(b)

2.Fine Grained

Concurrent removes

2. Explaining why every step is needed.

Page 261: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

2.Fine Grained

Concurrent removes

2. Explaining why every step is needed.

Page 262: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

2.Fine Grained

Concurrent removes

2. Explaining why every step is needed.

Page 263: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

2.Fine Grained

Concurrent removes

2. Explaining why every step is needed.

Page 264: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

2.Fine Grained

Concurrent removes

2. Explaining why every step is needed.

Page 265: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

2.Fine Grained

Concurrent removes

2. Explaining why every step is needed.

Page 266: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Concurrent Removes

a b c d

remove(b) remove(c)

2. Explaining why every step is needed.

Page 267: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Concurrent Removes

a b c d

remove(b) remove(c)

2. Explaining why every step is needed.

Page 268: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a c d

remove(b) remove(c)

2.Fine Grained

Concurrent removes

2. Explaining why every step is needed.

Page 269: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a c d

Bad news, C not removed remove(b)

remove(c)

2.Fine Grained

Concurrent removes

2. Explaining why every step is needed.

Page 270: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 271: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 272: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 273: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 274: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 275: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 276: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 277: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

remove(b) remove(c)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 278: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

Must acquire

Lock of b

remove(c)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 279: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

Cannot acquire

lock of b

remove(c)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 280: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b c d

Wait! remove(c)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 281: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b d

Proceed to

remove(b)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 282: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b d

remove(b)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 283: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a b d

remove(b)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 284: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a d

remove(b)

Concurrent removes

Now with 2 locks.

2.Fine Grained

2. Explaining why every step is needed.

Page 285: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

2.Fine Grained

• Conclusion:

• Now that we hold 2 locks for Remove / Add / Contains. If a node is locked : – It can’t be removed and so does the next node in the

list.

– No new node can be added before it and after it.

2. Explaining why every step is needed.

Page 286: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 106

Remove method

public boolean remove(Item item) {int key = item.hashCode();Node pred, curr;try {…

} finally {curr.unlock();pred.unlock();}}

Page 287: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 107

Remove method

public boolean remove(Item item) {int key = item.hashCode();Node pred, curr;try {…

} finally {curr.unlock();pred.unlock();}}

Key used to order node

Page 288: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 108

Remove method

public boolean remove(Item item) {int key = item.hashCode();Node pred, curr;try {…

} finally {currNode.unlock();predNode.unlock();}}

Predecessor and current nodes

Page 289: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 109

Remove method

public boolean remove(Item item) {int key = item.hashCode();Node pred, curr;try {…

} finally {curr.unlock();pred.unlock();}}

Make sure locks released

Page 290: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 110

Remove method

public boolean remove(Item item) {int key = item.hashCode();Node pred, curr;try {…

} finally {curr.unlock();pred.unlock();}}

Everything else

Page 291: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 111

Remove method

try {pred = this.head;pred.lock();curr = pred.next;curr.lock();…} finally { … }

Page 292: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 112

Remove method

try {pred = this.head;pred.lock();curr = pred.next;curr.lock();…} finally { … }

lock pred == head

Page 293: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 113

Remove method

try {pred = this.head;pred.lock();curr = pred.next;curr.lock();…} finally { … }

Lock current

Page 294: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 114

Remove method

try {pred = this.head;pred.lock();curr = pred.next;curr.lock();…} finally { … }

Traversing list

Page 295: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 115

Remove: searching

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

Page 296: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 116

Remove: searching

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

Search key range

Page 297: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 117

Remove: searching

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

At start of each loop: curr and pred locked

Page 298: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 118

Remove: searching

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;If item found, remove node

Page 299: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 119

Remove: searching

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;If node found, remove it

Page 300: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 120

Remove: searching

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

Unlock predecessor

Page 301: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 121

Remove: searching

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

Only one node locked!

Page 302: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 122

Remove: searching

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

demote current

Page 303: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 123

Remove: searching

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = currNode;curr = curr.next;curr.lock();}return false;

Find and lock new current

Page 304: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 124

Remove: searchingwhile (curr.key <= key) {

if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = currNode;curr = curr.next;curr.lock();}return false;

Lock invariant restored

Page 305: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 125

Remove: searching

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

Otherwise, not present

Page 306: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Code review:

Add:

public boolean add(T item) {

int key = item.hashCode();

head.lock();

Node pred = head;

try {

Node curr = pred.next;

curr.lock();

try {

while (curr.key < key) {

pred.unlock();

pred = curr;

curr = curr.next;

curr.lock();

}

Finding the place to add the item:

2.Fine Grained

if (curr.key == key) {

return false;

}

Node newNode = new Node(item);

newNode.next = curr;

pred.next = newNode;

return true;

} finally {

curr.unlock();

}

} finally {

pred.unlock();

}

}

Adding the item:

Continued:

Page 307: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Code review:

Contains:

public boolean contains(T item) {

Node pred = null, curr = null;

int key = item.hashCode();

head.lock();

try {

pred = head;

curr = pred.next;

curr.lock();

try {

while (curr.key < key) {

pred.unlock();

pred = curr;

curr = curr.next;

curr.lock();

}

2.Fine Grained

return (curr.key == key);

} finally {

curr.unlock();

}

} finally {

pred.unlock();

}

}

Return true iff found

Continued:

Finding the place to add the item:

Page 308: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Proving correctness for Remove(x) function:

2.Fine Grained

• So how do we prove correctness of a method in a concurrent environment?

1. Decide on a Rep-Invariant. Done!

2. Decide on an Abstraction map. Done!

3. Defining the operations: Remove(x): If x in the set => x won’t be in the set and return

true.

If x isn’t in the set => don’t change the set and return false. Done!

Page 309: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Proving correctness for Remove(x) function:

2.Fine Grained

4. Proving that each function keeps the Rep-Invariant:

1. Tail reachable from head.

2. Sorted.

3. No duplicates.

1. The newly created empty list obviously keeps the Rep-invariant.

2. Easy to see from the code that for each function if the Rep-invariant was kept before the call it will still hold after it. Done!

Page 310: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Proving correctness for Remove(x) function:

2.Fine Grained

5. Split the function to all possible run time outcomes.

In our case:

1. Successful remove. (x was in the list)

2. Failed remove. (x wasn’t in the list)

Done!

6. Proving for each possibility.

We will start with a successful remove. (failed remove is not much different)

Page 311: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Proving correctness for Remove(x) function:

2.Fine Grained

6. Deciding on a linearization point for a successful remove. Reminder: Linearization point – a point in time that we

can say the function has happened in a running execution.

We will set the Linearization point to after the second lock was acquired. Done!

successful remove.

Page 312: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 127

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

Why remove() is linearizable

•pred reachable from head•curr is pred.next•So curr.item is in the set

Page 313: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 128

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

Why remove() is linearizable

Linearization point ifitem is present

Page 314: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 129

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

Why remove() is linearizable

Node locked, so no other thread can remove it ….

Page 315: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 130

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

Why remove() is linearizable

Item not present

Page 316: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 131

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

Why remove() is linearizable

•pred reachable from head•curr is pred.next•pred.key < key •key < curr.key

Page 317: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 132

while (curr.key <= key) {if (item == curr.item) {pred.next = curr.next;return true;}pred.unlock();pred = curr;curr = curr.next;curr.lock();}return false;

Why remove() is linearizable

Linearization point

Page 318: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Proving correctness for Remove(x) function:

2.Fine Grained

7. Now that the linearization point is set we need to prove that:

7.1. Before the linearization point the set contained x.

7.2. After it the set won’t contain x.

successful remove.

Page 319: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

296

Proving correctness for Remove(x) function:

2.Fine Grained

7.1. Before the linearization point the set contained x.

1. Since we proved the Rep-Invariant holds then pred=z is accessible from the head.

2. Since z,x are locked. No other concurrent call can remove them.

3. Since curr=x is pointed to by pred then x is also accessible from the head.

y z x w

successful remove.

Page 320: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

297

Proving correctness for Remove(x) function:

2.Fine Grained

– S( ) = {a,b}

y z x w

7.1. Before the linearization point the set contained x. Now by the Abstraction map definition:

since x is reachable from the head => x is in the set! Done!

a b

successful remove.

Page 321: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Proving correctness for Remove(x) function:

2.Fine Grained

7.1. After it the set won’t contain x.

1. after the linearization point: pred.next = curr.next;

Curr=x won’t be pointed to by pred=z and so won’t be accessible from head.

y z x w

successful remove.

Page 322: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Proving correctness for Remove(x) function:

2.Fine Grained

7.1. After it the set won’t contain x.

2. Now by the Abstraction map definition:

since x is not reachable from the head => x is not in the set! Done!

y z x w

successful remove.

Page 323: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Proving correctness for Remove(x) function:

2.Fine Grained

• In conclusion:

– For every possible run time execution for Remove(x) we found a linearization point that holds the remove function specification in the set using the Abstraction map while holding the Rep-Invariant.

Done!

Page 324: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Methods properties:

2.Fine Grained

• Assuming fair scheduler. If the Lock implementation is Starvation free:

Every thread will eventually get the lock and since all methods move in the same direction in the list there won’t be deadlock and eventually the call to the function will return.

• So our implementation of Insert, Remove and Contains is Starvation-free.

Page 325: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

5. Advantages / Disadvantages:

Advantages:

- Better than coarse-grained lock

Threads can traverse in parallel.

Disadvantages:

- Long chain of acquire/release.

- Inefficient.

2.Fine Grained

Page 326: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

6. Running times:

Speed up

0

10

20

30

40

50

4 8 12 16 20 24 28 32

No. Threads

Seco

nd

s 2000 maxitems in list

20000 maxitems in list

2.Fine Grained

Page 327: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

6. Running times:

Speed up

max of 2000 items

02468

1012

4 8 12 16 20 24 28 32

No. Threads

Seco

nd

s Fine List

CoarseGrained

2.Fine Grained

Page 328: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

6. Running times:

Speed up

max of 20000 items

0

10

20

30

40

50

4 8 12 16 20 24 28 32

No. Threads

Seco

nd

s Fine List

CoarseGrained

2.Fine Grained

Page 329: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Optimistic

1. Describing the algorithm:

Add(x) / Remove (x) / Contains(x):

1. Find nodes without locking

2. Lock nodes

3. Check that everything is OK = Validation.

3.1 Check that pred is still reachable from head.

3.2 Check that pred still points to curr.

4. If validation passed => do the operation.

Page 330: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

b d e a

add(c) Aha!

3. Optimistic

1. Describing the algorithm:

• Example of add(c):

Finding without locking

Page 331: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

b d e a

add(c)

1. Describing the algorithm:

Locking • Example of add(c):

3. Optimistic

Page 332: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

b d e a

add(c)

1. Describing the algorithm:

Validation 1 • Example of add(c):

3. Optimistic

Page 333: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

b d e a

add(c)

1. Describing the algorithm:

Validation 1 • Example of add(c):

3. Optimistic

Page 334: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

b d e a

add(c)

1. Describing the algorithm:

Validation 2 • Example of add(c):

Yes. b is still reachable from head.

3. Optimistic

Page 335: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

b d e a

add(c)

1. Describing the algorithm:

Validation 2 • Example of add(c):

Yes. b still points to d.

3. Optimistic

Page 336: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

b d e a

add(c)

c

3. Optimistic

1. Describing the algorithm:

Add c. • Example of add(c):

Page 337: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Why do we need to Validate?

3. Optimistic

2. Explaining why every step is needed.

Page 338: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Optimistic

• First: Why do we need to validate that pred is accessible from head?

• Thread A Adds(c).

• After thread A found b, before A locks. Another thread removes b.

2. Explaining why every step is needed.

b d e a

Page 339: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

b d e a

add(c) Aha!

3. Optimistic

• Adds(c). Finding without locking

2. Explaining why every step is needed.

Page 340: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

d e a

add(c)

3. Optimistic

b

Another thread removed b

• Adds(c).

2. Explaining why every step is needed.

Page 341: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

d e a

add(c)

3. Optimistic

b

Now A locks b and d

• Adds(c).

2. Explaining why every step is needed.

Page 342: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

d e a

add(c)

3. Optimistic

b

And adds c

c

• Adds(c).

2. Explaining why every step is needed.

Page 343: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

d e a

3. Optimistic

b

Now frees the locks.

But c isn’t added!

c

• Adds(c).

2. Explaining why every step is needed.

Page 344: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Optimistic

• Second: Why do we need to validate that pred Still points to curr?

• Thread A removes(d).

• then thread A found b, before A locks. Another thread adds(c).

2. Explaining why every step is needed.

b d e a

Page 345: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

b d e a

add(c) Aha!

3. Optimistic

• Removes(d) Finding without locking

2. Explaining why every step is needed.

Page 346: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

d e a

add(c)

3. Optimistic

b

Another thread Adds(c)

c

• Removes(d)

2. Explaining why every step is needed.

Page 347: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

d e a

add(c)

3. Optimistic

b

Now A locks.

c

• Removes(d)

2. Explaining why every step is needed.

Page 348: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

d e a

add(c)

3. Optimistic

b

pred.next = curr.next;

• Removes(d)

c

2. Explaining why every step is needed.

Page 349: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Optimistic

Instead c and d were deleted!

• Removes(d)

d e a b

c

Now frees the locks.

2. Explaining why every step is needed.

Page 350: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 149

What Else Could Go Wrong?

b d ea

add(c) Aha!

Page 351: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 150

What Else Coould Go Wrong?

b d ea

add(c)add(b’)

Page 352: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 151

What Else Coould Go Wrong?

b d ea

add(c)add(b’)b’

Page 353: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 152

What Else Could Go Wrong?

b d ea

add(c)b’

Page 354: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 153

What Else Could Go Wrong?

b d ea

add(c)

c

Page 355: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Optimistic

• Do notice that threads might traverse deleted nodes. May cause problems to our Rep-Invariant.

• Careful not to recycle to the lists nodes that were deleted while threads are in a middle of an operation.

• With a garbage collection language like java – ok.

• For C – you need to solve this manually.

Important comment.

Page 356: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 158

Correctness

• If– Nodes b and c both locked– Node b still accessible– Node c still successor to b

• Then– Neither will be deleted– OK to delete and return true

Page 357: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 159

Unsuccessful Remove

a b d e

remove(c)

Aha!

Page 358: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 160

Validate (1)

a b d e

Yes, b still reachable from head

remove(c)

Page 359: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 161

Validate (2)

a b d e

remove(c) Yes, b still points to d

Page 360: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 162

OK Computer

a b d e

remove(c) return false

Page 361: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 163

Correctness

• If– Nodes b and d both locked– Node b still accessible– Node d still successor to b

• Then– Neither will be deleted– No thread can add c after b– OK to return false

Page 362: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 164

Validationprivate booleanvalidate(Node pred,

Node curry) {Node node = head;while (node.key <= pred.key) {if (node == pred)return pred.next == curr;node = node.next;}return false;}

Page 363: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 165

private booleanvalidate(Node pred,

Node curr) {Node node = head;while (node.key <= pred.key) {if (node == pred)return pred.next == curr;node = node.next;}return false;

}

Validation

Predecessor & current nodes

Page 364: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 166

private booleanvalidate(Node pred,

Node curr) {Node node = head;while (node.key <= pred.key) {if (node == pred)return pred.next == curr;node = node.next;}return false;

}

Validation

Begin at the beginning

Page 365: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 167

private booleanvalidate(Node pred,

Node curr) {Node node = head;while (node.key <= pred.key) {if (node == pred)return pred.next == curr;node = node.next;}return false;

}

Validation

Search range of keys

Page 366: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 168

private booleanvalidate(Node pred,

Node curr) {Node node = head;while (node.key <= pred.key) {if (node == pred)return pred.next == curr;node = node.next;}return false;

}

Validation

Predecessor reachable

Page 367: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 169

private booleanvalidate(Node pred,

Node curr) {Node node = head;while (node.key <= pred.key) {if (node == pred)return pred.next == curr;node = node.next;}return false;

}

Validation

Is current node next?

Page 368: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 170

private booleanvalidate(Node pred,

Node curr) {Node node = head;while (node.key <= pred.key) {if (node == pred)return pred.next == curr;node = node.next;}return false;

}

ValidationOtherwise move on

Page 369: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 171

private booleanvalidate(Node pred,

Node curr) {Node node = head;while (node.key <= pred.key) {if (node == pred)return pred.next == curr;node = node.next;}return false;

}

ValidationPredecessor not reachable

Page 370: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 172

Remove: searchingpublic boolean remove(Item item) {int key = item.hashCode();retry: while (true) {Node pred = this.head;Node curr = pred.next;while (curr.key <= key) {if (item == curr.item)

break;pred = curr;curr = curr.next;} …

Page 371: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 173

public boolean remove(Item item) {int key = item.hashCode();retry: while (true) {Node pred = this.head;Node curr = pred.next;while (curr.key <= key) {if (item == curr.item)break;pred = curr;curr = curr.next;} …

Remove: searching

Search key

Page 372: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 174

public boolean remove(Item item) {int key = item.hashCode();retry: while (true) {Node pred = this.head;Node curr = pred.next;while (curr.key <= key) {if (item == curr.item)break;pred = curr;curr = curr.next;} …

Remove: searching

Retry on synchronization conflict

Page 373: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 175

public boolean remove(Item item) {int key = item.hashCode();retry: while (true) {Node pred = this.head;Node curr = pred.next;while (curr.key <= key) {if (item == curr.item)break;pred = curr;curr = curr.next;} …

Remove: searching

Examine predecessor and current nodes

Page 374: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 176

public boolean remove(Item item) {int key = item.hashCode();retry: while (true) {Node pred = this.head;Node curr = pred.next;while (curr.key <= key) {if (item == curr.item)break;pred = curr;curr = curr.next;} …

Remove: searching

Search by key

Page 375: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 177

public boolean remove(Item item) {int key = item.hashCode();retry: while (true) {Node pred = this.head;Node curr = pred.next;while (curr.key <= key) {if (item == curr.item)break;pred = curr;curr = curr.next;} …

Remove: searching

Stop if we find item

Page 376: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 178

public boolean remove(Item item) {int key = item.hashCode();retry: while (true) {Node pred = this.head;Node curr = pred.next;while (curr.key <= key) {if (item == curr.item)break;pred = curr;curr = curr.next;} …

Remove: searching

Move along

Page 377: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 180

Remove Methodtry {pred.lock(); curr.lock();if (validate(pred,curr) {if (curr.item == item) {pred.next = curr.next;return true;} else {return false;}}} finally {

pred.unlock();curr.unlock();

}}}

Page 378: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 181

try {pred.lock(); curr.lock();if (validate(pred,curr) {if (curr.item == item) {pred.next = curr.next;return true;} else {return false;}}} finally {

pred.unlock();curr.unlock();

}}}

Remove Method

Always unlock

Page 379: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 182

try {pred.lock(); curr.lock();if (validate(pred,curr) {if (curr.item == item) {pred.next = curr.next;return true;} else {return false;}}} finally {

pred.unlock();curr.unlock();

}}}

Remove Method

Lock both nodes

Page 380: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 183

try {pred.lock(); curr.lock();if (validate(pred,curr) {if (curr.item == item) {pred.next = curr.next;return true;} else {return false;}}} finally {

pred.unlock();curr.unlock();

}}}

Remove Method

Check for synchronization conflicts

Page 381: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 184

try {pred.lock(); curr.lock();if (validate(pred,curr) {if (curr.item == item) {pred.next = curr.next;return true;} else {return false;}}} finally {

pred.unlock();curr.unlock();

}}}

Remove Method

target found, remove node

Page 382: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 185

try {pred.lock(); curr.lock();if (validate(pred,curr) {if (curr.item == item) {pred.next = curr.next;return true;} else {return false;}}} finally {

pred.unlock();curr.unlock();

}}}

Remove Method

target not found

Page 383: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Code review:

Add:

public boolean add(T item) {

int key = item.hashCode();

while (true) {

Entry pred = this.head;

Entry curr = pred.next;

while (curr.key <= key) {

pred = curr; curr = curr.next;

}

pred.lock(); curr.lock();

Search the list from the beginning each

time, until validation succeeds

try {

if (validate(pred, curr)) {

if (curr.key == key) {

return false;

} else {

Entry entry = new Entry(item);

entry.next = curr;

pred.next = entry;

return true;

}

}

} finally {

pred.unlock(); curr.unlock();

}

}

}

If validation succeeds Attempt Add

Continued:

3. Optimistic

Page 384: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Code review:

Contains:

public boolean contains(T item) {

int key = item.hashCode();

while (true) {

Entry pred = this.head;

Entry curr = pred.next;

while (curr.key < key) {

pred = curr; curr = curr.next;

}

try {

pred.lock(); curr.lock();

if (validate(pred, curr)) {

return (curr.key == key);

}

} finally {

pred.unlock(); curr.unlock();

}

}

}

Search the list from the beginning each

time, until validation succeeds

If validation succeeds Return the result

3. Optimistic

Page 385: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Methods properties:

• Assuming fair scheduler. Even if all the lock

implementations are Starvation free. We will show a scenario in which the methods Remove / Add / Contains do not return.

• And so our implementation won’t be starvation free.

3. Optimistic

Page 386: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Methods properties:

• Assuming Thread A operation is Remove(d) /

Add(c) / Contains(c).

• If the following sequence of operations will happen:

3. Optimistic

d e a b

Page 387: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Methods properties:

The sequence:

• 1. Thread A will find b.

• 2. Thread B will remove b.

• 3. The validation of thread A will fail.

3. Optimistic

The thread call to the function won’t return!

d e a b

• 4. Thread C will add b.

now go to 1.

Page 388: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

5. Advantages / Disadvantages:

Advantages:

- Limited hot-spots

• Targets of add(), remove(), contains().

• No contention on traversals.

- Much less lock acquisition/releases.

– Better concurrency.

Disadvantages:

- Need to traverse list twice!

- Contains() method acquires locks.

3.Optimistic

Page 389: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

5. Advantages / Disadvantages:

3.Optimistic

• Optimistic is effective if:

– The cost of scanning twice without locks is less than the cost of scanning once with locks

• Drawback:

– Contains() acquires locks. Normally, about 90% of the calls are contains.

Page 390: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

6. Running times:

Speed up

0

5

10

15

20

25

30

4 8 12 16 20 24 28 32

No. Threads

Seco

nd

s 2000 maxitems in list

20000 maxitems in list

3. Optimistic

Page 391: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

6. Running times:

Speed up

max of 2000 items

0

5

10

15

20

25

4 8 12 16 20 24 28 32

No. Threads

Seco

nd

s Fine List

CoarseGrained

Optimistic

3. Optimistic

Page 392: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

6. Running times:

Speed up

max of 20000 items

0

10

20

30

40

50

4 8 12 16 20 24 28 32

No. Threads

Seco

nd

s Fine List

CoarseGrained

Optimistic

3. Optimistic

Page 393: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Lazy

1. Describing the algorithm:

Validate:

– Pred is not marked as deleted.

– Curr is not marked as deleted.

– Pred points to curr.

Page 394: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Lazy

1. Describing the algorithm:

Remove(x):

• Find the node to remove.

• Lock pred and curr.

• Validate. (New validation!)

• Logical delete

– Marks current node as removed (new!).

• Physical delete

– Redirects predecessor’s next.

Page 395: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Lazy

1. Describing the algorithm:

Add(x):

• Find the node to remove.

• Lock pred and curr.

• Validate. (New validation!)

• Physical add

– The same as Optimistic.

Page 396: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Lazy

1. Describing the algorithm:

Contains(x):

• Find the node to remove without locking!

• Return true if found the node and it isn’t marked as deleted.

• No locks!

Page 397: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a a b c d

4. Lazy

1. Describing the algorithm:

• Remove(c):

Page 398: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

c a a b d

Present in list

4. Lazy

1. Describing the algorithm:

• Remove(c):

1. Find the node

Page 399: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

c a a b d

Present in list

4. Lazy

1. Describing the algorithm:

• Remove(c):

2. lock

Page 400: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

c a a b d

Present in list

4. Lazy

1. Describing the algorithm:

• Remove(c):

3. Validate

Page 401: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

c a a b d

Set as marked

4. Lazy

1. Describing the algorithm:

• Remove(c):

4. Logically delete

Page 402: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a a b c d

Pred.next = curr.next

4. Lazy

1. Describing the algorithm:

• Remove(c):

5. Physically delete

Page 403: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

a a b d

Cleaned

4. Lazy

1. Describing the algorithm:

• Remove(c):

5. Physically delete

Page 404: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Lazy

1. Describing the algorithm:

Given the Lazy Synchronization algorithm.

What else should we change?

Page 405: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Lazy

1. Describing the algorithm:

• New Abstraction map!

• S(head) =

– { x | there exists node a such that

• a reachable from head and

• a.item = x and

• a is unmarked

– }

Page 406: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Why do we need to Validate?

2. Explaining why every step is needed.

4. Lazy

Page 407: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• First: Why do we need to validate that pred Still points to curr?

• The same as in Optimistic:

• Thread A removes(d).

• Then thread A found b, before A locks. Another thread adds(c). – c and d will be removed instead of just d.

2. Explaining why every step is needed.

4. Lazy

b d e a

Page 408: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

• Second: Why do we need to validate that pred and curr aren’t marked logically removed?

• To make sure a thread hasn’t removed them between our find and our lock.

• The same scenario we showed for validating that pred is still accessible from head holds here: – After thread A found b, before A locks. Another

thread removes b. (our operation won’t take place).

2. Explaining why every step is needed.

4. Lazy

d e b a

Page 409: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Code review:

Add:

public boolean add(T item) {

int key = item.hashCode();

while (true) {

Node pred = this.head;

Node curr = head.next;

while (curr.key < key) {

pred = curr; curr = curr.next;

}

pred.lock();

try {

curr.lock();

Search the list from the beginning each

time, until validation succeeds

try {

if (validate(pred, curr)) {

if (curr.key == key) {

return false;

} else {

Node Node = new Node(item);

Node.next = curr;

pred.next = Node;

return true;

}

}

} finally {

curr.unlock();

}

} finally {

pred.unlock();

}

}

}

If validation succeeds Attempt Add

Continued:

4. Lazy

Page 410: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Code review:

Remove:

public boolean remove(T item) {

int key = item.hashCode();

while (true) {

Node pred = this.head;

Node curr = head.next;

while (curr.key < key) {

pred = curr; curr = curr.next;

}

pred.lock();

try {

curr.lock();

try {

Search the list from the beginning each

time, until validation succeeds

try{

if (validate(pred, curr)) {

if (curr.key != key) {

return false;

} else {

curr.marked = true;

pred.next = curr.next;

return true;

}

}

} finally {

curr.unlock();

}

} finally {

pred.unlock();

}

}

}

Validation

Continued:

4. Lazy

Logically remove

Physically remove

Page 411: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

3. Code review:

Contains:

public boolean contains(T item) {

int key = item.hashCode();

Node curr = this.head;

while (curr.key < key)

curr = curr.next;

return curr.key == key && !curr.marked;

}

Check if its there and not marked

4. Lazy

No Lock!

Page 412: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Methods properties:

Remove and Add:

• Assuming fair scheduler. Even if all the lock implementations are Starvation free. The same scenario we showed for optimistic holds here.

• (only here the validation will fail because the node will be marked and not because it can’t be reached from head)

• And so our implementation won’t be starvation free.

4. Lazy

Page 413: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

4. Methods properties:

But… Contains:

• Contains does not lock!

• In fact it isn’t dependent on other threads to work.

• And so… Contains is Wait-free.

• Do notice that other threads can’t increase the list forever while the thread is in contains because we have a maximum size to the list (<tail).

4. Lazy

Page 414: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

5. Advantages / Disadvantages:

• Advantages:

– Contains is Wait-free. Usually 90% of the calls!

– Validation doesn’t rescan the list.

• Drawbacks:

– Failure to validate restarts the function call.

– Add and Remove use locks.

Lock-free implementation

4. Lazy

Page 415: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

6. Running times:

Speed up

0

2

4

6

8

10

4 8 12 16 20 24 28 32

No. Threads

Seco

nd

s 2000 maxitems in list

20000 maxitems in list

4. Lazy

Page 416: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

6. Running times:

Speed up

max of 2000 items

0

5

10

15

20

25

4 8 12 16 20 24 28 32

No. Threads

Seco

nd

s

Fine List

CoarseGrained

Optimistic

Lazy

4. Lazy

Page 417: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

6. Running times:

Speed up

max of 20000 items

0

10

20

30

40

50

4 8 12 16 20 24 28 32

No. Threads

Seco

nd

s

Fine List

CoarseGrained

Optimistic

Lazy

4. Lazy

Page 418: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Optimistic lock-free Concurrency

Pessimistic Optimistic

lock x;

x++;

unlock x;

int t;

do {

t = x;

} while (!CAS(&x, t, t+1))

CAS(&x,a,b) = if *x = a then *x = b return true else return false

Page 419: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 227

Reminder: Lock-Free Data Structures

• No matter what …– Guarantees minimal progress in any

execution– i.e. Some thread will always complete a

method call– Even if others halt at malicious times– Implies that implementation can’t use locks

Page 420: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 228

Lock-free Lists

• Next logical step– Wait-free contains()– lock-free add() and remove()

• Use only compareAndSet()– What could go wrong?

Page 421: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 229

a 0 0 0a b c 0e1c

Logical Removal

Physical RemovalUse CAS to verify pointer is correct

Not enough!

Lock-free Lists

Page 422: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 230

Problem…

a 0 0 0a b c 0e1c

Logical Removal

Physical Removal0d

Node added

Page 423: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 231

The Solution: Combine Bit and Pointer

a 0 0 0a b c 0e1c

Logical Removal =Set Mark Bit

PhysicalRemovalCAS

0d

Mark-Bit and Pointerare CASed together(AtomicMarkableReference)

Fail CAS: Node not added after logical Removal

Page 424: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 232

Solution

• Use AtomicMarkableReference• Atomically

– Swing reference and– Update flag

• Remove in two steps– Set mark bit in next field– Redirect predecessor’s pointer

Page 425: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 233

Marking a Node

• AtomicMarkableReference class– Java.util.concurrent.atomic package

address F

mark bit

Reference

Page 426: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 234

Extracting Reference & Mark

Public Object get(boolean[] marked);

Page 427: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 235

Extracting Reference & Mark

Public Object get(boolean[] marked);

Returns reference

Returns mark at array index 0!

Page 428: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 236

Extracting Mark Only

public boolean isMarked();

Value of mark

Page 429: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 237

Changing State

Public boolean compareAndSet( Object expectedRef,Object updateRef,boolean expectedMark,boolean updateMark);

Page 430: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 238

Changing State

Public boolean compareAndSet(Object expectedRef,Object updateRef,boolean expectedMark,boolean updateMark);

If this is the current reference …

And this is the current mark …

Page 431: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 239

Changing State

Public boolean compareAndSet( Object expectedRef,Object updateRef,boolean expectedMark,boolean updateMark);

…then change to this new reference …

… and this new mark

Page 432: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 240

Changing State

public boolean attemptMark( Object expectedRef,boolean updateMark);

Page 433: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 241

Changing State

public boolean attemptMark(Object expectedRef,boolean updateMark);

If this is the current reference …

Page 434: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 242

Changing State

public boolean attemptMark( Object expectedRef,boolean updateMark);

.. then change to this new mark.

Page 435: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

bCAS

Art of Multiprocessor Programming 243

Removing a Node

a c d

remove c

Page 436: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 244

Removing a Node

a b d

remove b

remove c

c

failed

CAS CAS

Page 437: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 245

Removing a Node

a b d

remove b

remove c

c

Page 438: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 246

Removing a Node

a d

remove b

remove c

Page 439: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 247

Traversing the List

• Q: what do you do when you find a “logically” deleted node in your path?

• A: finish the job.– CAS the predecessor’s next field– Proceed (repeat as needed)

Page 440: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 248

Lock-Free Traversal(only Add and Remove)

a b c dCAS

Uh-oh

pred currpred curr

Page 441: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 249

The Window Class

class Window {public Node pred;public Node curr;Window(Node pred, Node curr) {this.pred = pred; this.curr = curr;

}}

Page 442: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 250

The Window Class

class Window {public Node pred;public Node curr;Window(Node pred, Node curr) {this.pred = pred; this.curr = curr;

}}

A container for pred and current values

Page 443: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 251

Using the Find Method

Window window = find(head, key);Node pred = window.pred;curr = window.curr;

Page 444: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 252

Using the Find Method

Window window = find(head, key);Node pred = window.pred;curr = window.curr;

Find returns window

Page 445: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 253

Using the Find Method

Window window = find(head, key);Node pred = window.pred;curr = window.curr;

Extract pred and curr

Page 446: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming© Herlihy-Shavit 2007

254

The Find Method

Window window = find(item);

At some instant,

pred curr succ

item or …

Page 447: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming© Herlihy-Shavit 2007

255

The Find Method

Window window = find(item);

At some instant,

predcurr= null

succ

item not in list

Page 448: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 256

Removepublic boolean remove(T item) {Boolean snip; while (true) {Window window = find(head, key);Node pred = window.pred, curr = window.curr;if (curr.key != key) {

return false;} else {Node succ = curr.next.getReference();snip = curr.next.compareAndSet(succ, succ, false

true);if (!snip) continue;pred.next.compareAndSet(curr, succ, false, false);

return true;}}}

Page 449: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 257

Removepublic boolean remove(T item) {Boolean snip; while (true) {Window window = find(head, key);Node pred = window.pred, curr = window.curr;if (curr.key != key) {

return false;} else {Node succ = curr.next.getReference();snip = curr.next.compareAndSet (succ, succ, false,

true);if (!snip) continue;pred.next.compareAndSet(curr, succ, false, false);

return true;}}} Keep trying

Page 450: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 258

Removepublic boolean remove(T item) {Boolean snip; while (true) {Window window = find(head, key);Node pred = window.pred, curr = window.curr;if (curr.key != key) {

return false;} else {Node succ = curr.next.getReference();snip = curr.next.compareAndSet (succ, succ, false,

true);if (!snip) continue;pred.next.compareAndSet(curr, succ, false, false);

return true;}}} Find neighbors

Page 451: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 259

Removepublic boolean remove(T item) {Boolean snip; while (true) {Window window = find(head, key);Node pred = window.pred, curr = window.curr;if (curr.key != key) {

return false;} else {Node succ = curr.next.getReference();snip = curr.next.compareAndSet(succ, succ, false,

true);if (!snip) continue;pred.next.compareAndSet(curr, succ, false, false);

return true;}}} She’s not there …

Page 452: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 260

Removepublic boolean remove(T item) {Boolean snip; while (true) {Window window = find(head, key);Node pred = window.pred, curr = window.curr;if (curr.key != key) {

return false;} else {Node succ = curr.next.getReference();snip = curr.next.compareAndSet(succ, succ, false,

true);if (!snip) continue;pred.next.compareAndSet(curr, succ, false, false);

return true;}}}

Try to mark node as deleted

Page 453: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 261

Removepublic boolean remove(T item) {Boolean snip; while (true) {Window window = find(head, key);Node pred = window.pred, curr = window.curr;if (curr.key != key) {

return false;} else {Node succ = curr.next.getReference();snip = curr.next.compareAndSet(succ, succ, false,

true);if (!snip) continue;pred.next.compareAndSet(curr, succ, false, false);

return true;}}}

If it doesn’t work, just retry, if it

does, job essentially done

Page 454: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 262

Removepublic boolean remove(T item) {Boolean snip; while (true) {Window window = find(head, key);Node pred = window.pred, curr = window.curr;if (curr.key != key) {

return false;} else {Node succ = curr.next.getReference();snip = curr.next.compareAndSet(succ, succ, false,

true);if (!snip) continue;pred.next.compareAndSet(curr, succ, false, false);

return true;}}}

Try to advance reference(if we don’t succeed, someone else did or will).

a

Page 455: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 263

Addpublic boolean add(T item) {boolean splice;while (true) {

Window window = find(head, key);Node pred = window.pred, curr = window.curr;if (curr.key == key) {

return false;} else {Node node = new Node(item);node.next = new AtomicMarkableRef(curr, false);if (pred.next.compareAndSet(curr, node, false,

false)) {return true;}}}}

Page 456: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 264

Addpublic boolean add(T item) {boolean splice;while (true) {

Window window = find(head, key);Node pred = window.pred, curr = window.curr;if (curr.key == key) {

return false;} else {Node node = new Node(item);node.next = new AtomicMarkableRef(curr, false);if (pred.next.compareAndSet(curr, node, false,

false)) {return true;}}}} Item already there.

Page 457: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 265

Addpublic boolean add(T item) {boolean splice;while (true) {

Window window = find(head, key);Node pred = window.pred, curr = window.curr;if (curr.key == key) {

return false;} else {Node node = new Node(item);node.next = new AtomicMarkableRef(curr, false);if (pred.next.compareAndSet(curr, node, false,

false)) {return true;}}}}

create new node

Page 458: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 266

Addpublic boolean add(T item) {boolean splice;while (true) {

Window window = find(head, key);Node pred = window.pred, curr = window.curr;if (curr.key == key) {

return false;} else {Node node = new Node(item);node.next = new AtomicMarkableRef(curr, false);if (pred.next.compareAndSet(curr, node, false,

false)) {return true;}}}}

Install new node, else retry loop

Page 459: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 267

Wait-free Contains

public boolean contains(T item) {boolean marked; int key = item.hashCode();Node curr = this.head;while (curr.key < key)

curr = curr.next;Node succ = curr.next.get(marked);return (curr.key == key && !marked[0])

}

Page 460: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 268

Wait-free Contains

public boolean contains(T item) {boolean marked; int key = item.hashCode();Node curr = this.head;while (curr.key < key)

curr = curr.next;Node succ = curr.next.get(marked);return (curr.key == key && !marked[0])

}

Only diff is that we get and check

marked

Page 461: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 269

Lock-free Findpublic Window find(Node head, int key) {Node pred = null, curr = null, succ = null;boolean[] marked = {false}; boolean snip;retry: while (true) {

pred = head;curr = pred.next.getReference(); while (true) {succ = curr.next.get(marked); while (marked[0]) {…}if (curr.key >= key)

return new Window(pred, curr);pred = curr;curr = succ;

}}}

Page 462: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 270

Lock-free Findpublic Window find(Node head, int key) {Node pred = null, curr = null, succ = null;boolean[] marked = {false}; boolean snip;retry: while (true) {

pred = head;curr = pred.next.getReference(); while (true) {succ = curr.next.get(marked); while (marked[0]) {…}if (curr.key >= key)

return new Window(pred, curr);pred = curr;curr = succ;

}}}

If list changes while traversed,

start over

Page 463: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 271

public Window find(Node head, int key) {Node pred = null, curr = null, succ = null;boolean[] marked = {false}; boolean snip;retry: while (true) {

pred = head;curr = pred.next.getReference();while (true) {succ = curr.next.get(marked); while (marked[0]) {…}if (curr.key >= key)

return new Window(pred, curr);pred = curr;curr = succ;

}}}

Lock-free FindStart looking from head

Page 464: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 272

public Window find(Node head, int key) {Node pred = null, curr = null, succ = null;boolean[] marked = {false}; boolean snip;retry: while (true) {

pred = head;curr = pred.next.getReference(); while (true) {succ = curr.next.get(marked); while (marked[0]) {…}if (curr.key >= key)

return new Window(pred, curr);pred = curr;curr = succ;

}}}

Lock-free Find

Move down the list

Page 465: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 273

public Window find(Node head, int key) {Node pred = null, curr = null, succ = null;boolean[] marked = {false}; boolean snip;retry: while (true) {

pred = head;curr = pred.next.getReference(); while (true) {succ = curr.next.get(marked);while (marked[0]) {…}if (curr.key >= key)

return new Window(pred, curr);pred = curr;curr = succ;

}}}

Lock-free Find

Get ref to successor and current deleted bit

Page 466: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 274

public Window find(Node head, int key) {Node pred = null, curr = null, succ = null;boolean[] marked = {false}; boolean snip;retry: while (true) {

pred = head;curr = pred.next.getReference(); while (true) {succ = curr.next.get(marked); while (marked[0]) {…}if (curr.key >= key)

return new Window(pred, curr);pred = curr;curr = succ;

}}}

Lock-free Find

Try to remove deleted nodes in path…code details soon

Page 467: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 275

public Window find(Node head, int key) {Node pred = null, curr = null, succ = null;boolean[] marked = {false}; boolean snip;retry: while (true) {

pred = head;curr = pred.next.getReference(); while (true) {succ = curr.next.get(marked); while (marked[0]) {…}if (curr.key >= key)

return new Window(pred, curr);pred = curr;curr = succ;

}}}

Lock-free Find

If curr key that is greater or equal, return pred and curr

Page 468: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 276

public Window find(Node head, int key) {Node pred = null, curr = null, succ = null;boolean[] marked = {false}; boolean snip;retry: while (true) {

pred = head;curr = pred.next.getReference(); while (true) {succ = curr.next.get(marked); while (marked[0]) {…}if (curr.key >= key)

return new Window(pred, curr);pred = curr;curr = succ;

}}}

Lock-free Find

Otherwise advance window and loop again

Page 469: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 277

Lock-free Find

retry: while (true) {…while (marked[0]) {

snip = pred.next.compareAndSet(curr, succ, false, false);

if (!snip) continue retry;curr = succ; succ = curr.next.get(marked);

}…

Page 470: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 278

Lock-free Find

retry: while (true) {…while (marked[0]) {

snip = pred.next.compareAndSet(curr, succ, false, false);

if (!snip) continue retry;curr = succ;succ = curr.next.get(marked);

}…

Try to snip out node

Page 471: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 279

Lock-free Find

retry: while (true) {…while (marked[0]) {

snip = pred.next.compareAndSet(curr, succ, false, false);

if (!snip) continue retry;curr = succ; succ = curr.next.get(marked);

}…

if predecessor’s next field changed, retry whole traversal

Page 472: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 280

Lock-free Find

retry: while (true) {…while (marked[0]) {

snip = pred.next.compareAndSet(curr, succ, false, false);

if (!snip) continue retry;curr = succ; succ = curr.next.get(marked);

}…

Otherwise move on to check if next node deleted

Page 473: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Performance

• Different list-based set implementaions• 16-node machine• Vary percentage of contains() calls

Art of Multiprocessor Programming 281

Page 474: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 282

High Contains Ratio

Lock-free Lazy list

Coarse GrainedFine Lock-coupling

Page 475: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 283

Low Contains Ratio

Lock-free

Lazy list

Coarse GrainedFine Lock-coupling

Page 476: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 284

As Contains Ratio Increases

Lock-free Lazy list

Coarse GrainedFine Lock-coupling

% Contains()

Page 477: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 285

Summary

• Coarse-grained locking• Fine-grained locking• Optimistic synchronization• Lazy synchronization• Lock-free synchronization

Page 478: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Art of Multiprocessor Programming 286

“To Lock or Not to Lock”

• Locking vs. Non-blocking:– Extremist views on both sides

• The answer: nobler to compromise– Example: Lazy list combines blocking add()

and remove()and a wait-free contains()– Remember: Blocking/non-blocking is a property

of a method

Page 479: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

An Optimistic Lock-free Stack

pop( ){

1 local done, next, t;

2 done = false;

3 while (!done) {

4 t = Top;

5 if (t==null) return null;

6 next = t.Next;

7 done = CAS(&Top, t, next);

8 }

9 return t;

push(x){

10 local done, t;

11 done = false;

12 while(!done) {

13 t = Top;

14 x.Next = t;

15 done = CAS(&Top, t, x);

16 }

17 return true; Bug#1: t might be a dangling pointer

Bug#2: ABA problem leads to corrupted stacks

… n Nex

t n Next

Top

Page 480: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

ABA Problem Threads T1 and T2 are interleaved as follows:

A

C

B

Top

t

next

B

C next (removed)

Top

A

B

t

Top

C next (removed)

Timeline

T1:

pop()

{

t = Top

next = t.Next

interrupted

resumes

CAS(&Top,t,next)

succeeds

stack corrupted

T2:

a = pop();

c = pop();

push(a);

Page 481: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Summary

Our winner: Optimistic Lock-free.

Second best: Lazy.

Third: Optimistic.

Fourth: Fine-Grained.

Last: Coarse-Grained.

?

Page 482: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Summary

Answer: No.

Choose your implementation carefully based on your requirements.

Page 483: Introduction to Concurrencystaff.ustc.edu.cn/.../lectureNotes/10_Concurrency_Lin.pdf · 2017-05-09 · Introduction to Concurrency and Multicore Programming . Slides adapted from

Summary

• Concurrent programming is hard.

• Concurrency is error-prone.

• Formal method is necessary.