[cpp-threads] modes

Alexander Terekhov alexander.terekhov at gmail.com
Sat May 7 17:34:54 BST 2005


On 5/7/05, Doug Lea <dl at cs.oswego.edu> wrote:
[...]
> 2. There's very little motivation to define CAS-with-acquire only.
[...]
> Also, as always, I just don't think that explicit LL/SC is worth
> exposing. LL/SC instructions tend to have so many restrictions and
> quirks that the just about only thing it is good for is CAS/weakCAS
> emulation

I disagree on both accounts.

http://sourceware.org/ml/pthreads-win32/2004/msg00120.html
(msync::acq can be replaced with msync::acq, and hopefully smart
compiler can hoist {lw}sync out of attempt_update loop).

CAS-based version looks similar, but is less effecient when CAS 
is emulated on top of LL/LR-SC.

void lock() {
  int old = load_naked(&m_lock_status);
  if (old || old = cas_ccacq(&m_lock_status, 0, 1)) {
    do {
      while (old < 0 || old = cas_naked(&m_lock_status, 1, -1)) { 
        m_retry_event.wait(); // acquire semantics
        old = load_naked(&m_lock_status);
      }
    } while (old = cas_ccacq(&m_lock_status, 0, -1));
  }
}

void unlock() {
  int old = load_naked(&m_lock_status);
  if (old < 0 || cas_rel(&m_lock_status, 1, 0) < 0) {
     store_rel(&m_lock_status, 0);
     m_retry_event.set(); // release semantics
  }
}

regards,
alexander.




More information about the cpp-threads mailing list