ChibiOS/RT Logo ChibiOS/RT

Architecture - Reference Manual - Guides

Mutual Exclusion guide

The most common problem when writing multithreaded code is the synchronization on the shared resources/services.
ChibiOS/RT offers a rich variety of mechanisms that apparently solve the same problem. I wrote apparently because each mechanism has its pro and cons. This article will introduce the various mechanisms and the explain the right scenarios for each one.

Basics

Some of the concepts mentioned in this article can be found in the following Wikipedia articles:

Mutual exclusion by System Locks

This is the lowest level mechanism, the system is locked by invoking the chSysLock() API and then unlocked by invoking chSysUnlock().
The implementation is architecture dependent but it is guaranteed to, at least, disable the interrupt sources with hardware priority below or equal the kernel level.

Advantages

Disadvantages

When use Locks

Example

 ...
 chSysLock();
 /* Protected code */
 chSysUnlock();
 ...

Mutual exclusion by Semaphores

In ChibiOS/RT the counting semaphores are mainly meant as a synchronization mechanism between interrupt handlers and high level code running at thread level. Usually a thread waits on a semaphore that is signaled asynchronously by an interrupt handler.
The semaphores can, however, be used as simple mutexes by initializing the semaphore counter to one.

Advantages

Disadvantages

When use Semaphores

Example

 static Semaphore sem; /* Semaphore declaration */
 ...
 chSemInit(&sem, 1); /* Semaphore initialization before use */
 ...
 chSemWait(&sem);
 /* Protected code */
 chSemSignal(&sem);
 ...

Mutual exclusion by Mutexes

The mutexes are the mechanism intended as the most general solution for Mutual Exclusion.

Advantages

Disadvantages

When use Mutexes

Example

 static Mutex mtx; /* Mutex declaration */
 ...
 chMtxInit(&mtx); /* Mutex initialization before use */
 ...
 chMtxLock(&mtx);
 /* Protected code */
 chMtxUnlock();
 ...

Mutual exclusion by priority boost

Another way to implement mutual exclusion is to boost the thread priority to a level higher than all of the threads competing for a certain resource. This solution effectively implements an Immediate Priority Ceiling algorithm.

Advantages

Disadvantages

Example

 /* Priority assigned to the resource, threads must have lower
    priority than this.*/
 #define AAA_RESOURCE_PRIORITY NORMALPRIO+10
 ...
 /* Locks the resources AAA.*/
 tprio_t aaa_old_prio = chThdSetPriority(AAA_RESOURCE_PRIORITY);
 /* Accessing resource AAA */
 chThdSetPriority(aaa_old_prio); /* Unlocks AAA.*/
 ...

Mutual exclusion by message passing

Another method is to make a single dedicated thread execute the critical code and make it work as a messages server. The other threads can request the service to the server by sending a properly formatted message and then wait for the answer with the result.
This method is very useful when integrating into the system components not designed to be reentrant or to be executed in a multithreaded environment, as example a 3rd part file system or a networking protocol stack.

Advantages

Disadvantages


Generated on Sun Oct 24 2010 09:40:45 for ChibiOS/RT by doxygen 1.7.1