43 #include "../rtt-config.h" 51 #ifdef ORO_OS_USE_BOOST_THREAD 53 #include <boost/thread/mutex.hpp> 54 #include <boost/thread/recursive_mutex.hpp> 55 #include <boost/thread/shared_mutex.hpp> 56 #include <boost/date_time/posix_time/posix_time_types.hpp> 71 virtual void lock() =0;
72 virtual void unlock() =0;
73 virtual bool trylock() = 0;
74 virtual bool timedlock(
Seconds) = 0;
95 #ifndef ORO_OS_USE_BOOST_THREAD 157 boost::timed_mutex m;
180 virtual void unlock()
190 virtual bool trylock()
202 virtual bool timedlock(
Seconds s)
204 return m.timed_lock( boost::posix_time::microseconds(
Seconds_to_nsecs(s)/1000) );
220 #ifndef ORO_OS_USE_BOOST_THREAD 282 boost::recursive_timed_mutex recm;
305 virtual void unlock()
315 virtual bool trylock()
317 return recm.try_lock();
327 virtual bool timedlock(
Seconds s)
329 return recm.timed_lock( boost::posix_time::microseconds(
Seconds_to_nsecs(s)/1000 ) );
354 static const Status one_reader = (1 << 0);
355 static const Status one_wait_to_read = (1 << 10);
356 static const Status one_writer = (1 << 20);
357 static const Status status_mask = (1 << 10) - 1;
359 static inline unsigned int readers(Status status) {
360 return (status & status_mask);
363 return ((status >> 10) & status_mask);
365 static inline unsigned int writers(Status status) {
366 return ((status >> 20) & status_mask);
390 Status old_status, new_status;
393 new_status += one_writer;
394 assert(writers(new_status) > 0);
395 }
while(!
CAS(&status, (
int) old_status, (
int) new_status));
397 if (readers(old_status) > 0 || writers(old_status) > 0) {
398 write_semaphore.
wait();
404 Status old_status, new_status;
405 unsigned int wait_to_read = 0;
408 assert(readers(old_status) == 0);
409 assert(writers(old_status) > 0);
410 new_status -= one_writer;
411 wait_to_read = waitToRead(old_status);
412 if (wait_to_read > 0) {
414 new_status = (new_status & (status_mask << 20)) | wait_to_read;
416 }
while(!
CAS(&status, (
int) old_status, (int) new_status));
419 if (wait_to_read > 0) {
420 for(
unsigned int i = 0; i < wait_to_read; ++i) read_semaphore.
signal();
423 if (writers(old_status) > 1) {
459 Status old_status, new_status;
462 if (writers(old_status) > 0) {
463 new_status += one_wait_to_read;
464 assert(waitToRead(new_status) > 0);
466 new_status += one_reader;
467 assert(readers(new_status) > 0);
469 }
while(!
CAS(&status, (
int) old_status, (
int) new_status));
472 if (writers(old_status) > 0) {
473 read_semaphore.
wait();
482 Status old_status, new_status;
485 assert(readers(old_status) > 0);
486 new_status -= one_reader;
487 }
while(!
CAS(&status, (
int) old_status, (
int) new_status));
490 if (readers(old_status) == 1 && writers(old_status) > 0) {
virtual bool timedlock(Seconds s)
Lock this mutex, but don't wait longer for the lock than the specified timeout.
double Seconds
Seconds are stored as a double precision float.
int rtos_mutex_rec_lock(rt_rec_mutex_t *m)
virtual ~Mutex()
Destroy a Mutex.
virtual void unlock_shared()
int rtos_mutex_unlock(rt_mutex_t *m)
void signal()
Raise this semaphore and signal one thread waiting on this semaphore.
int rtos_mutex_rec_init(rt_rec_mutex_t *m)
virtual bool timedlock(Seconds s)
Lock this mutex, but don't wait longer for the lock than the specified timeout.
virtual void lock_shared()
int rtos_mutex_rec_trylock(rt_rec_mutex_t *m)
An object oriented wrapper around a counting semaphore.
int rtos_mutex_rec_trylock_for(rt_rec_mutex_t *m, NANO_TIME relative_time)
virtual bool trylock()
Try to lock this mutex.
An object oriented wrapper around a condition variable.
virtual ~SharedMutex()
Destroy a shared Mutex.
virtual void unlock_shared()
Release shared ownership of this mutex.
static unsigned int waitToRead(Status status)
int oro_atomic_read(oro_atomic_t *a)
Returns the current counter value of the atomic structure a.
static unsigned int writers(Status status)
virtual bool timedlock_shared(Seconds)
virtual bool trylock_shared()
Attempt to obtain shared ownership of this mutex.
int rtos_mutex_rec_unlock(rt_rec_mutex_t *m)
virtual ~MutexRecursive()
Destroy a MutexRecursive.
void wait()
Lower this semaphore and return if value() is non zero.
bool CAS(volatile T *addr, const V &expected, const W &value)
Compare And Swap.
void ORO_ATOMIC_SETUP(oro_atomic_t *a, int n)
Initializes the uninitialized atomic structure a with a counter value of 'n'.
Semaphore write_semaphore
void lock_shared()
Obtain shared ownership of this mutex.
int rtos_mutex_lock(rt_mutex_t *m)
void ORO_ATOMIC_CLEANUP(oro_atomic_t *a)
Cleans up all resources allocated durint the setup of atomic structure a.
An object oriented wrapper around a shared mutex (multiple readers allowed, but only one writer with ...
int rtos_mutex_init(rt_mutex_t *m)
int rtos_mutex_destroy(rt_mutex_t *m)
virtual bool trylock()
Try to lock this mutex exclusively.
virtual bool timedlock(Seconds s)
Lock this mutex exclusively, but don't wait longer for the lock than the specified timeout...
int rtos_mutex_trylock(rt_mutex_t *m)
nsecs Seconds_to_nsecs(const Seconds s)
virtual bool timedlock_shared(Seconds s)
Attempt to obtain shared ownership of this mutex, but don't wait longer for the lock than the specifi...
virtual bool trylock_shared()
static unsigned int readers(Status status)
An object oriented wrapper around a non recursive mutex.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
An object oriented wrapper around a recursive mutex.
int rtos_mutex_trylock_for(rt_mutex_t *m, NANO_TIME relative_time)
Mutex()
Initialize a Mutex.
virtual bool trylock()
Try to lock this mutex.
MutexRecursive()
Initialize a recursive Mutex.
virtual ~MutexInterface()
Structure that contains an int for atomic operations.
SharedMutex()
Initialize a shared Mutex.
int rtos_mutex_rec_destroy(rt_rec_mutex_t *m)