|
Boost : |
From: Martin Taylor (martin.taylor_at_[hidden])
Date: 2004-01-27 10:08:04
Hi
I would like to put forward an implementation of a lightweight mutex
for MacOS.
I have tested it with shared_ptr_mt_test.cpp without problem.
It's fairly short, so I hope you don't mind me posting it directly here
for review.
I should add also that I didn't really write this code (that's not a
disclaimer - Howard Hinnant cooked it up after I asked the Codewarrior
newsgroup for help!)
Anyway, here's the code:
#if __MACH__
#include <sched.h>
#else
#include <Multiprocessing.h>
#endif
namespace boost
{
namespace detail
{
class lightweight_mutex
{
private:
volatile int a_;
lightweight_mutex(lightweight_mutex const &);
lightweight_mutex & operator=(lightweight_mutex const &);
public:
lightweight_mutex(): a_(0)
{
}
class scoped_lock;
friend class scoped_lock;
class scoped_lock
{
private:
lightweight_mutex & m_;
scoped_lock(scoped_lock const &);
scoped_lock & operator=(scoped_lock const &);
void yield_thread()
{
#if __MACH__
sched_yield();
#else
MPYield();
#endif
}
public:
explicit scoped_lock(lightweight_mutex & m);
~scoped_lock()
{
m_.a_ = 0;
}
};
};
inline lightweight_mutex::scoped_lock::scoped_lock(lightweight_mutex &
m)
: m_(m)
{
register volatile int *p = &m_.a_;
register int f;
register int one = 1;
asm
{
loop:
lwarx f, 0, p
cmpwi f, 0
bne- yield
stwcx. one, 0, p
beq+ done
}
yield:
yield_thread();
goto loop;
done:
;
}
} // namespace detail
} // namespace boost
Martin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk