Bare Metal Programming Tool Kit
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
Public Member Functions | Friends | List of all members
bmptk::rtos::mutex Class Reference

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.
 

Detailed Description

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.

TBW

Definition at line 971 of file rtos.h.

Constructor & Destructor Documentation

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

Member Function Documentation

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  )

claim the mutex

If the mutex was set it it is now cleared, and the calling task owns the mutex.

Otherwise the current task waits (is halted) until the owning task calls signal() on the same mutex. The signal() calls will release the tasks in the order of their wait() calls.


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