hoare-style specifications as correctness conditions for non ... · amdahl’s law character-izes...

64
Hoare-style Specifications as Correctness Conditions for Non-Linearizable Concurrent Objects Ilya Sergey Aleks Nanevski Anindya Banerjee Germán Andrés Delbianco OOPSLA 2016 November 2, 2016 C o n s is t e nt * Co m p l e t e *W e l l D o c u m e n t e d * E as y t o R e u s e * * E v a l u a t e d * OO P S L A * A rt if act * A E C

Upload: others

Post on 20-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Hoare-style Specifications as Correctness Conditions

for Non-Linearizable Concurrent Objects

Ilya SergeyAleks Nanevski

Anindya Banerjee Germán Andrés Delbianco

biennial report2008-09

madrid institute for advanced studies

memoria software v3.qxd 13/8/10 10:48 Página 1

OOPSLA 2016 November 2, 2016

Consist

ent *Complete *

Well D

ocumented*Easyt

oR

euse* *

Evaluated*

OOPSLA*

Artifact *

AEC

Page 2: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Linearizable Concurrent Objects

Nоn-overlapping calls to methods of a concurrent object should appear to take effect in their sequential order.

Linearizability: A Correctness Condition for Concurrent Objects MAURICE P. HERLIHY and JEANNETTE M. WING Carnegie Mellon University

A concurrent object is a data object shared by concurrent processes. Linearizability is a correctness condition for concurrent objects that exploits the semantics of abstract data types. It permits a high degree of concurrency, yet it permits programmers to specify and reason about concurrent objects using known techniques from the sequential domain. Linearizability provides the illusion that each operation applied by concurrent processes takes effect instantaneously at some point between its invocation and its response, implying that the meaning of a concurrent object’s operations can be given by pre- and post-conditions. This paper defines linearizability, compares it to other correctness conditions, presents and demonstrates a method for proving the correctness of implementations, and shows how to reason about concurrent objects, given they are linearizable.

Categories and Subject Descriptors: D.1.3 [Programming Techniques]: Concurrent Programming; D.2.1 [Software Engineering]: Requirements/Specifications; D.3.3 [Programming Lan- guages]: Language Constructs--abstract data types, concurrent programming structures, data types and structures; F.1.2 [Computation by Abstract Devices]: Modes of Computation-parallelism; F.3.1 [Logics and Meanings of Programs]: Specifying and Verifying and Reasoning about Programs-pre- and post-conditions, specification techniques

General Terms: Theory, Verification

Additional Key Words and Phrases: Concurrrency, correctness, Larch, linearizability, multi- processing, serializability, shared memory, specification

1. INTRODUCTION

1 .l Overview Informally, a concurrent system consists of a collection of sequential processes that communicate through shared typed objects. This model encompasses both message-passing architectures in which the shared objects are message queues,

A preliminary version of this paper appeared in the Proceedings of the 14th ACM Symposium on Principles of Programming Languages, January 1987 [21]. This research was sponsored by IBM and the Defense Advanced Research Projects Agents (DOD), ARPA order 4976 (Amendment 20), under contract F33615-87-C-1499, monitored by the Avionics Laboratory, Air Force Wright Aeronautical Laboratories, Wright-Patterson AFB. Additional spport for J. M. Wing was provided in part by the National Science Foundation under grant CCR-8620027. The views and conclusions contained in this document are those of the authors and should not be interpreted as representing the official policies, either expressed or implied, of the Defense Advanced Research Projects Agency or the US Government. Authors’ address: Department of Computer Science, Carnegie Mellon University, Pittsburgh, PA 15213-3890. Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. 0 1990 ACM 0164-0925/90/0700-0463 $01.50

ACM Transactions on Programming Languages and Systems, Vol. 12, No. 3, July 1990, Pages 463-492.

Page 3: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Linearizability is expensive

Laws of Order: Expensive Synchronization inConcurrent Algorithms Cannot be Eliminated

Hagit AttiyaTechnion

[email protected]

Rachid GuerraouiEPFL

[email protected]

Danny HendlerBen-Gurion University

[email protected]

Petr KuznetsovTU Berlin/Deutsche Telekom Labs

[email protected]

Maged M. MichaelIBM T. J. Watson Research Center

[email protected]

Martin VechevIBM T. J. Watson Research Center

[email protected]

AbstractBuilding correct and efficient concurrent algorithms is known tobe a difficult problem of fundamental importance. To achieve ef-ficiency, designers try to remove unnecessary and costly synchro-nization. However, not only is this manual trial-and-error processad-hoc, time consuming and error-prone, but it often leaves design-ers pondering the question of: is it inherently impossible to elimi-nate certain synchronization, or is it that I was unable to eliminateit on this attempt and I should keep trying?

In this paper we respond to this question. We prove that it is im-possible to build concurrent implementations of classic and ubiqui-tous specifications such as sets, queues, stacks, mutual exclusionand read-modify-write operations, that completely eliminate theuse of expensive synchronization.

We prove that one cannot avoid the use of either: i) read-after-write (RAW), where a write to shared variable A is followed by aread to a different shared variable B without a write to B in between,or ii) atomic write-after-read (AWAR), where an atomic operationreads and then writes to shared locations. Unfortunately, enforcingRAW or AWAR is expensive on all current mainstream processors.To enforce RAW, memory ordering–also called fence or barrier–instructions must be used. To enforce AWAR, atomic instructionssuch as compare-and-swap are required. However, these instruc-tions are typically substantially slower than regular instructions.

Although algorithm designers frequently struggle to avoid RAWand AWAR, their attempts are often futile. Our result characterizesthe cases where avoiding RAW and AWAR is impossible. On theflip side, our result can be used to guide designers towards newalgorithms where RAW and AWAR can be eliminated.

Categories and Subject Descriptors D.1.3 [Concurrent Pro-gramming]; E.1 [Data]: Data Structures

General Terms Algorithms, TheoryKeywords Concurrency, Algorithms, Lower Bounds, MemoryFences, Memory Barriers

Permission to make digital or hard copies of all or part of this work for personal orclassroom use is granted without fee provided that copies are not made or distributedfor profit or commercial advantage and that copies bear this notice and the full citationon the first page. To copy otherwise, to republish, to post on servers or to redistributeto lists, requires prior specific permission and/or a fee.POPL’11, January 26–28, 2011, Austin, Texas, USA.Copyright c⃝ 2011 ACM 978-1-4503-0490-0/11/01. . . $10.00

1. IntroductionThe design of concurrent applications that avoid costly synchro-nization patterns is a cardinal programming challenge, requiringconsideration of algorithmic concerns and architectural issues withimplications to formal testing and verification.

Two common synchronization patterns that frequently arise inthe design of concurrent algorithms are read after write (RAW) andatomic write after read (AWAR).

The RAW pattern consists of a process writing to some sharedvariable A, followed by the same process reading a different sharedvariable B, without that process writing to B in between. TheAWAR pattern consists of a process reading some shared variablefollowed by the process writing to a shared variable (the write couldbe to the same shared variable as the read), where the entire read-write sequence is atomic. Examples of the AWAR pattern includeread-modify-write operations such as a Compare-and-Swap [26](CAS).

Unfortunately, on all mainstream processor architectures, theRAW and AWAR patterns are associated with expensive instruc-tions. Modern processor architectures use relaxed memory mod-els, where guaranteeing RAW order among accesses to indepen-dent memory locations requires the execution of memory order-ing instructions–often called memory fences or memory barriers–that enforce RAW order.1 Guaranteeing the atomicity of AWARrequires the use of atomic instructions. Typically, fence and atomicinstructions are substantially slower than regular instructions, evenunder the most favorable caching conditions.

Due to these high overheads, designers of concurrent algorithmsaim to avoid both RAW and AWAR patterns. However, such at-tempts are often unsuccessful: in many cases, even after multipleattempts, it turns out impossible to avoid these patterns while en-suring correctness of the algorithm.

This raises an interesting and important practical question:

Can we discover and formalize the conditions under whichavoiding RAW and AWAR, while ensuring correctness, is futile?

In this paper, we answer this question formally. We show thatimplementations of a wide class of concurrent algorithms mustinvolve RAW or AWAR. In particular, we focus on two widely used

1 RAW order requires the use of explicit fences or atomic instructionseven on strongly ordered architectures (e.g., X86 and SPARC TSO) thatautomatically guarantee other types of ordering (read after read, write afterread, and write after write).

Page 4: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Enabling better parallelism

76 COMMUNICATIONS OF THE ACM | MARCH 2011 | VOL. 54 | NO. 3

review articles

problems with regular, slow-changing (or even static) communication and coordination patterns. Such problems arise in scientific computing or in graphics, but rarely in systems.

The future promises us multiple cores on anything from phones to lap-tops, desktops, and servers, and there-fore a plethora of applications char-acterized by complex, fast-changing interactions and data exchanges.

Why are these dynamic interactions and data exchanges a problem? The formula we need in order to answer this question is called Amdahl’s Law. It cap-tures the idea that the extent to which we can speed up any complex computa-tion is limited by how much of the com-putation must be executed sequentially.

Define the speedup S of a computa-tion to be the ratio between the time it takes one processor to complete the computation (as measured by a wall clock) versus the time it takes n concur-rent processors to complete the same computation. Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an application, where p is the fraction of the computation that can be executed in parallel. Assume, for sim-plicity, that it takes (normalized) time 1 for a single processor to complete the computation. With n concurrent pro-cessors, the parallel part takes time p/n, and the sequential part takes time 1− p. Overall, the parallelized computation takes time 1− p + pn . Amdahl’s Law says the speedup, that is, the ratio between

“M ULT IC OR E PROC ESSORS ARE about to revolutionize the way we design and use data structures.”

You might be skeptical of this statement; after all, are multicore processors not a new class of multiprocessor machines running parallel programs, just as we have been doing for more than a quarter of a century?

The answer is no. The revolution is partly due to changes multicore processors introduce to parallel architectures; but mostly it is the result of the change in the applications that are being parallelized: multicore processors are bringing parallelism to mainstream computing.

Before the introduction of multicore processors, parallelism was largely dedicated to computational

key insights We are experiencing a fundamental shift

in the properties required of concurrent data structures and of the algorithms at the core of their implementation.

The data structures of our childhood—stacks, queues, and heaps—will soon disappear, replaced by looser “unordered” concurrent constructs based on distribution and randomization.

Future software engineers will need to learn how to program using these novel structures, understanding their performance benefits and their fairness limitations.

Data Structures in the Multicore Age

DOI:10.1145/1897852.1897873

The advent of multicore processors as the standard computing platform will force major changes in software design.

BY NIR SHAVIT

IL

LU

ST

RA

TI

ON

BY

AN

DY

GI

LM

OR

E

Relaxing the correctness condition would allow one to implement concurrent data structures more efficiently, as they would be free of synchronization bottlenecks.

Page 5: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Alternatives to linearizability• Quiescent Consistency [Aspnes-al:JACM94]

• Quasi-Linearizability [Afek-al:OPODIS10]

• Quantitative Relaxation [Henzinger-al:POPL13]

• Quantitative Quiescent Consistency [Jagadeesan-Riely:ICALP14]

• Concurrency-Aware Linearizability [Hemed-Rinetzky:DISC15]

• Local Linearizability [Haas-al:CONCUR16]

• …

Page 6: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

• Composing different conditions (CAL, QC, QQC) in a single program, which uses multiple objects;

• Providing syntactic proof methods for establishing all these conditions (akin to linearization points);

• Employing these criteria for client-side reasoning (uniformity).

Challenges of diversity

Page 7: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Hoare-style Specifications as Correctness Conditions

for Non-Linearizable Concurrent Objects

OOPSLA 2016 November 2, 2016

Consist

ent *Complete *

Well D

ocumented*Easyt

oR

euse* *

Evaluated*

OOPSLA*

Artifact *

AEC

Ilya Sergey

biennial report2008-09

madrid institute for advanced studies

memoria software v3.qxd 13/8/10 10:48 Página 1

Aleks Nanevski Anindya Banerjee

Germán Andrés Delbianco

Page 8: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Hoare-style Specifications

{ P } e { Q } @ C

precondition postcondition concurrent invariantsand protocol

If the initial state satisfies P, then, after e terminates, the final state satisfies Q

(no matter the interference manifested by C).

Page 9: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Hoare-style Specifications

• Compositional — substitution principle;

• Syntactic proof method — inference rules;

• Uniform — reasoning about objects and their clients in the same proof system.

{ P } e { Q } @ C

Page 10: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Hoare-style Specifications

{ P } e { Q } @ C

• Compositional — substitution principle;

• Syntactic proof method — inference rules;

• Uniform — reasoning about objects and their clients in the same proof system.

Live

Rich

Two-sided

Page 11: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

This work: Hoare-style specs as CAL, QC, QQC

Concurrency-Aware Linearizability (CAL):Effects of some concurrent method calls should appear to happen simultaneously.

Quiescent Consistency (QC):Method calls separated by a period of no interference (quiescence) should appear to take effect in their order.

Quantitative Quiescent Consistency (QQC):The number of out-of-order method results is bounded by the number of interfering threads (with a constant factor).

This talk

Page 12: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Simple Counting Network

def getAndInc() : nat

Page 13: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

= { n ← &x; b ← CAS(x, n, n + 1); if b then return n; else getAndInc();}

def getAndInc() : nat

high contention location

Simple Counting Network

Page 14: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Simple Counting Network

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

b

baln0

n1

x

x+1

0

1

Page 15: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Sequential Execution (T1)

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

0

bal0

1

x

x+1

Page 16: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

1

bal0

1

x

x+1

0

T1.b1 = 0

Sequential Execution (T1)

Page 17: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

1

bal2

1

x

x+1

T1.res1 = 0T1.b1 = 0

Sequential Execution (T1)

Page 18: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

0

bal2

1

x

x+11

T1.res1 = 0T1.b1 = 0

T1.b2 = 1

Sequential Execution (T1)

Page 19: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

0

bal2

3

x

x+1

T1.res1 = 0

T1.res2 = 1

T1.b1 = 0

T1.b2 = 1

Sequential Execution (T1)

Page 20: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Concurrent Execution (T1, T2)

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

0

bal0

1

x

x+1

Page 21: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

1

bal0

1

x

x+1

0

T1.b1 = 0

Concurrent Execution (T1, T2)

Page 22: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

0

bal0

1

x

x+11

T1.b1 = 0T2.b1 = 1

Concurrent Execution (T1, T2)

Page 23: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

0

bal0

3

x

x+1T2.res1 = 1

T1.b1 = 0T2.b1 = 1

Concurrent Execution (T1, T2)

Page 24: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

1

bal0

3

x

x+1

T1.b1 = 0T2.b1 = 1T2.res1 = 1T2.b2 = 0

0

Concurrent Execution (T1, T2)

Page 25: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

def getAndInc() : nat = { b ← flip(bal); res ← fetchAndAdd2(x + b); return res;}

1

bal2

3

x

x+1

T1.b1 = 0T2.b1 = 1T2.res1 = 1T2.b2 = 0T2.res2 = 0

Concurrent Execution (T1, T2)

Page 26: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Correctness Conditions for Counting Network

• R0: calls to getAndInc() take effect in their sequential order

• R1: different calls return distinct results (strong concurrent counter)

• R2: two calls, separated by period of quiescence, take effect in their sequential order (QC)

• R3: results of two calls in the same thread are out of order by no more than 2 * (number of calls interfering with both) (QQC)

Page 27: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Observations about the Counting Network

• Every flip of the balancer grants thread a capability to add 2 to a counter (x or x+1);

• Each of the counters (x and x+1) changes continuously wrt. even/odd values

“Histories”

“Tokens”

Page 28: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Real and Auxiliary State• Hoare-style specs constrain state, auxiliary or real

• Real state — heap (pointers bal, x, x+1);

• Auxiliary state — any fictional splittable resource:

✦ Token sets (τ) — disjoint sets;

✦ Histories (χ) — partial maps with nat as domain.

Page 29: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Auxiliary State of the Network

0 2 4 6

1 3 5

history of the counter x

history of the counter x+1

z0

u1

y0v0

tokens of pending threads

1

current valueof the balancer

Tokens = pending updates

Histories = observed updates

Page 30: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

χ = { …, n ↦ ι, … }

Interference-capturing histories

“timestamp”, a value written to a counter x or x+1 (0, 1, 2, etc.)

Page 31: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Interference-capturing histories

sets of tokens, held by interfering threads at the moment the entry has been written

χ = { …, n ↦ ι, … }

Page 32: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Notation for Subjective Histories and Tokens

• χ, χ — histories, contributed by this and other threads;

• τ, τ — tokens, held by this and other threads

Page 33: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{ τ = ∅ }

Specification of getAndInc()

res ← getAndInc()

{∃ ι, τ′ = ∅, χ′ = χ ∪ (res + 2) ↦ ι, τ ⊆ τ′ ∪ spent(χ′\ χ), last(χ ∪ χ) < res + 2 + 2 | ι ∩ τ |}

Page 34: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Specification of getAndInc()no tokens held initially

by this thread{ τ = ∅ }

{∃ ι, τ′ = ∅, χ′ = χ ∪ (res + 2) ↦ ι, τ ⊆ τ′ ∪ spent(χ′\ χ), last(χ ∪ χ) < res + 2 + 2 | ι ∩ τ |}

res ← getAndInc()

Page 35: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{∃ ι, τ′ = ∅, χ′ = χ ∪ (res + 2) ↦ ι, τ ⊆ τ′ ∪ spent(χ′\ χ), last(χ ∪ χ) < res + 2 + 2 | ι ∩ τ |}

Specification of getAndInc()

Final tokensand self-history

{ τ = ∅ }res ← getAndInc()

Page 36: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Specification of getAndInc()

Tokens don’t go missing

res ← getAndInc()

{∃ ι, τ′ = ∅, χ′ = χ ∪ (res + 2) ↦ ι, τ ⊆ τ′ ∪ spent(χ′\ χ), last(χ ∪ χ) < res + 2 + 2 | ι ∩ τ |}

{ τ = ∅ }

Page 37: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Specification of getAndInc()

result + 2 is greater than any previous value

in the history (modulo past ∩ present interference)

res ← getAndInc()

{∃ ι, τ′ = ∅, χ′ = χ ∪ (res + 2) ↦ ι, τ ⊆ τ′ ∪ spent(χ′\ χ), last(χ ∪ χ) < res + 2 + 2 | ι ∩ τ |}

{ τ = ∅ }

Page 38: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

What this spec is good for?

Page 39: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

• R1: different calls return distinct results (strong concurrent counter)

• R2: two calls, separated by period of quiescence, take effect in their sequential order (QC)

• R3: results of two calls in the same thread are out of order by no more than 2 * (number of calls interfering with both) (QQC)

Each result corresponds to a fresh history entry

Implications of the spec for getAndInc

Page 40: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

• R1: different calls return distinct results (strong concurrent counter)

• R2: two calls, separated by period of quiescence, take effect in their sequential order (QC)

• R3: results of two calls in the same thread are out of order by no more than 2 * (number of calls interfering with both) (QQC)

Implications of the spec for getAndInc

Page 41: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Exercising Quiescent Consistency

(res1, -) ← (getAndInc() || e1); (res2, -) ← (getAndInc() || e2);

return (res1, res2);

“quiescent moment”

{ ¿ res1 < res2 ? }

Page 42: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Specification of interfering program

e1

{ τ = ∅, χ = ∅ }

{∃ η1, τ = ∅, χ = η1, τ ⊆ τ′∪ spent(χ′\χ) }

adds an arbitrary number of history entries

Page 43: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Spec for parallel composition

{ τ = ∅ }

{∃ ι, η1, τ = ∅, χ = χ ∪ η1 ∪ (res1+2) ↦ ι, τ ⊆ τ′ ∪ spent(χ′\χ), last(χ ∪ χ) < res1 + 2 + 2 | ι ∩ τ |}

(res1, -) ← getAndInc() || e1

Page 44: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Spec for parallel composition

{ τ = ∅ }

{∃ ι, η1, τ = ∅, χ = χ ∪ η1 ∪ (res1+2) ↦ ι, τ ⊆ τ′ ∪ spent(χ′\χ), last(χ ∪ χ) < res1 + 2 + 2 | ι ∩ τ |}

(res1, -) ← getAndInc() || e1

Page 45: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

(res2, -) ← getAndInc() || e2;

return (res1, res2);

(res1, -) ← getAndInc() || e1;

Page 46: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 47: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′⊆ τ′′ ∪ spent(χ′′\χ′), last(χ′′ ∪ χ′) < res2 + 2 + 2 | ι ∩ τ′ |}

Page 48: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′⊆ τ′′ ∪ spent(χ′′\χ′), last(χ′′ ∪ χ′) < res2 + 2 + 2 | ι ∩ τ′ |}

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 49: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′⊆ τ′′ ∪ spent(χ′′\χ′), last(χ′′ ∪ χ′) < res2 + 2 + 2 | ι ∩ τ′ |}

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 50: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′⊆ τ′′ ∪ spent(χ′′\χ′), last(χ′′ ∪ χ′) < res2 + 2 + 2 | ι ∩ τ′ |}

No more forked threadsat this point!

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 51: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′⊆ ∅ ∪ spent(∅), last(χ′′ ∪ χ′) < res2 + 2 + 2 | ι ∩ τ′ |}

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 52: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′= ∅, last(χ′′ ∪ χ′) < res2 + 2 + 2 | ι ∩ τ′ |}

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 53: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′= ∅, last(χ′′ ∪ χ′) < res2 + 2 + 2 | ι ∩ τ′ |}

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 54: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′= ∅, last(χ′′ ∪ χ′) < res2 + 2 + 2 | ι ∩ ∅ |}

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 55: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′= ∅, last(χ′′ ∪ χ′) < res2 + 2 + 2 | ∅ |}

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 56: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′= ∅, last(χ′′ ∪ χ′) < res2 + 2 }

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 57: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′= ∅, last(χ′′ ∪ χ′) < res2 + 2 }

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 58: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′= ∅, last(χ′′ ∪ χ′) < res2 + 2 }

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 59: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′= ∅, res1 + 2 < res2 + 2 }

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 60: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

{∃ η1,η2, ι, τ = ∅, χ′′ = χ ∪ η1 ∪ η2 ∪ (res1+2 ↦ -) ∪ (res2+2 ↦ -), τ′= ∅, res1 < res2 }

{ τ = ∅ }

{ ∃ η1, τ = ∅, χ′ = χ ∪ η1 ∪ (res1+2 ↦ -), χ ⊆ χ′ }

(res1, -) ← getAndInc() || e1;

(res2, -) ← getAndInc() || e2;

return (res1, res2);

Page 61: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

• R1: different calls return distinct results (strong concurrent counter)

• R2: two calls, separated by period of quiescence, take effect in their sequential order (QC)

• R3: results of two calls in the same thread are out of order by no more than 2 * (number of calls interfering with both) (QQC)

Implications of the spec for getAndInc

Page 62: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

Summary of the proof pattern

• Express interference that matters via auxiliary state — tokens;

• Capture past interference and results in auxiliary histories;

• Assume closed world to bound interference (quiescence).

Page 63: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

• Full formal specification of the counting network;

• Formal proofs of QC and QQC properties for the network;

• Discussion on applying the technique for QC-queues;

• Spec and verification of java.util.concurrent.Exchanger;

• Verification of an exchanger client in the spirit of concurrency-aware linearizability (CAL).

• Report on implementation in FCSL/Coq.

What’s in the paper

Consist

ent *Complete *

Well D

ocumented*Easyt

oR

euse* *

Evaluated

*

OOPSLA*

Artifact *

AEC

Page 64: Hoare-style Specifications as Correctness Conditions for Non ... · Amdahl’s Law character-izes the maximum speedup S that can be achieved by n processors collaborat-ing on an

To take away

• Compositional — substitution principle;

• Syntactic proof method — inference rules;

• Uniform — reasoning about objects and their clients in the same proof system.

Hoare-style Specificationsfor Non-linearizable Concurrent Objects

Thanks!

Good specification is in the eye of the beholder.