title: Does acquiring a spinlock require compare-and-swap or is swap enough?
body: <p>Suppose we have a <code>spinlock</code> implementation:</p>
  
  <pre><code>struct Lock {
      locked : Atomic(bool),
  }
  </code></pre>
  
  <p>Then an unlock function could be:</p>
  
  <pre><code>fun unlock(lock : &amp;Lock) {
      atomic_store(&amp;lock.locked, false, release);
  }
  </code></pre>
  
  <p>But what about <code>lock</code>? Commonly, it uses a compare-and-swap like this:</p>
  
  <pre><code>fun lock(lock : &amp;Lock) {
      while atomic_compare_and_swap(&amp;lock.locked, false, true, acquire) {}
  }
  </code></pre>
  
  <p>But wouldn't a swap be enough for this? Something like this:</p>
  
  <pre><code>fun lock(lock : &amp;Lock) {
      while atomic_swap(&amp;lock.locked, true, acquire) {}
  }
  </code></pre>
  
  <p>Is there any problem with this?</p>