public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: Alexander Terekhov <TEREKHOV@de.ibm.com>
To: rpj@callisto.canberra.edu.au
Cc: pthreads-win32@sources.redhat.com
Subject: Re: mutexes: "food for thought"
Date: Fri, 08 Oct 2004 12:53:00 -0000	[thread overview]
Message-ID: <OF3993F0AD.A1619485-ONC1256F27.0046165C-C1256F27.0046D581@de.ibm.com> (raw)
In-Reply-To: <4166852F.8090300@callisto.canberra.edu.au>

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

public:

  // 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))
        m_retry_event.wait();
  } 

  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) 
      m_retry_event.set();
  } 
};

As for Ulrich's "take 2", my "take 3" can be found here:

http://listman.redhat.com/archives/phil-list/2003-October/msg00030.html
(Subject: Mutex, Take 3 (for "dumb" futex))

regards,
alexander.

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

http://lists.boost.org/MailArchives/boost/msg67616.php
http://lists.boost.org/MailArchives/boost/msg67651.php
http://lists.boost.org/MailArchives/boost/msg67667.php

regards,
alexander.

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

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <4166852F.8090300@callisto.canberra.edu.au>
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:
  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=OF3993F0AD.A1619485-ONC1256F27.0046165C-C1256F27.0046D581@de.ibm.com \
    --to=terekhov@de.ibm.com \
    --cc=pthreads-win32@sources.redhat.com \
    --cc=rpj@callisto.canberra.edu.au \
    /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).