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"
Date: Sat, 18 Oct 2003 17:47:00 -0000	[thread overview]
Message-ID: <OF1BDED071.2BB52616-ONC1256DC3.005FFB57-C1256DC3.0061B143@de.ibm.com> (raw)

G'Day,

here's "ala futex based" mutex stuff using XCHG. 

No need for CAS. I hope that it will work just fine. 

Can you see any harmful race condition(s) here? 

TIA.

#define SWAP_BASED_MUTEX_FOR_WINDOWS_INITIALIZER { 0, 0 }

struct swap_based_mutex_for_windows {

  atomic<int>                m_lock_status;  // -1: free, 0: locked, 1 
lock-contention
  atomic<auto_reset_event *> m_retry_event;  // DCSI'd 

  void DCSI(); // double-checked serialized initialization
  void slow_lock();
  bool slow_trylock();
  bool slow_timedlock(absolute_timeout const & timeout);
  void release_one_waiter_if_any();

  void lock() {
    if (m_lock_status.swap(0, msync::acq) >= 0) slow_lock();
  } 

  bool trylock() {
    return (m_lock_status.swap(0, msync::acq) < 0) ? true : 
slow_trylock(); 
  }

  bool timedlock(absolute_timeout const & timeout) {
    return (m_lock_status.swap(0, msync::acq) < 0) ? true : 
slow_timedlock(timeout);
  }

  void unlock() {
    if (m_lock_status.swap(-1, msync::rel) > 0) 
release_one_waiter_if_any();
  }

};

void swap_based_mutex_for_windows::slow_lock() {
  DCSI();
  while (m_lock_status.swap(1, msync::acq) >= 0)
    m_retry_event.load(msync::none)->wait();
}

bool swap_based_mutex_for_windows::slow_trylock() {
  DCSI();
  return m_lock_status.swap(1, msync::acq) < 0;
}

bool swap_based_mutex_for_windows::slow_timedlock(absolute_timeout const & 
timeout) {
  DCSI();
  while (m_lock_status.swap(1, msync::acq) >= 0)
    if (!m_retry_event.load(msync::none)->timedwait(timeout)) return 
false;
  return true;
}

void swap_based_mutex_for_windows::release_one_waiter_if_any() {
  m_retry_event.load(msync::none)->set();
}

void swap_based_mutex_for_windows::DCSI() {
  if (!m_retry_event.load(msync::none)) {
    named_windows_mutex_trick guard(this);
    if (!m_retry_event.load(msync::none)) {
      m_retry_event.store(new auto_reset_event(), msync::rel);
      m_lock_status.store(-1, msync::rel);
    }
  }
}

regards,
alexander.

P.S. I've never run it. Just a sketch.

             reply	other threads:[~2003-10-18 17:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-18 17:47 Alexander Terekhov [this message]
2004-10-08 12:49 ` Ross Johnson
2004-10-26 17:29   ` mutexes: "food for thought" [upcoming XBOX] Alexander Terekhov
     [not found] <4166852F.8090300@callisto.canberra.edu.au>
2004-10-08 12:53 ` mutexes: "food for thought" Alexander Terekhov

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=OF1BDED071.2BB52616-ONC1256DC3.005FFB57-C1256DC3.0061B143@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).