Skip to content

Commit 05acef7

Browse files
authored
Merge 2018-11 CWG Motion 2
P0668R5 Revising the C++ memory model Fixes #2395
2 parents 9060df4 + 9d123bc commit 05acef7

File tree

2 files changed

+100
-63
lines changed

2 files changed

+100
-63
lines changed

source/atomics.tex

Lines changed: 74 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -379,75 +379,90 @@
379379
release sequence headed by \placeholder{A}.
380380

381381
\pnum
382-
There shall be a single total order \placeholder{S} on all \tcode{memory_order::seq_cst}
383-
operations, consistent with the ``happens before'' order and modification orders for all
384-
affected locations, such that each \tcode{memory_order::seq_cst} operation
385-
\placeholder{B} that loads a
386-
value from an atomic object \placeholder{M}
387-
observes one of the following values:
388-
382+
An atomic operation \placeholder{A} on some atomic object \placeholder{M} is
383+
\defn{coherence-ordered before}
384+
another atomic operation \placeholder{B} on \placeholder{M} if
389385
\begin{itemize}
390-
\item the result of the last modification \placeholder{A} of \placeholder{M} that precedes
391-
\placeholder{B} in \placeholder{S}, if it exists, or
392-
393-
\item if \placeholder{A} exists, the result of some modification of \placeholder{M}
394-
that is not
395-
\tcode{memory_order::seq_cst} and that does not happen before \placeholder{A}, or
396-
397-
\item if \placeholder{A} does not exist, the result of some modification of \placeholder{M}
398-
that is not
399-
\tcode{memory_order::seq_cst}.
386+
\item \placeholder{A} is a modification, and
387+
\placeholder{B} reads the value stored by \placeholder{A}, or
388+
\item \placeholder{A} precedes \placeholder{B}
389+
in the modification order of \placeholder{M}, or
390+
\item \placeholder{A} and \placeholder{B} are not
391+
the same atomic read-modify-write operation, and
392+
there exists an atomic modification \placeholder{X} of \placeholder{M}
393+
such that \placeholder{A} reads the value stored by \placeholder{X} and
394+
\placeholder{X} precedes \placeholder{B}
395+
in the modification order of \placeholder{M}, or
396+
\item there exists an atomic modification \placeholder{X} of \placeholder{M}
397+
such that \placeholder{A} is coherence-ordered before \placeholder{X} and
398+
\placeholder{X} is coherence-ordered before \placeholder{B}.
400399
\end{itemize}
401400

402-
\begin{note} Although it is not explicitly required that \placeholder{S} include locks, it can
403-
always be extended to an order that does include lock and unlock operations, since the
404-
ordering between those is already included in the ``happens before'' ordering. \end{note}
405-
406401
\pnum
407-
For an atomic operation \placeholder{B} that reads the value of an atomic object \placeholder{M},
408-
if there is a \tcode{memory_order::seq_cst} fence \placeholder{X} sequenced before \placeholder{B},
409-
then \placeholder{B} observes either the last \tcode{memory_order::seq_cst} modification of
410-
\placeholder{M} preceding \placeholder{X} in the total order \placeholder{S} or a later modification of
411-
\placeholder{M} in its modification order.
412-
413-
\pnum
414-
For atomic operations \placeholder{A} and \placeholder{B} on an atomic object \placeholder{M}, where
415-
\placeholder{A} modifies \placeholder{M} and \placeholder{B} takes its value, if there is a
416-
\tcode{memory_order::seq_cst} fence \placeholder{X} such that \placeholder{A} is sequenced before
417-
\placeholder{X} and \placeholder{B} follows \placeholder{X} in \placeholder{S}, then \placeholder{B} observes
418-
either the effects of \placeholder{A} or a later modification of \placeholder{M} in its
419-
modification order.
402+
There is a single total order \placeholder{S}
403+
on all \tcode{memory_order::seq_cst} operations, including fences,
404+
that satisfies the following constraints.
405+
First, if \placeholder{A} and \placeholder{B} are
406+
\tcode{memory_order::seq_cst} operations and
407+
\placeholder{A} strongly happens before \placeholder{B},
408+
then \placeholder{A} precedes \placeholder{B} in \placeholder{S}.
409+
Second, for every pair of atomic operations \placeholder{A} and
410+
\placeholder{B} on an object \placeholder{M},
411+
where \placeholder{A} is coherence-ordered before \placeholder{B},
412+
the following four conditions are required to be satisfied by \placeholder{S}:
413+
\begin{itemize}
414+
\item if \placeholder{A} and \placeholder{B} are both
415+
\tcode{memory_order::seq_cst} operations,
416+
then \placeholder{A} precedes \placeholder{B} in \placeholder{S}; and
417+
\item if \placeholder{A} is a \tcode{memory_order::seq_cst} operation and
418+
\placeholder{B} happens before
419+
a \tcode{memory_order::seq_cst} fence \placeholder{Y},
420+
then \placeholder{A} precedes \placeholder{Y} in \placeholder{S}; and
421+
\item if a \tcode{memory_order::seq_cst} fence \placeholder{X}
422+
happens before \placeholder{A} and
423+
\placeholder{B} is a \tcode{memory_order::seq_cst} operation,
424+
then \placeholder{X} precedes \placeholder{B} in \placeholder{S}; and
425+
\item if a \tcode{memory_order::seq_cst} fence \placeholder{X}
426+
happens before \placeholder{A} and
427+
\placeholder{B} happens before
428+
a \tcode{memory_order::seq_cst} fence \placeholder{Y},
429+
then \placeholder{X} precedes \placeholder{Y} in \placeholder{S}.
430+
\end{itemize}
420431

421432
\pnum
422-
For atomic operations \placeholder{A} and \placeholder{B} on an atomic object \placeholder{M}, where
423-
\placeholder{A} modifies \placeholder{M} and \placeholder{B} takes its value, if there are
424-
\tcode{memory_order::seq_cst} fences \placeholder{X} and \placeholder{Y} such that \placeholder{A} is
425-
sequenced before \placeholder{X}, \placeholder{Y} is sequenced before \placeholder{B}, and \placeholder{X}
426-
precedes \placeholder{Y} in \placeholder{S}, then \placeholder{B} observes either the effects of
427-
\placeholder{A} or a later modification of \placeholder{M} in its modification order.
433+
\begin{note}
434+
This definition ensures that \placeholder{S} is consistent with
435+
the modification order of any atomic object \placeholder{M}.
436+
It also ensures that
437+
a \tcode{memory_order::seq_cst} load \placeholder{A} of \placeholder{M}
438+
gets its value either from the last modification of \placeholder{M}
439+
that precedes \placeholder{A} in \placeholder{S} or
440+
from some non-\tcode{memory_order::seq_cst} modification of \placeholder{M}
441+
that does not happen before any modification of \placeholder{M}
442+
that precedes \placeholder{A} in \placeholder{S}.
443+
\end{note}
428444

429445
\pnum
430-
For atomic modifications \placeholder{A} and \placeholder{B} of an atomic object \placeholder{M},
431-
\placeholder{B} occurs later than \placeholder{A} in the modification order of \placeholder{M} if:
432-
433-
\begin{itemize}
434-
\item there is a \tcode{memory_order::seq_cst} fence \placeholder{X} such that \placeholder{A}
435-
is sequenced before \placeholder{X}, and \placeholder{X} precedes \placeholder{B} in \placeholder{S}, or
436-
\item there is a \tcode{memory_order::seq_cst} fence \placeholder{Y} such that \placeholder{Y}
437-
is sequenced before \placeholder{B}, and \placeholder{A} precedes \placeholder{Y} in \placeholder{S}, or
438-
\item there are \tcode{memory_order::seq_cst} fences \placeholder{X} and \placeholder{Y} such that \placeholder{A}
439-
is sequenced before \placeholder{X}, \placeholder{Y} is sequenced before \placeholder{B},
440-
and \placeholder{X} precedes \placeholder{Y} in \placeholder{S}.
441-
\end{itemize}
442-
446+
\begin{note}
447+
We do not require that \placeholder{S} be consistent with
448+
``happens before''\iref{intro.races}.
449+
This allows more efficient implementation
450+
of \tcode{memory_order::acquire} and \tcode{memory_order::release}
451+
on some machine architectures.
452+
It can produce surprising results
453+
when these are mixed with \tcode{memory_order::seq_cst} accesses.
454+
\end{note}
443455

444456
\pnum
445-
\begin{note} \tcode{memory_order::seq_cst} ensures sequential consistency only for a
446-
program that is free of data races and uses exclusively \tcode{memory_order::seq_cst}
447-
operations. Any use of weaker ordering will invalidate this guarantee unless extreme
448-
care is used. In particular, \tcode{memory_order::seq_cst} fences ensure a total order
449-
only for the fences themselves. Fences cannot, in general, be used to restore sequential
450-
consistency for atomic operations with weaker ordering specifications. \end{note}
457+
\begin{note}
458+
\tcode{memory_order::seq_cst} ensures sequential consistency only
459+
for a program that is free of data races and
460+
uses exclusively \tcode{memory_order::seq_cst} atomic operations.
461+
Any use of weaker ordering will invalidate this guarantee
462+
unless extreme care is used.
463+
In many cases, \tcode{memory_order::seq_cst} atomic operations are reorderable
464+
with respect to other atomic operations performed by the same thread.
465+
\end{note}
451466

452467
\pnum
453468
Implementations should ensure that no ``out-of-thin-air'' values are computed that

source/basic.tex

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5193,17 +5193,39 @@
51935193
possible only through the use of consume operations. \end{note}
51945194

51955195
\pnum
5196-
An evaluation \placeholder{A} \defn{strongly happens before} an evaluation \placeholder{B}
5196+
An evaluation \placeholder{A} \defn{simply happens before} an evaluation \placeholder{B}
51975197
if either
51985198
\begin{itemize}
51995199
\item \placeholder{A} is sequenced before \placeholder{B}, or
52005200
\item \placeholder{A} synchronizes with \placeholder{B}, or
5201-
\item \placeholder{A} strongly happens before \placeholder{X} and \placeholder{X} strongly happens before \placeholder{B}.
5201+
\item \placeholder{A} simply happens before \placeholder{X} and
5202+
\placeholder{X} simply happens before \placeholder{B}.
52025203
\end{itemize}
52035204
\begin{note}
52045205
In the absence of consume operations,
5205-
the happens before and strongly happens before relations are identical.
5206-
Strongly happens before essentially excludes consume operations.
5206+
the happens before and simply happens before relations are identical.
5207+
\end{note}
5208+
5209+
\pnum
5210+
An evaluation \placeholder{A} \defn{strongly happens before}
5211+
an evaluation \placeholder{D} if, either
5212+
\begin{itemize}
5213+
\item \placeholder{A} is sequenced before \placeholder{D}, or
5214+
\item \placeholder{A} synchronizes with \placeholder{D}, and
5215+
both \placeholder{A} and \placeholder{D} are
5216+
sequentially consistent atomic operations\iref{atomics.order}, or
5217+
\item there are evaluations \placeholder{B} and \placeholder{C}
5218+
such that \placeholder{A} is sequenced before \placeholder{B},
5219+
\placeholder{B} simply happens before \placeholder{C}, and
5220+
\placeholder{C} is sequenced before \placeholder{D}, or
5221+
\item there is an evaluation \placeholder{B} such that
5222+
\placeholder{A} strongly happens before \placeholder{B}, and
5223+
\placeholder{B} strongly happens before \placeholder{D}.
5224+
\end{itemize}
5225+
\begin{note}
5226+
Informally, if \placeholder{A} strongly happens before \placeholder{B},
5227+
then \placeholder{A} appears to be evaluated before \placeholder{B}
5228+
in all contexts. Strongly happens before excludes consume operations.
52075229
\end{note}
52085230

52095231
\pnum

0 commit comments

Comments
 (0)