Bare Metal Programming Tool Kit
|
mutual execlusion semaphore More...
#include <rtos.h>
Public Member Functions | |
mutex (const char *name="") | |
constructor, specify the name | |
~mutex () | |
generates an error | |
void | print (std::ostream &stream, bool header=true) const |
prints a mutex, for debugging only. | |
void | wait (void) |
void | signal (void) |
release the mutex | |
Friends | |
void | add (mutex *m) |
void | print (std::ostream &stream) |
prints statistics about the tasks to the stream. | |
mutual execlusion semaphore
A mutex (mutual exclusion semaphore) is a synchronization mechanism that is used to give a task exclusive access to some resource: the task can execute a sequence of statements, being sure that no other task is accessing the same resource.
A typical use is to protect a resource (for instance global data) that should be used by only one task at a time, so it can update it and leave it in a consistent state.
A mutex is not created for a particular task,and it is not a waitable.
Initially a mutex is free. The mutex::wait() operation blocks the task until the mutex is free, and then claims the mutex for the executing task. The mutex::signal() operation frees the mutex again. It is an error to call mutex::signal on a mutex that is not currently owned by the executing task.
The example below shows two tasks that write messages to the LCD display. Each task writes to its own line. For the demonstration, after each LCD operation a sleep() is put to allow the other tasks to run. A mutex prevents the other task from accessing the LCD while the first one is still using it.
bmptk::rtos::mutex::mutex | ( | const char * | name = "" | ) |
constructor, specify the name
The name is used for debugging only.
bmptk::rtos::mutex::~mutex | ( | ) |
generates an error
A mutex should never be destroyed
void bmptk::rtos::mutex::signal | ( | void | ) |
release the mutex
If one or more tasks are waiting for the mutex the firs one is released, and it now owns the mutex. Otherwise, if the mutex is cleared it is now set.
It is an error for a task to call signal() on a mutex that it does not own (that it did not call wait() on). After the signal the task no longer owns the mutex.
void bmptk::rtos::mutex::wait | ( | void | ) |