Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

MutexLock< num_doors > Class Template Reference

Implements a mutual exclusion lock using pthread mutex. More...

#include <MutexLock.h>

Inheritance diagram for MutexLock< num_doors >:

Detailed Description

template<unsigned int num_doors>
class MutexLock< num_doors >

Implements a mutual exclusion lock using pthread mutex.

Use this to prevent more than one thread from accessing a data structure at the same time (which often leads to unpredictable and unexpected results)

The template parameter is not used (only needed if compiling with IPC enabled)

Locks in this class can be recursive or non-recursive, depending whether you call releaseAll() or unlock(). If you lock 5 times, then you need to call unlock() 5 times as well before it will be unlocked. However, if you lock 5 times, just one call to releaseAll() will undo all 5 levels of locking.

Just remember, unlock() releases one level. But releaseAll() completely unlocks.

Note that there is no check that the thread doing the unlocking is the one that actually has the lock. Be careful about this.

Definition at line 106 of file MutexLock.h.

List of all members.

Public Member Functions

 MutexLock ()
 constructor, gets a new semaphore from the semaphore manager
 ~MutexLock ()
 destructor, releases semaphore back to semaphore manager
void lock (int id)
 blocks until lock is achieved. This is done efficiently using a SysV style semaphore
bool try_lock (int id)
 attempts to get a lock, returns true if it succeeds
void unlock ()
 releases one recursive lock-level from whoever has the current lock
void releaseAll ()
 completely unlocks, regardless of how many times a recursive lock has been obtained
unsigned int get_lock_level () const
 returns the lockcount
int owner () const
 returns the current owner's id

Protected Member Functions

virtual void useResource (Resource::Data &)
 marks the resource as in use
virtual void releaseResource (Resource::Data &)
 releases the resource

Protected Attributes

unsigned int owner_index
 holds the tekkotsu process id of the current lock owner
Thread::Lock thdLock
 all the actual implementation is handed off to the thread lock

Friends

class MarkScope

Constructor & Destructor Documentation

template<unsigned int num_doors>
MutexLock< num_doors >::MutexLock (  ) 

constructor, gets a new semaphore from the semaphore manager

Definition at line 109 of file MutexLock.h.

template<unsigned int num_doors>
MutexLock< num_doors >::~MutexLock (  ) 

destructor, releases semaphore back to semaphore manager

Definition at line 112 of file MutexLock.h.


Member Function Documentation

template<unsigned int num_doors>
unsigned int MutexLock< num_doors >::get_lock_level (  )  const

returns the lockcount

Definition at line 156 of file MutexLock.h.

Referenced by MessageQueue< MAX_UNREAD, MAX_RECEIVERS, MAX_SENDERS >::sendMessage().

template<unsigned int num_doors>
void MutexLock< num_doors >::lock ( int  id  ) 

blocks until lock is achieved. This is done efficiently using a SysV style semaphore

You should pass some process-specific ID number as the input - just make sure no other process will be using the same value.

Definition at line 123 of file MutexLock.h.

Referenced by IPCEventTranslator::bufferRequest(), MotionManager::func_begin(), MotionManager::InitAccess(), MotionManager::lock(), MessageQueue< MAX_UNREAD, MAX_RECEIVERS, MAX_SENDERS >::sendMessage(), and MutexLock< MAX_ACCESS >::useResource().

template<unsigned int num_doors>
int MutexLock< num_doors >::owner (  )  const

returns the current owner's id

Definition at line 159 of file MutexLock.h.

template<unsigned int num_doors>
void MutexLock< num_doors >::releaseAll (  ) 

completely unlocks, regardless of how many times a recursive lock has been obtained

Definition at line 149 of file MutexLock.h.

Referenced by MessageQueue< MAX_UNREAD, MAX_RECEIVERS, MAX_SENDERS >::sendMessage().

template<unsigned int num_doors>
virtual void MutexLock< num_doors >::releaseResource ( Resource::Data d  )  [protected, virtual]

releases the resource

Implements Resource.

Definition at line 164 of file MutexLock.h.

template<unsigned int num_doors>
bool MutexLock< num_doors >::try_lock ( int  id  ) 

attempts to get a lock, returns true if it succeeds

You should pass some process-specific ID number as the input - just make sure no other process will be using the same value.

Definition at line 132 of file MutexLock.h.

Referenced by MotionManager::trylock().

template<unsigned int num_doors>
void MutexLock< num_doors >::unlock (  ) 

releases one recursive lock-level from whoever has the current lock

Definition at line 140 of file MutexLock.h.

Referenced by IPCEventTranslator::bufferRequest(), MotionManager::func_end(), MotionManager::InitAccess(), IPCEventTranslator::post(), MutexLock< MAX_ACCESS >::releaseResource(), and MotionManager::unlock().

template<unsigned int num_doors>
virtual void MutexLock< num_doors >::useResource ( Resource::Data d  )  [protected, virtual]

marks the resource as in use

Implements Resource.

Definition at line 163 of file MutexLock.h.


Friends And Related Function Documentation

template<unsigned int num_doors>
friend class MarkScope [friend]

Definition at line 162 of file MutexLock.h.


Member Data Documentation

template<unsigned int num_doors>
unsigned int MutexLock< num_doors >::owner_index [protected]
template<unsigned int num_doors>
Thread::Lock MutexLock< num_doors >::thdLock [protected]

The documentation for this class was generated from the following file:

Tekkotsu v5.1CVS
Generated Mon May 9 04:59:13 2016 by Doxygen 1.6.3