ZestCode
 
Loading...
Searching...
No Matches
mutex_create_static

semphr. h

sem_t mutex_create_static( static_sem_s_t *pxMutexBuffer )

Creates a new mutex type semaphore instance, and returns a handle by which the new mutex can be referenced.

Internally, within the FreeRTOS implementation, mutex semaphores use a block of memory, in which the mutex structure is stored. If a mutex is created using mutex_create() then the required memory is automatically dynamically allocated inside the mutex_create() function. (see http://www.freertos.org/a00111.html). If a mutex is created using mutex_create_static() then the application writer must provided the memory. mutex_create_static() therefore allows a mutex to be created without using any dynamic memory allocation.

Mutexes created using this function can be accessed using the sem_wait() and sem_post() macros. The mutex_recursive_take() and mutex_recursive_give() macros must not be used.

This type of semaphore uses a priority inheritance mechanism so a task 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the semaphore it is no longer required.

Mutex type semaphores cannot be used from within interrupt service routines.

See sem_binary_create() for an alternative implementation that can be used for pure synchronisation (where one task or interrupt always 'gives' the semaphore and another always 'takes' the semaphore) and from within interrupt service routines.

Parameters
pxMutexBufferMust point to a variable of type static_sem_s_t, which will be used to hold the mutex's data structure, removing the need for the memory to be allocated dynamically.
Returns
If the mutex was successfully created then a handle to the created mutex is returned. If pxMutexBuffer was NULL then NULL is returned.

Example usage:

sem_t xSemaphore;
static_sem_s_t xMutexBuffer;

void vATask( void * pvParameters )
{
   // A mutex cannot be used before it has been created.  xMutexBuffer is
   // into mutex_create_static() so no dynamic memory allocation is
   // attempted.
   xSemaphore = mutex_create_static( &xMutexBuffer );

   // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
   // so there is no need to check it.
}