public inbox for
 help / color / mirror / Atom feed
From: Alexander Terekhov <>
Subject: Re: mutexes: "food for thought"
Date: Fri, 08 Oct 2004 12:53:00 -0000	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <>

Hi Ross,

I'm sorta busy at the moment. Here's a version without DCSI.

More next week.

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

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


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

  void lock() throw() {
    if (m_lock_status.swap(1, msync::acq)) 
      while (m_lock_status.swap(-1, msync::acq))

  bool trylock() throw() {
    return !m_lock_status.swap(1, msync::acq) ? 
      true : !m_lock_status.swap(-1, msync::acq);

  bool timedlock(absolute_timeout const & timeout) throw() {
    if (m_lock_status.swap(1, msync::acq)) {
      while (m_lock_status.swap(-1, msync::acq))
        if (!m_retry_event.timedwait(timeout)) 
          return false;
    }  return true;

  void unlock() throw() {
    if (m_lock_status.swap(0, msync::rel) < 0) 

As for Ulrich's "take 2", my "take 3" can be found here:
(Subject: Mutex, Take 3 (for "dumb" futex))


P.S. "POSIX-safety" with respect to destruction:


       reply	other threads:[~2004-10-08 12:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <>
2004-10-08 12:53 ` Alexander Terekhov [this message]
2003-10-18 17:47 Alexander Terekhov
2004-10-08 12:49 ` Ross Johnson

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:

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

  git send-email \ \ \ \ \

* 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).