Lumiera  0.pre.03
»edit your freedom«
mutex.h File Reference

Go to the source code of this file.

Description

Mutual exclusion locking, header.

Definition in file mutex.h.

#include "lib/error.h"
#include "lib/sectionlock.h"
#include "lib/lockerror.h"
#include <pthread.h>
#include <time.h>
#include <nobug.h>

Classes

struct  lumiera_mutex
 Mutex. More...
 

Typedefs

typedef lumiera_mutex * LumieraMutex
 

Macros

#define LUMIERA_MUTEX_SECTION(nobugflag, mtx)
 Mutual exclusive section. More...
 
#define LUMIERA_MUTEX_SECTION_CHAIN(nobugflag, mtx)
 Mutual exclusion chainbuilder section. More...
 
#define LUMIERA_MUTEX_SECTION_UNLOCK   LUMIERA_SECTION_UNLOCK_(&lumiera_lock_section_)
 

Functions

LumieraMutex lumiera_mutex_destroy (LumieraMutex self, struct nobug_flag *flag, const struct nobug_context ctx)
 Destroy a mutex variable. More...
 
LumieraMutex lumiera_mutex_init (LumieraMutex self, const char *purpose, struct nobug_flag *flag, const struct nobug_context ctx)
 Initialise a mutex variable This initialises a 'fast' default mutex which must not be locked recursively from one thread. More...
 
static LumieraMutex lumiera_mutex_lock (LumieraMutex self, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)
 Lock a mutex variable Never fails. More...
 
static LumieraMutex lumiera_mutex_timedlock (LumieraMutex self, const struct timespec *timeout, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)
 Try to lock a mutex variable with a timeout. More...
 
static LumieraMutex lumiera_mutex_trylock (LumieraMutex self, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)
 Try to lock a mutex variable. More...
 
static void lumiera_mutex_unlock (LumieraMutex self, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)
 unlock a mutex variable Never fails More...
 

Macro Definition Documentation

◆ LUMIERA_MUTEX_SECTION

#define LUMIERA_MUTEX_SECTION (   nobugflag,
  mtx 
)
Value:
for (lumiera_sectionlock NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) \
lumiera_lock_section_ = { \
mtx, (lumiera_sectionlock_unlock_fn) lumiera_mutex_unlock \
NOBUG_ALPHA_COMMA(&NOBUG_FLAG(nobugflag)) NOBUG_ALPHA_COMMA_NULL}; \
({ \
if (lumiera_lock_section_.lock) \
lumiera_lock_section_.lock = \
lumiera_mutex_lock (mtx, &NOBUG_FLAG(nobugflag), \
&lumiera_lock_section_.rh, NOBUG_CONTEXT); \
lumiera_lock_section_.lock; \
}); \
({ \
LUMIERA_MUTEX_SECTION_UNLOCK; \
}))
static void lumiera_mutex_unlock(LumieraMutex self, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)
unlock a mutex variable Never fails
Definition: mutex.h:239

Mutual exclusive section.

Definition at line 41 of file mutex.h.

◆ LUMIERA_MUTEX_SECTION_CHAIN

#define LUMIERA_MUTEX_SECTION_CHAIN (   nobugflag,
  mtx 
)
Value:
for (lumiera_sectionlock *lumiera_lock_section_old_ = &lumiera_lock_section_, \
NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) lumiera_lock_section_ = { \
mtx, (lumiera_sectionlock_unlock_fn) lumiera_mutex_unlock \
NOBUG_ALPHA_COMMA(&NOBUG_FLAG(nobugflag)) NOBUG_ALPHA_COMMA_NULL}; \
({ \
if (lumiera_lock_section_.lock) \
{ \
REQUIRE (lumiera_lock_section_old_->lock, "section prematurely unlocked"); \
lumiera_lock_section_.lock = \
lumiera_mutex_lock (mtx, &NOBUG_FLAG(nobugflag), \
&lumiera_lock_section_.rh, NOBUG_CONTEXT); \
LUMIERA_SECTION_UNLOCK_(lumiera_lock_section_old_); \
} \
lumiera_lock_section_.lock; \
}); \
({ \
LUMIERA_MUTEX_SECTION_UNLOCK; \
}))
static void lumiera_mutex_unlock(LumieraMutex self, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)
unlock a mutex variable Never fails
Definition: mutex.h:239

Mutual exclusion chainbuilder section.

Usage: LUMIERA_MUTEX_SECTION(a){LUMIERA_MUTEX_SECTION_CHAIN(b){run();}} calls lock(a); lock(b); unlock(a); run(); unlock(b); This macro should only be used inside LUMIERA_MUTEX_SECTION and should be called on the correct mutexes, period.

Definition at line 65 of file mutex.h.

Function Documentation

◆ lumiera_mutex_init()

LumieraMutex lumiera_mutex_init ( LumieraMutex  self,
const char *  purpose,
struct nobug_flag *  flag,
const struct nobug_context  ctx 
)

Initialise a mutex variable This initialises a 'fast' default mutex which must not be locked recursively from one thread.

Parameters
selfis a pointer to the mutex to be initialised
purposetextual hint for the nobug resourcetracker
flagnobug logging target
Returns
self as given

Definition at line 30 of file mutex.c.

◆ lumiera_mutex_destroy()

LumieraMutex lumiera_mutex_destroy ( LumieraMutex  self,
struct nobug_flag *  flag,
const struct nobug_context  ctx 
)

Destroy a mutex variable.

Parameters
selfis a pointer to the mutex to be destroyed
flagnobug logging target
Returns
self as given

Definition at line 48 of file mutex.c.

References LUMIERA_DIE.

◆ lumiera_mutex_lock()

static LumieraMutex lumiera_mutex_lock ( LumieraMutex  self,
struct nobug_flag *  flag,
struct nobug_resource_user **  handle,
const struct nobug_context  ctx 
)
inlinestatic

Lock a mutex variable Never fails.

Parameters
selfis a pointer to the mutex to be destroyed
flagnobug logging target
handlepointer to nobug user handle (NULL in beta and release builds)
Returns
self as given

Definition at line 138 of file mutex.h.

References LUMIERA_DIE.

◆ lumiera_mutex_trylock()

static LumieraMutex lumiera_mutex_trylock ( LumieraMutex  self,
struct nobug_flag *  flag,
struct nobug_resource_user **  handle,
const struct nobug_context  ctx 
)
inlinestatic

Try to lock a mutex variable.

Parameters
selfis a pointer to the mutex to be destroyed
flagnobug logging target
handlepointer to nobug user handle (NULL in beta and release builds)
Returns
self as given or NULL on error, lumiera_error gets set approbiately

Definition at line 166 of file mutex.h.

References lumiera_lockerror_set().

+ Here is the call graph for this function:

◆ lumiera_mutex_timedlock()

static LumieraMutex lumiera_mutex_timedlock ( LumieraMutex  self,
const struct timespec *  timeout,
struct nobug_flag *  flag,
struct nobug_resource_user **  handle,
const struct nobug_context  ctx 
)
inlinestatic

Try to lock a mutex variable with a timeout.

Parameters
selfis a pointer to the mutex to be destroyed
timeouttimeout for waiting for the lock
flagnobug logging target
handlepointer to nobug user handle (NULL in beta and release builds)
Returns
self as given or NULL on error, lumiera_error gets set approbiately

Definition at line 202 of file mutex.h.

References lumiera_lockerror_set().

+ Here is the call graph for this function:

◆ lumiera_mutex_unlock()

static void lumiera_mutex_unlock ( LumieraMutex  self,
struct nobug_flag *  flag,
struct nobug_resource_user **  handle,
const struct nobug_context  ctx 
)
inlinestatic

unlock a mutex variable Never fails

Parameters
selfis a pointer to the mutex to be destroyed
flagnobug logging target
handlepointer to nobug user handle (NULL in beta and release builds)

Definition at line 239 of file mutex.h.

References LUMIERA_DIE.