2008-12-24 05:07:59 +01:00
|
|
|
/*
|
2009-01-27 10:10:03 +01:00
|
|
|
reccondition.h - recursive locked condition variables
|
2008-12-24 05:07:59 +01:00
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
2009-01-27 10:10:03 +01:00
|
|
|
2008, 2009, Christian Thaeter <ct@pipapo.org>
|
2008-12-24 05:07:59 +01: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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef LUMIERA_RECCONDITION_H
|
|
|
|
|
#define LUMIERA_RECCONDITION_H
|
|
|
|
|
|
|
|
|
|
#include "lib/error.h"
|
2009-01-27 10:10:03 +01:00
|
|
|
#include "lib/sectionlock.h"
|
2008-12-24 05:07:59 +01:00
|
|
|
|
2009-01-27 10:10:03 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <nobug.h>
|
2008-12-24 05:07:59 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file
|
2009-01-27 10:10:03 +01:00
|
|
|
* Condition variables, header
|
2008-12-24 05:07:59 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-27 10:10:03 +01:00
|
|
|
* Recursive Condition section.
|
|
|
|
|
* Locks the condition mutex, one can use LUMIERA_RECCONDITION_WAIT to wait for signals or
|
2008-12-24 05:07:59 +01:00
|
|
|
* LUMIERA_RECCONDITION_SIGNAL or LUMIERA_RECCONDITION_BROADCAST to wake waiting threads
|
2009-01-27 10:10:03 +01:00
|
|
|
* @param nobugflag NoBug flag used to log actions on the condition
|
|
|
|
|
* @param cnd Condition variable to be locked
|
2008-12-24 05:07:59 +01:00
|
|
|
*/
|
2009-01-27 10:10:03 +01:00
|
|
|
#define LUMIERA_RECCONDITION_SECTION(nobugflag, cnd) \
|
|
|
|
|
for (lumiera_sectionlock NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) \
|
|
|
|
|
lumiera_reccond_section_ = { \
|
2009-01-28 04:17:01 +01:00
|
|
|
(void*)1, lumiera_reccondition_unlock_cb NOBUG_ALPHA_COMMA_NULL NOBUG_ALPHA_COMMA_NULL}; \
|
2009-01-27 10:10:03 +01:00
|
|
|
lumiera_reccond_section_.lock;) \
|
|
|
|
|
for ( \
|
|
|
|
|
({ \
|
|
|
|
|
lumiera_reccond_section_.lock = (cnd); \
|
|
|
|
|
NOBUG_IF_ALPHA(lumiera_reccond_section_.flag = &NOBUG_FLAG(nobugflag);) \
|
|
|
|
|
RESOURCE_ENTER (nobugflag, (cnd)->rh, "acquire reccondmutex", &lumiera_reccond_section_, \
|
|
|
|
|
NOBUG_RESOURCE_WAITING, lumiera_reccond_section_.rh); \
|
|
|
|
|
if (pthread_mutex_lock (&(cnd)->reccndmutex)) \
|
|
|
|
|
LUMIERA_DIE (LOCK_ACQUIRE); \
|
|
|
|
|
RESOURCE_STATE (nobugflag, NOBUG_RESOURCE_RECURSIVE, lumiera_reccond_section_.rh); \
|
|
|
|
|
}); \
|
|
|
|
|
lumiera_reccond_section_.lock; \
|
|
|
|
|
({ \
|
|
|
|
|
LUMIERA_RECCONDITION_SECTION_UNLOCK; \
|
2008-12-24 05:07:59 +01:00
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
2009-01-27 10:10:03 +01:00
|
|
|
|
|
|
|
|
#define LUMIERA_RECCONDITION_SECTION_CHAIN(nobugflag, cnd) \
|
|
|
|
|
for (lumiera_sectionlock *lumiera_lock_section_old_ = &lumiera_lock_section_, \
|
|
|
|
|
NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) lumiera_reccond_section_ = { \
|
|
|
|
|
(void*)1, lumiera_reccondition_unlock_cb NOBUG_ALPHA_COMMA_NULL NOBUG_ALPHA_COMMA_NULL}; \
|
|
|
|
|
lumiera_reccond_section_.lock;) \
|
|
|
|
|
for ( \
|
|
|
|
|
({ \
|
|
|
|
|
REQUIRE (lumiera_lock_section_old_->lock, "section prematurely unlocked"); \
|
|
|
|
|
lumiera_reccond_section_.lock = (cnd); \
|
|
|
|
|
NOBUG_IF_ALPHA(lumiera_reccond_section_.flag = &NOBUG_FLAG(nobugflag);) \
|
|
|
|
|
RESOURCE_ENTER (nobugflag, (cnd)->rh, "acquire reccondmutex", &lumiera_reccond_section_, \
|
|
|
|
|
NOBUG_RESOURCE_WAITING, lumiera_reccond_section_.rh); \
|
|
|
|
|
if (pthread_mutex_lock (&(cnd)->reccndmutex)) \
|
|
|
|
|
LUMIERA_DIE (LOCK_ACQUIRE); \
|
|
|
|
|
RESOURCE_STATE (nobugflag, NOBUG_RESOURCE_RECURSIVE, lumiera_reccond_section_.rh); \
|
|
|
|
|
LUMIERA_SECTION_UNLOCK_(lumiera_lock_section_old_) \
|
|
|
|
|
}); \
|
|
|
|
|
lumiera_reccond_section_.lock; \
|
|
|
|
|
({ \
|
|
|
|
|
LUMIERA_RECCONDITION_SECTION_UNLOCK; \
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define LUMIERA_RECCONDITION_SECTION_UNLOCK \
|
|
|
|
|
LUMIERA_SECTION_UNLOCK_(&lumiera_reccond_section_)
|
2008-12-24 05:07:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-27 10:10:03 +01:00
|
|
|
* Wait for a condition.
|
2008-12-24 05:07:59 +01:00
|
|
|
* Must be used inside a RECCONDITION_SECTION.
|
2009-01-27 10:10:03 +01:00
|
|
|
* @param expr Conditon which must become true, else the condition variable goes back into sleep
|
2008-12-24 05:07:59 +01:00
|
|
|
*/
|
2009-01-27 10:10:03 +01:00
|
|
|
#define LUMIERA_RECCONDITION_WAIT(expr) \
|
|
|
|
|
do { \
|
|
|
|
|
REQUIRE (lumiera_reccond_section_.lock, "Condition recmutex not locked"); \
|
|
|
|
|
NOBUG_RESOURCE_STATE_RAW (lumiera_reccond_section_.flag, NOBUG_RESOURCE_WAITING, lumiera_reccond_section_.rh); \
|
2009-01-28 04:17:01 +01:00
|
|
|
pthread_cond_wait (&((LumieraReccondition)lumiera_reccond_section_.lock)->cond, \
|
|
|
|
|
&((LumieraReccondition)lumiera_reccond_section_.lock)->reccndmutex); \
|
2009-01-27 10:10:03 +01:00
|
|
|
NOBUG_RESOURCE_STATE_RAW (lumiera_reccond_section_.flag, NOBUG_RESOURCE_RECURSIVE, lumiera_reccond_section_.rh); \
|
2008-12-24 05:07:59 +01:00
|
|
|
} while (!(expr))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-27 10:10:03 +01:00
|
|
|
* Signal a condition variable
|
2008-12-24 05:07:59 +01:00
|
|
|
* Must be used inside a RECCONDITION_SECTION.
|
|
|
|
|
* Wakes one thread waiting on the condition variable
|
|
|
|
|
*/
|
2009-01-27 10:10:03 +01:00
|
|
|
#define LUMIERA_RECCONDITION_SIGNAL \
|
|
|
|
|
do { \
|
|
|
|
|
REQUIRE (lumiera_reccond_section_.lock, "Condition recmutex not locked"); \
|
|
|
|
|
TRACE(NOBUG_FLAG_RAW(lumiera_reccond_section_.flag), "Signal %p", &lumiera_reccond_section_); \
|
2009-01-28 04:17:01 +01:00
|
|
|
pthread_cond_signal (&((LumieraReccondition)lumiera_reccond_section_.lock)->cond); \
|
2009-01-27 10:10:03 +01:00
|
|
|
} while (0)
|
2008-12-24 05:07:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-27 10:10:03 +01:00
|
|
|
* Broadcast a condition variable
|
2008-12-24 05:07:59 +01:00
|
|
|
* Must be used inside a RECCONDITION_SECTION.
|
2009-01-27 10:10:03 +01:00
|
|
|
* Wakes all threads waiting on the condition variable
|
2008-12-24 05:07:59 +01:00
|
|
|
*/
|
2009-01-27 10:10:03 +01:00
|
|
|
#define LUMIERA_RECCONDITION_BROADCAST \
|
|
|
|
|
do { \
|
|
|
|
|
REQUIRE (lumiera_reccond_section_.lock, "Condition recmutex not locked"); \
|
|
|
|
|
TRACE(NOBUG_FLAG_RAW(lumiera_reccond_section_.flag), "Broadcast %p", &lumiera_reccond_section_); \
|
2009-01-28 04:17:01 +01:00
|
|
|
pthread_cond_broadcast (&((LumieraReccondition)lumiera_reccond_section_.lock)->cond); \
|
2009-01-27 10:10:03 +01:00
|
|
|
} while (0)
|
2008-12-24 05:07:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-27 10:10:03 +01:00
|
|
|
* Condition variables. Recursive mutex variant.
|
2008-12-24 05:07:59 +01:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
struct lumiera_reccondition_struct
|
|
|
|
|
{
|
|
|
|
|
pthread_cond_t cond;
|
2009-01-27 10:10:03 +01:00
|
|
|
pthread_mutex_t reccndmutex;
|
2008-12-24 05:07:59 +01:00
|
|
|
RESOURCE_HANDLE (rh);
|
|
|
|
|
};
|
|
|
|
|
typedef struct lumiera_reccondition_struct lumiera_reccondition;
|
|
|
|
|
typedef lumiera_reccondition* LumieraReccondition;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-27 10:10:03 +01:00
|
|
|
* Initialize a condition variable
|
|
|
|
|
* @param self is a pointer to the condition variable to be initialized
|
2008-12-24 05:07:59 +01:00
|
|
|
* @return self as given
|
|
|
|
|
*/
|
|
|
|
|
LumieraReccondition
|
|
|
|
|
lumiera_reccondition_init (LumieraReccondition self, const char* purpose, struct nobug_flag* flag);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-27 10:10:03 +01:00
|
|
|
* Destroy a condition variable
|
|
|
|
|
* @param self is a pointer to the condition variable to be destroyed
|
2008-12-24 05:07:59 +01:00
|
|
|
* @return self as given
|
|
|
|
|
*/
|
|
|
|
|
LumieraReccondition
|
|
|
|
|
lumiera_reccondition_destroy (LumieraReccondition self, struct nobug_flag* flag);
|
|
|
|
|
|
|
|
|
|
|
2009-01-27 10:10:03 +01:00
|
|
|
int
|
|
|
|
|
lumiera_reccondition_unlock_cb (void* cond);
|
2008-12-24 05:07:59 +01:00
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
/*
|
|
|
|
|
// Local Variables:
|
|
|
|
|
// mode: C
|
|
|
|
|
// c-file-style: "gnu"
|
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
|
// End:
|
|
|
|
|
*/
|