lib/condition.h File Reference


Detailed Description

Condition variables, header.

Definition in file condition.h.

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

Include dependency graph for condition.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  lumiera_condition_struct
 Condition variables. More...

Defines

#define LUMIERA_CONDITION_BROADCAST
 Broadcast a condition variable Must be used inside a CONDITION_SECTION.
#define LUMIERA_CONDITION_SECTION(nobugflag, cnd)
 Condition section.
#define LUMIERA_CONDITION_SECTION_CHAIN(nobugflag, cnd)
#define LUMIERA_CONDITION_SECTION_UNLOCK   LUMIERA_SECTION_UNLOCK_(&lumiera_lock_section_)
#define LUMIERA_CONDITION_SIGNAL
 Signal a condition variable Must be used inside a CONDITION_SECTION.
#define LUMIERA_CONDITION_TIMEDWAIT(expr, timeout)
 Timed wait for a condition.
#define LUMIERA_CONDITION_WAIT(expr)
 Wait for a condition.

Typedefs

typedef struct
lumiera_condition_struct 
lumiera_condition
typedef lumiera_conditionLumieraCondition

Functions

static void lumiera_condition_broadcast (LumieraCondition self, struct nobug_flag *flag, const struct nobug_context ctx)
LumieraCondition lumiera_condition_destroy (LumieraCondition self, struct nobug_flag *flag, const struct nobug_context ctx)
 Destroy a condition variable.
LumieraCondition lumiera_condition_init (LumieraCondition self, const char *purpose, struct nobug_flag *flag, const struct nobug_context ctx)
 Initialize a condition variable.
static LumieraCondition lumiera_condition_lock (LumieraCondition self, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)
static void lumiera_condition_signal (LumieraCondition self, struct nobug_flag *flag, const struct nobug_context ctx)
static LumieraCondition lumiera_condition_timedlock (LumieraCondition self, const struct timespec *timeout, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)
static LumieraCondition lumiera_condition_timedwait (LumieraCondition self, const struct timespec *timeout, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)
static LumieraCondition lumiera_condition_trylock (LumieraCondition self, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)
static void lumiera_condition_unlock (LumieraCondition self, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)
static LumieraCondition lumiera_condition_wait (LumieraCondition self, struct nobug_flag *flag, struct nobug_resource_user **handle, const struct nobug_context ctx)


Define Documentation

#define LUMIERA_CONDITION_SECTION ( nobugflag,
cnd   ) 

Value:

for (lumiera_sectionlock NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked)    \
         lumiera_lock_section_ = {                                              \
         cnd, (lumiera_sectionlock_unlock_fn) lumiera_condition_unlock          \
           NOBUG_ALPHA_COMMA(&NOBUG_FLAG(nobugflag)) NOBUG_ALPHA_COMMA_NULL};   \
       ({                                                                       \
         if (lumiera_lock_section_.lock)                                        \
           lumiera_lock_section_.lock =                                         \
             lumiera_condition_lock (cnd, &NOBUG_FLAG(nobugflag),               \
                                     &lumiera_lock_section_.rh, NOBUG_CONTEXT); \
         lumiera_lock_section_.lock;                                            \
       });                                                                      \
       ({                                                                       \
         LUMIERA_CONDITION_SECTION_UNLOCK;                                      \
       }))
Condition section.

Locks the condition mutex, one can use LUMIERA_CONDITION_WAIT to wait for signals or LUMIERA_CONDITION_SIGNAL or LUMIERA_CONDITION_BROADCAST to wake waiting threads Condition variables must be at the end of locking chains, they can not be used at intermediate position.

Parameters:
nobugflag NoBug flag used to log actions on the condition
cnd Condition variable to be locked

Definition at line 49 of file condition.h.

Referenced by lumiera_thread_destroy(), lumiera_thread_join(), lumiera_thread_run(), lumiera_thread_sync_other(), lumiera_threadpool_acquire_thread(), and lumiera_threadpool_release_thread().

#define LUMIERA_CONDITION_SECTION_CHAIN ( nobugflag,
cnd   ) 

Value:

for (lumiera_sectionlock *lumiera_lock_section_old_ = &lumiera_lock_section_,         \
         NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) lumiera_lock_section_ = {    \
         cnd, (lumiera_sectionlock_unlock_fn) lumiera_condition_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_condition_lock (cnd, &NOBUG_FLAG(nobugflag),                     \
                                       &lumiera_lock_section_.rh, NOBUG_CONTEXT);       \
             LUMIERA_SECTION_UNLOCK_(lumiera_lock_section_old_);                        \
           }                                                                            \
         lumiera_lock_section_.lock;                                                    \
       });                                                                              \
       ({                                                                               \
         LUMIERA_CONDITION_SECTION_UNLOCK;                                              \
       }))

Definition at line 66 of file condition.h.

#define LUMIERA_CONDITION_WAIT ( expr   ) 

Value:

while (!(expr)) {                                                     \
    REQUIRE (lumiera_lock_section_.lock, "Condition mutex not locked"); \
    lumiera_condition_wait (lumiera_lock_section_.lock,                 \
                            lumiera_lock_section_.flag,                 \
                            &lumiera_lock_section_.rh,                  \
                            NOBUG_CONTEXT);                             \
  } while (!(expr))
Wait for a condition.

Must be used inside a CONDITION_SECTION.

Parameters:
expr Conditon which must become true, else the condition variable goes back into sleep

Definition at line 96 of file condition.h.

Referenced by lumiera_thread_join(), lumiera_thread_sync_other(), and lumiera_threadpool_acquire_thread().

#define LUMIERA_CONDITION_TIMEDWAIT ( expr,
timeout   ) 

Value:

while (!(expr)) {                                                     \
    REQUIRE (lumiera_lock_section_.lock, "Condition mutex not locked"); \
    if (!lumiera_condition_timedwait (lumiera_lock_section_.lock,       \
                                      timeout,                          \
                                      lumiera_lock_section_.flag,       \
                                      &lumiera_lock_section_.rh,        \
                                      NOBUG_CONTEXT))                   \
      break;                                                            \
  }
Timed wait for a condition.

Must be used inside a CONDITION_SECTION.

Parameters:
expr Conditon which must become true, else the condition variable goes back into sleep
timeout time when the wait expired sets LUMIERA_ERROR_LOCK_TIMEOUT when the timeout passed

Definition at line 113 of file condition.h.

#define LUMIERA_CONDITION_SIGNAL

Value:

do {                                                                  \
    REQUIRE (lumiera_lock_section_.lock, "Condition mutex not locked"); \
    lumiera_condition_signal (lumiera_lock_section_.lock,               \
                              lumiera_lock_section_.flag,               \
                              NOBUG_CONTEXT);                           \
  } while (0)
Signal a condition variable Must be used inside a CONDITION_SECTION.

Wakes one thread waiting on the condition variable

Definition at line 129 of file condition.h.

Referenced by lumiera_thread_destroy(), lumiera_thread_join(), lumiera_thread_run(), and lumiera_thread_sync_other().

#define LUMIERA_CONDITION_BROADCAST

Value:

do {                                                                  \
    REQUIRE (lumiera_lock_section_.lock, "Condition mutex not locked"); \
    lumiera_condition_broadcast (lumiera_lock_section_.lock,            \
                                 lumiera_lock_section_.flag,            \
                                 NOBUG_CONTEXT);                        \
  } while (0)
Broadcast a condition variable Must be used inside a CONDITION_SECTION.

Wakes all threads waiting on the condition variable

Definition at line 143 of file condition.h.

Referenced by lumiera_threadpool_release_thread().


Function Documentation

LumieraCondition lumiera_condition_init ( LumieraCondition  self,
const char *  purpose,
struct nobug_flag *  flag,
const struct nobug_context  ctx 
)

Initialize a condition variable.

Parameters:
self is a pointer to the condition variable to be initialized
Returns:
self as given

Definition at line 31 of file condition.c.

Referenced by lumiera_thread_new(), and lumiera_threadpool_init().

Here is the caller graph for this function:

LumieraCondition lumiera_condition_destroy ( LumieraCondition  self,
struct nobug_flag *  flag,
const struct nobug_context  ctx 
)

Destroy a condition variable.

Parameters:
self is a pointer to the condition variable to be destroyed
Returns:
self as given

Definition at line 50 of file condition.c.

References LUMIERA_DIE.

Referenced by lumiera_thread_destroy().

Here is the caller graph for this function:


Generated on Sun Aug 1 21:31:11 2010 for Lumiera by  doxygen 1.5.6