<html>
    <head>
      <base href="https://bugs.freedesktop.org/">
    </head>
    <body>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Segmentation fault in _cairo_traps_compositor_glyphs"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=103037#c19">Comment # 19</a>
              on <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Segmentation fault in _cairo_traps_compositor_glyphs"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=103037">bug 103037</a>
              from <span class="vcard"><a class="email" href="mailto:spitzak@gmail.com" title="Bill Spitzak <spitzak@gmail.com>"> <span class="fn">Bill Spitzak</span></a>
</span></b>
        <pre><span class="quote">>> 1. Everybody is wrong and the above code can fail on Intel-style processors.</span >

<span class="quote">>It can fail if compiler decided to optimize access to "y" and reads it only once before entering the loop.</span >

The problem with that argument is that there are atomic operations inside the
loop, including normal sequential fences. The compiler must re-read 'y' after
the loop contents execute or it is wrong.

The argument *does* hold if you use 'x' instead of 'y', it could read an old x,
then get a new y==1 and think it's copy of x is ok (the loop is not run so it
does not need to re-read x). In practice x is either written to by code inside
the loop, or is pointed to by y, so that it is unlikely for an optimizer to do
this. But this is a good reason to write something to force it not to happen,
like your suggestion:

<span class="quote">>while (atomic_load_explicit (&y, memory_order_acquire) != 1) {</span >

That is what I thought would work but quick test reveals that it produces a
sync instruction before the access. Unless I am really confused the sync should
not be necessary, since the worst that could happen is it would get the old
value of y and think x is no good yet even though it is, and correctly-written
code must already handle this. Possibly I need to turn on optimization?

BTW the current code is fine, or the release/aquire version (which seems to
compile to the same thing for me but might be the actual "correct" code). Just
trying to get an answer to this question as I see this all the time and there
is a very strong desire by programmers to make the overhead of run-once code
minimal after it is run the first time, so the temptation to remove the atomic
operation which seems to not be required needs an extremely convincing argument
against it.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are the QA Contact for the bug.</li>
      </ul>
    </body>
</html>