Design Pattern Examples
Overview of object-oriented design patterns
mutex.c File Reference

Implementation of the functions used for working with the Mutex structure that deal with mutexes. More...

#include <stdlib.h>
#include <stdio.h>
#include <threads.h>
#include "mutex.h"
Include dependency graph for mutex.c:

Go to the source code of this file.

Macros

#define MUTEX_TYPE   mtx_t
 

Functions

static MUTEX_TYPE _get_mutex_handle (void *handle)
 Helper function to retrieve the mutex handle from the given memory pointer.
 
static void * _allocate_mutex_handle (MUTEX_TYPE handle)
 Helper function to allocate memory from the heap in which to store the given mutex handle. Returns the pointer to the memory containing the mutex handle.
 
static void _deallocate_mutex_handle (void *handle)
 Helper function to deallocate memory that contained a mutex handle. The mutex handle itself should be destroyed through normal means.
 
bool mutex_create (Mutex *mutex)
 Create a new mutex, which is initially not owned.
 
bool mutex_destroy (Mutex *mutex)
 Destroy a previously created mutex.
 
bool mutex_lock (Mutex *mutex)
 Lock a previously created and unlocked mutex. This will block if the mutex is already locked by some other thread.
 
bool mutex_unlock (Mutex *mutex)
 Unlock a previously locked mutex.
 

Detailed Description

Implementation of the functions used for working with the Mutex structure that deal with mutexes.

Definition in file mutex.c.

Macro Definition Documentation

◆ MUTEX_TYPE

#define MUTEX_TYPE   mtx_t

Definition at line 14 of file mutex.c.

Function Documentation

◆ _allocate_mutex_handle()

static void * _allocate_mutex_handle ( MUTEX_TYPE  handle)
static

Helper function to allocate memory from the heap in which to store the given mutex handle. Returns the pointer to the memory containing the mutex handle.

Parameters
handleThe handle to the mutex to store.
Returns
Returns pointer to the memory containing the mutex handle.

Definition at line 46 of file mutex.c.

References MUTEX_TYPE.

Referenced by mutex_create().

◆ _deallocate_mutex_handle()

static void _deallocate_mutex_handle ( void *  handle)
static

Helper function to deallocate memory that contained a mutex handle. The mutex handle itself should be destroyed through normal means.

Parameters
handlePointer to the memory holding a mutex handle.

Definition at line 61 of file mutex.c.

Referenced by mutex_destroy().

◆ _get_mutex_handle()

static MUTEX_TYPE _get_mutex_handle ( void *  handle)
static

Helper function to retrieve the mutex handle from the given memory pointer.

Parameters
handlePointer to the memory containing the mutex handle. Cannot be NULL.
Returns
Returns the handle from the memory pointer.

Definition at line 34 of file mutex.c.

References MUTEX_TYPE.

Referenced by mutex_destroy(), mutex_lock(), and mutex_unlock().

◆ mutex_create()

bool mutex_create ( Mutex mutex)

Create a new mutex, which is initially not owned.

Parameters
mutexA Mutex object in which the mutex is created.
Returns
Returns true if the mutex was created; otherwise, returns false.

Definition at line 71 of file mutex.c.

References _allocate_mutex_handle(), and Mutex::handle.

Referenced by _CreateMutex().

◆ mutex_destroy()

bool mutex_destroy ( Mutex mutex)

Destroy a previously created mutex.

Parameters
mutexA Mutex object to destroy.
Returns
Returns true if the mutex was destroyed; otherwise, returns false.

Definition at line 128 of file mutex.c.

References _deallocate_mutex_handle(), _get_mutex_handle(), and Mutex::handle.

Referenced by _DestroyMutex().

◆ mutex_lock()

bool mutex_lock ( Mutex mutex)

Lock a previously created and unlocked mutex. This will block if the mutex is already locked by some other thread.

Parameters
mutexA Mutex object to lock.
Returns
Returns true if the mutex was locked; otherwise, returns false.

Definition at line 160 of file mutex.c.

References _get_mutex_handle(), and Mutex::handle.

Referenced by _LockMutex().

◆ mutex_unlock()

bool mutex_unlock ( Mutex mutex)

Unlock a previously locked mutex.

Parameters
mutexA Mutex object to unlock.
Returns
Returns true if the mutex was unlocked; otherwise, returns false.

Definition at line 197 of file mutex.c.

References _get_mutex_handle(), and Mutex::handle.

Referenced by _UnlockMutex().