|
379 | 379 | release sequence headed by \placeholder{A}.
|
380 | 380 |
|
381 | 381 | \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 |
389 | 385 | \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}. |
400 | 399 | \end{itemize}
|
401 | 400 |
|
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 |
| - |
406 | 401 | \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} |
420 | 431 |
|
421 | 432 | \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} |
428 | 444 |
|
429 | 445 | \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} |
443 | 455 |
|
444 | 456 | \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} |
451 | 466 |
|
452 | 467 | \pnum
|
453 | 468 | Implementations should ensure that no ``out-of-thin-air'' values are computed that
|
|
0 commit comments