<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2130: Missing ordering constraints</title>
<meta property="og:title" content="Issue 2130: Missing ordering constraints">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2130.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++14">C++14</a> status.</em></p>
<h3 id="2130"><a href="lwg-defects.html#2130">2130</a>. Missing ordering constraints</h3>
<p><b>Section:</b> 32.5.4 <a href="https://wg21.link/atomics.order">[atomics.order]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Mark Batty <b>Opened:</b> 2012-02-22 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#atomics.order">active issues</a> in [atomics.order].</p>
<p><b>View all other</b> <a href="lwg-index.html#atomics.order">issues</a> in [atomics.order].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>C11 issue 407</b></p>

<p>
It seems that both C11 and C++11 are missing the following two derivatives of this 
rule:
</p>
<blockquote><p>
  For atomic modifications <code>A</code> and <code>B</code> of an atomic object <code>M</code>, if there is
  a <code>memory_order_seq_cst</code> fence <code>X</code> such that <code>A</code> is sequenced before <code>X</code>,
  and <code>X</code> precedes <code>B</code> in <code>S</code>, then <code>B</code> occurs later than <code>A</code> in the
  modification order of <code>M</code>.
</p></blockquote>
<blockquote><p>
  For atomic modifications <code>A</code> and <code>B</code> of an atomic object <code>M</code>, if there is
  a <code>memory_order_seq_cst</code> fence <code>Y</code> such that <code>Y</code> is sequenced before <code>B</code>,
  and <code>A</code> precedes <code>Y</code> in <code>S</code>, then <code>B</code> occurs later than <code>A</code> in the
  modification order of <code>M</code>.
</p></blockquote>
<p>
Above wording has been suggested for the Technical Corrigendum of C11 via issue 407, details can be found 
<a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1584.pdf">here</a>.
</p>

<p><i>[2012-03-19: Daniel proposes a slightly condensed form to reduce wording duplications]</i></p>


<p><i>[2012-03-20: Hans comments]</i></p>

<p>
The usage of the term <em>atomic operations</em> in 32.5.4 <a href="https://wg21.link/atomics.order">[atomics.order]</a> p7 is actually
incorrect and should better be replaced by <em>atomic modifications</em> as used in the C11
407 wording.
<p/>
There seems to be a similar wording incorrectness used in 6.10.2 <a href="https://wg21.link/intro.multithread">[intro.multithread]</a> p17
which should be corrected as well.
</p>

<p><i>[2012, Portland: move to Review]</i></p>

<p>
Olivier: does the fence really participate in the modifications?
</p>
<p>
Hans: S is the total set of <em>all</em> sequentially consistent operations,
      and sequentially consistent fences are in S.
</p>
<p>
Olivier: this sort of combination of a pair of half-open rules seems to imply the
         write must make it to main memory
</p>
<p>
But not all implementations treat a fence as a memory operation; cannot observe the half-open rule.
</p>
<p>
Hans: not sure this is actually prevented here. You could defer until the next load.
      What the wording doesn't quite show is that the third bullet in the new wording
      is already in the standard.
</p>
<p>
Hans: it is the interaction between fences on one side and other memory modifications
      on the other that is being defined here.
</p>
<p>
Pablo: S is not directly observable; it is a hypothetic ordering.
</p>
<p>
Moved to review
</p>
<p>
Hans: to alert C liaison
</p>

<p><i>[2013-04-20, Bristol]</i></p>

<p>Accepted for the working paper</p>



<p id="res-2130"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3376.</p>

<ol>
<li><p><em>[Drafting note: The project editor is kindly asked to consider to replace in 6.10.2 <a href="https://wg21.link/intro.multithread">[intro.multithread]</a> p17
the phrase <em>"before an operation <i>B</i> on <i>M</i>"</em> by <em>"before a modification <i>B</i> of <i>M</i>"</em>.]</em></p>
</li>
<li><p>Change 32.5.4 <a href="https://wg21.link/atomics.order">[atomics.order]</a> paragraph 7 as indicated: <em>[Drafting note: Note that the
wording change intentionally does also replace the term <em>atomic operation</em> by <em>atomic modification</em>]</em>
</p>

<p>
-7- <del>For atomic operations <i>A</i> and <i>B</i> on an atomic object <i>M</i>, if there are 
<code>memory_order_seq_cst</code> fences <i>X</i> and <i>Y</i> such that <i>A</i> is sequenced before <i>X</i>, 
<i>Y</i> is sequenced before <i>B</i>, and <i>X</i> precedes <i>Y</i> in <i>S</i>, then <i>B</i> 
occurs later than <i>A</i> in the modification order of <i>M</i>.</del>
<ins>For atomic modifications <i>A</i> and <i>B</i> of an atomic object <i>M</i>, <i>B</i> occurs
later than <i>A</i> in the modification order of <i>M</i> if:</ins>
<ul>
<li><ins>there is a <code>memory_order_seq_cst</code> fence <i>X</i> such that <i>A</i> is sequenced before <i>X</i>, 
and <i>X</i> precedes <i>B</i> in <i>S</i>, or</ins>
</li>
<li><ins>there is a <code>memory_order_seq_cst</code> fence <i>Y</i> such that <i>Y</i> is sequenced before <i>B</i>,
and <i>A</i> precedes <i>Y</i> in <i>S</i>, or</ins>
</li>
<li><ins>there are <code>memory_order_seq_cst</code> fences <i>X</i> and <i>Y</i> such that <i>A</i> is sequenced 
before <i>X</i>, <i>Y</i> is sequenced before <i>B</i>, and <i>X</i> precedes <i>Y</i> in <i>S</i>.</ins>
</li>
</ul>
<p/>
-8- [ <i>Note</i>: <code>memory_order_seq_cst</code> ensures sequential consistency only for a program that is free of data races
and uses exclusively <code>memory_order_seq_cst</code> operations. Any use of weaker ordering will invalidate this
guarantee unless extreme care is used. In particular, <code>memory_order_seq_cst</code> fences ensure a total order
only for the fences themselves. Fences cannot, in general, be used to restore sequential consistency for atomic
operations with weaker ordering specifications. &mdash; <i>end note</i> ]
</p>
</li>
</ol>






</body>
</html>
