mutexes: "food for thought"
Alexander Terekhov
TEREKHOV@de.ibm.com
Fri Oct 8 12:53:00 GMT 2004
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.
More information about the Pthreads-win32
mailing list