public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: Alexander Terekhov <TEREKHOV@de.ibm.com>
To: pthreads-win32@sources.redhat.com
Subject: mutexes: "food for thought" [upcoming XBOX]
Date: Tue, 26 Oct 2004 17:29:00 -0000	[thread overview]
Message-ID: <OFF1FC0806.2D54E214-ONC1256F39.005A9C7C-C1256F39.00601ADF@de.ibm.com> (raw)
In-Reply-To: <41668CD1.7090207@ise.canberra.edu.au>

Given

http://www.theinquirer.net/?article=14407
http://www.gamepro.com/microsoft/xbox/hardware/news/35216.shtml

here's "food for thought" illustration. 

// doesn't provide "POSIX-safety" with respect to destruction
class mutex_for_XBOX_NEXT { // noncopyable

  atomic<int>      m_lock_status; // 0: free, 1/-1: locked/contention
  auto_reset_event m_retry_event; // prohibitively slow bin.sema/gate

  template<typename T>
  int attempt_update(int old, int new, T msync) {
    while (!m_lock_status.store_conditional(new, msync)) {
      int fresh = m_lock_status.load_reserved(msync::none);
      if (fresh != old)
        return fresh;
    }
    return old;
  }

public:

  // ctor/dtor [w/o lazy event init] 

  bool trylock() throw() {
    return !(m_lock_status.load_reserved(msync::none) || 
             attempt_update(0, 1, msync::acq)); 
  }

  // bool timedlock() omitted for brevity

  void lock() throw() {
    int old = m_lock_status.load_reserved(msync::none);
    if (old || old = attempt_update(0, 1, msync::acq)) {
      do {
        while (old < 0 || 
               old = attempt_update(1, -1, msync::none)) { 
          m_retry_event.wait();
          old = m_lock_status.load_reserved(msync::none);
          if (!old) break;
        }
      } while (old = attempt_update(0, -1, msync::acq));
    }
  }

  void unlock() throw() {
    if (m_lock_status.load_reserved(msync::none) < 0 || 
        attempt_update(1, 0, msync::rel) < 0) { // or just !SC
      m_lock_status.store(0, msync::rel);
      m_retry_event.set();
    }
  }

};

Oder?

regards,
alexander.

      reply	other threads:[~2004-10-26 17:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-18 17:47 mutexes: "food for thought" Alexander Terekhov
2004-10-08 12:49 ` Ross Johnson
2004-10-26 17:29   ` Alexander Terekhov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=OFF1FC0806.2D54E214-ONC1256F39.005A9C7C-C1256F39.00601ADF@de.ibm.com \
    --to=terekhov@de.ibm.com \
    --cc=pthreads-win32@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).