This is the mail archive of the
pthreads-win32@sources.redhat.com
mailing list for the pthreas-win32 project.
Re: mutexes: "food for thought"
- From: Alexander Terekhov <TEREKHOV at de dot ibm dot com>
- To: rpj at callisto dot canberra dot edu dot au
- Cc: pthreads-win32 at sources dot redhat dot com
- Date: Fri, 8 Oct 2004 14:53:33 +0200
- Subject: Re: mutexes: "food for thought"
- Sensitivity:
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.