|
General IRI utilities
|
Implementation of a mutual exclusion mechanism. More...
#include <mutex.h>
Public Member Functions | |
| void | enter (void) |
| function to request access to the critical section More... | |
| bool | try_enter (void) |
| function to request access to the critical section and return if denied. More... | |
| void | exit (void) |
| function to release the critical section More... | |
| CMutex () | |
| default constructor More... | |
| virtual | ~CMutex () |
| default destructor More... | |
This class implements the mutual exclusion mechanism. Any class that needs to handle shared resources must have an object of this class as a member to avoid that multiple threads of execution simultaneoulsy modify memory or peripherals in a critical section.
Any thread that needs to access shared resources must first try to lock the mutual exclusion mechanism by calling the enter() function. If the critical section is free, the current thread will be granted access but any other thread will get blocked at the enter() function call. Otherwise, if the shared memory is already in use, the curretn thread will be blocked until the mutex is released by the current owner by calling the exit() function.
The necessary information of the mutex is initialized when an object of this class is createdd, and automatically destroyed when the object is deleted. This class uses exceptions to report error, and the name of the associated exception class is CMutexException. For a more detailed description of this class, see the corresponding documentation.
| CMutex::CMutex | ( | ) |
this constructor creates the mutex and initializes it to be used throughout the code. If an error ocurrs, an exception is thown that must be handled by a try ... catch environment.
This function throws a CMutexException in case of any error. See the corresponding documentation for a more detailed description.
|
virtual |
the destructor tries to destroy the mutex and throws and exception in case of any error. The excpetion must be handled by a try ... catch environment.
This function throws a CMutexException in case of any error. See the corresponding documentation for a more detailed description.
Definition at line 75 of file mutex.cpp.
References exit(), and try_enter().

| void CMutex::enter | ( | void | ) |
this function must be call before accessing any shared resource to block access to any other thread. If the resource is free the function will return immediatelly, and it will get blocked if there is another thread accessing the shared resources.
This function throws a CMutexException in case of any error. See the corresponding documentation for a more detailed description.
Definition at line 35 of file mutex.cpp.
Referenced by CThread::attach(), CThreadServer::attach_thread(), CEventServer::create_event(), CThreadServer::create_thread(), CEventServer::delete_event(), CThreadServer::delete_thread(), CThread::detach(), CThreadServer::detach_thread(), CLog::disable(), CLog::enable(), CThread::end(), CThreadServer::end_thread(), CEventServer::event_is_set(), CEventServer::get_num_activations(), CEventServer::get_num_events(), CThread::get_state(), CEvent::is_set(), CThread::kill(), CThreadServer::kill_thread(), CLog::log(), CLog::log_vector(), CEvent::reset(), CEventServer::reset_event(), CEvent::set(), CEventServer::set_event(), CThread::start(), CThreadServer::start_thread(), CThread::thread_function(), CEvent::wait(), CEventServer::wait_all(), CEventServer::wait_first(), CEvent::~CEvent(), and CLog::~CLog().

| bool CMutex::try_enter | ( | void | ) |
If you'd like to access a resource when it is available, but continue if the resource is not available, then call this function before accessing the shared resource. If the resource is free it will return true immediatelly, and false if there is another thread accessing the shared resources.
If successfull, this function also locks the mutex, so it is necessary to release the mutex if the return value is true. Otherwise, the mutex will block the next time it is accessed.
This function throws a CMutexException in case of any error. See the corresponding documentation for a more detailed description.
Definition at line 46 of file mutex.cpp.
Referenced by ~CMutex().

| void CMutex::exit | ( | void | ) |
this function must be called after accessing the shared resources to free the critical section an allow other threads to access it. If this function is not called, the shared resource will be blocked forever and so will be all the threads trying to access it.
If this function is called more than once without calling the enter() function, it will not fail, but the mutex will behave erratically and it may fail.
This function throws a CMutexException in case of any error. See the corresponding documentation for a more detailed description.
Definition at line 64 of file mutex.cpp.
Referenced by CThread::attach(), CThreadServer::attach_thread(), CEventServer::create_event(), CThreadServer::create_thread(), CEventServer::delete_event(), CThreadServer::delete_thread(), CThread::detach(), CThreadServer::detach_thread(), CLog::disable(), CLog::enable(), CThread::end(), CThreadServer::end_thread(), CEventServer::event_is_set(), CEventServer::get_num_activations(), CEventServer::get_num_events(), CThread::get_state(), CEvent::is_set(), CThread::kill(), CThreadServer::kill_thread(), CLog::log(), CLog::log_vector(), CEvent::reset(), CEventServer::reset_event(), CEvent::set(), CEventServer::set_event(), CThread::start(), CThreadServer::start_thread(), CThread::thread_function(), CEvent::wait(), CEventServer::wait_all(), CEventServer::wait_first(), CEvent::~CEvent(), CLog::~CLog(), and ~CMutex().

1.8.6