LUMIERA.clone/src/lib/condition.h

208 lines
5.4 KiB
C
Raw Normal View History

2007-09-02 00:52:40 +02:00
/*
2007-09-02 13:56:33 +02:00
condition.h - condition variables
2007-09-02 00:52:40 +02:00
Copyright (C) Lumiera.org
2008, Christian Thaeter <ct@pipapo.org>
2007-09-02 00:52:40 +02:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
2008-03-10 06:09:44 +01:00
#ifndef LUMIERA_CONDITION_H
#define LUMIERA_CONDITION_H
2007-09-02 00:52:40 +02:00
#include "lib/locking.h"
2007-09-02 00:52:40 +02:00
/**
* @file Condition variables, header
*/
LUMIERA_ERROR_DECLARE (CONDITION_DESTROY);
2007-09-02 00:52:40 +02:00
/**
* Condition variables.
*
*/
2008-03-10 06:09:44 +01:00
struct lumiera_condition_struct
2007-09-02 00:52:40 +02:00
{
pthread_cond_t cond;
pthread_mutex_t mutex;
};
2008-03-10 06:09:44 +01:00
typedef struct lumiera_condition_struct lumiera_condition;
typedef lumiera_condition* LumieraCondition;
2007-09-02 00:52:40 +02:00
2008-03-10 06:09:44 +01:00
LumieraCondition
lumiera_condition_init (LumieraCondition self);
2007-09-02 00:52:40 +02:00
2008-03-10 06:09:44 +01:00
LumieraCondition
lumiera_condition_destroy (LumieraCondition self);
2007-09-02 00:52:40 +02:00
/**
* signal a single waiting thread.
* @param self condition variable to be signaled, must be given, all errors are fatal
*/
static inline void
2008-03-10 06:09:44 +01:00
lumiera_condition_signal (LumieraCondition self)
2007-09-02 00:52:40 +02:00
{
REQUIRE (self);
if (pthread_mutex_lock (&self->mutex))
LUMIERA_DIE (MUTEX_LOCK);
2007-09-02 00:52:40 +02:00
pthread_cond_signal (&self->cond);
if (pthread_mutex_unlock (&self->mutex))
LUMIERA_DIE (MUTEX_UNLOCK);
2007-09-02 00:52:40 +02:00
}
/**
* signal all waiting threads
* @param self condition variable to be signaled, must be given, all errors are fatal
*/
static inline void
2008-03-10 06:09:44 +01:00
lumiera_condition_broadcast (LumieraCondition self)
2007-09-02 00:52:40 +02:00
{
REQUIRE (self);
if (pthread_mutex_lock (&self->mutex))
LUMIERA_DIE (MUTEX_LOCK);
2007-09-02 00:52:40 +02:00
pthread_cond_broadcast (&self->cond);
if (pthread_mutex_unlock (&self->mutex))
LUMIERA_DIE (MUTEX_UNLOCK);
2007-09-02 00:52:40 +02:00
}
/**
2007-09-03 06:40:44 +02:00
* conditionacquirer used to manage the state of a condition variable.
2007-09-02 00:52:40 +02:00
*/
2008-03-10 06:09:44 +01:00
struct lumiera_conditionacquirer_struct
2007-09-02 00:52:40 +02:00
{
2008-03-10 06:09:44 +01:00
LumieraCondition cond;
enum lumiera_lockstate state;
2007-09-02 00:52:40 +02:00
};
2008-03-10 06:09:44 +01:00
typedef struct lumiera_conditionacquirer_struct lumiera_conditionacquirer;
typedef struct lumiera_conditionacquirer_struct* LumieraConditionacquirer;
2007-09-02 00:52:40 +02:00
/* helper function for nobug */
static inline void
2008-03-10 06:09:44 +01:00
lumiera_conditionacquirer_ensureunlocked (LumieraConditionacquirer self)
2007-09-02 00:52:40 +02:00
{
2008-03-10 06:09:44 +01:00
ENSURE (self->state == LUMIERA_UNLOCKED, "forgot to unlock the condition mutex");
2007-09-02 00:52:40 +02:00
}
/* override with a macro to use the cleanup checker */
2008-03-10 06:09:44 +01:00
#define lumiera_conditionacquirer \
lumiera_conditionacquirer NOBUG_CLEANUP(lumiera_conditionacquirer_ensureunlocked)
2007-09-02 00:52:40 +02:00
/**
2007-09-03 06:40:44 +02:00
* initialize a conditionacquirer state
* @param self conditionacquirer to be initialized, must be an automatic variable
2007-09-02 00:52:40 +02:00
* @param cond associated condition variable
2008-03-10 06:09:44 +01:00
* @param state initial state of the mutex, either LUMIERA_LOCKED or LUMIERA_UNLOCKED
2007-09-02 00:52:40 +02:00
* @return self as given
* errors are fatal
*/
2008-03-10 06:09:44 +01:00
static inline LumieraConditionacquirer
lumiera_conditionacquirer_init (LumieraConditionacquirer self, LumieraCondition cond, enum lumiera_lockstate state)
2007-09-02 00:52:40 +02:00
{
REQUIRE (self);
REQUIRE (cond);
self->cond = cond;
self->state = state;
2008-03-10 06:09:44 +01:00
if (state == LUMIERA_LOCKED)
2007-09-02 00:52:40 +02:00
if (pthread_mutex_lock (&cond->mutex))
LUMIERA_DIE (MUTEX_LOCK);
2007-09-02 00:52:40 +02:00
return self;
}
/**
* lock the mutex.
* must not already be locked
2007-09-03 06:40:44 +02:00
* @param self conditionacquirer associated with a condition variable
2007-09-02 00:52:40 +02:00
*/
static inline void
2008-03-10 06:09:44 +01:00
lumiera_conditionacquirer_lock (LumieraConditionacquirer self)
2007-09-02 00:52:40 +02:00
{
REQUIRE (self);
2008-03-10 06:09:44 +01:00
REQUIRE (self->state == LUMIERA_UNLOCKED, "mutex already locked");
2007-09-02 00:52:40 +02:00
if (pthread_mutex_lock (&self->cond->mutex))
LUMIERA_DIE (MUTEX_LOCK);
2007-09-02 00:52:40 +02:00
2008-03-10 06:09:44 +01:00
self->state = LUMIERA_LOCKED;
2007-09-02 00:52:40 +02:00
}
/**
* wait on a locked condition.
* Waits until the condition variable gets signaled from another thread. Must already be locked.
2007-09-03 06:40:44 +02:00
* @param self conditionacquirer associated with a condition variable
2007-09-02 00:52:40 +02:00
*/
static inline void
2008-03-10 06:09:44 +01:00
lumiera_conditionacquirer_wait (LumieraConditionacquirer self)
2007-09-02 00:52:40 +02:00
{
REQUIRE (self);
2008-03-10 06:09:44 +01:00
REQUIRE (self->state == LUMIERA_LOCKED, "mutex must be locked");
2007-09-02 00:52:40 +02:00
pthread_cond_wait (&self->cond->cond, &self->cond->mutex);
}
/**
* release mutex.
2007-09-03 06:40:44 +02:00
* a conditionacquirer must be unlocked before leaving scope
* @param self conditionacquirer associated with a condition variable
2007-09-02 00:52:40 +02:00
*/
2008-04-10 06:20:51 +02:00
static inline void
2008-03-10 06:09:44 +01:00
lumiera_conditionacquirer_unlock (LumieraConditionacquirer self)
2007-09-02 00:52:40 +02:00
{
REQUIRE (self);
2008-03-10 06:09:44 +01:00
REQUIRE (self->state == LUMIERA_LOCKED, "mutex was not locked");
2007-09-02 00:52:40 +02:00
if (pthread_mutex_unlock (&self->cond->mutex))
LUMIERA_DIE (MUTEX_UNLOCK);
2008-03-10 06:09:44 +01:00
self->state = LUMIERA_UNLOCKED;
2007-09-02 00:52:40 +02:00
}
/**
* signal a single waiting thread
2007-09-03 06:40:44 +02:00
* @param self conditionacquirer associated with the condition variable to be signaled
2007-09-02 00:52:40 +02:00
*/
static inline void
2008-03-10 06:09:44 +01:00
lumiera_conditionacquirer_signal (LumieraConditionacquirer self)
2007-09-02 00:52:40 +02:00
{
REQUIRE (self);
2008-03-10 06:09:44 +01:00
REQUIRE (self->state == LUMIERA_LOCKED, "mutex was not locked");
2007-09-02 00:52:40 +02:00
pthread_cond_signal (&self->cond->cond);
}
/**
* signal all waiting threads
2007-09-03 06:40:44 +02:00
* @param self conditionacquirer associated with the condition variable to be signaled
2007-09-02 00:52:40 +02:00
*/
2008-04-10 06:20:51 +02:00
static inline void
2008-03-10 06:09:44 +01:00
lumiera_conditionacquirer_broadcast (LumieraConditionacquirer self)
2007-09-02 00:52:40 +02:00
{
REQUIRE (self);
2008-03-10 06:09:44 +01:00
REQUIRE (self->state == LUMIERA_LOCKED, "mutex was not locked");
2007-09-02 00:52:40 +02:00
pthread_cond_broadcast (&self->cond->cond);
}
#endif