C wraper for condition variables
This commit is contained in:
parent
194d7810f4
commit
754b9cc24e
5 changed files with 296 additions and 3 deletions
|
|
@ -25,11 +25,14 @@ libcin3_a_SOURCES = \
|
|||
$(libcin3_a_srcdir)/plugin.c \
|
||||
$(libcin3_a_srcdir)/error.c \
|
||||
$(libcin3_a_srcdir)/time.c \
|
||||
$(libcin3_a_srcdir)/framerate.c
|
||||
$(libcin3_a_srcdir)/framerate.c \
|
||||
$(libcin3_a_srcdir)/locking.c
|
||||
|
||||
|
||||
noinst_HEADERS += \
|
||||
$(libcin3_a_srcdir)/plugin.h \
|
||||
$(libcin3_a_srcdir)/error.h \
|
||||
$(libcin3_a_srcdir)/time.h \
|
||||
$(libcin3_a_srcdir)/framerate.h
|
||||
$(libcin3_a_srcdir)/framerate.h \
|
||||
$(libcin3_a_srcdir)/locking.h
|
||||
|
||||
|
|
|
|||
52
src/lib/locking.c
Normal file
52
src/lib/locking.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
locking.c - locking primitives
|
||||
|
||||
Copyright (C) CinelerraCV
|
||||
2007, Christian Thaeter <ct@pipapo.org>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include "lib/locking.h"
|
||||
|
||||
CinelerraCondition
|
||||
cinelerra_condition_init (CinelerraCondition self)
|
||||
{
|
||||
if (self)
|
||||
{
|
||||
pthread_cond_init (&self->cond, NULL);
|
||||
pthread_mutex_init (&self->mutex, NULL);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
CinelerraCondition
|
||||
cinelerra_condition_destroy (CinelerraCondition self)
|
||||
{
|
||||
if (self)
|
||||
{
|
||||
if (pthread_mutex_destroy (&self->mutex))
|
||||
CINELERRA_DIE;
|
||||
else if (pthread_cond_destroy (&self->cond))
|
||||
CINELERRA_DIE;
|
||||
else
|
||||
return self;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
226
src/lib/locking.h
Normal file
226
src/lib/locking.h
Normal file
|
|
@ -0,0 +1,226 @@
|
|||
/*
|
||||
locking.h - locking primitives
|
||||
|
||||
Copyright (C) CinelerraCV
|
||||
2007, Christian Thaeter <ct@pipapo.org>
|
||||
|
||||
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 CINELERRA_LOCKING_H
|
||||
#define CINELERRA_LOCKING_H
|
||||
|
||||
#include <pthread.h>
|
||||
#include <nobug.h>
|
||||
|
||||
#include "lib/error.h"
|
||||
|
||||
/**
|
||||
* used to store the current lock state.
|
||||
*
|
||||
*
|
||||
*/
|
||||
enum cinelerra_lockstate
|
||||
{
|
||||
CINELERRA_UNLOCKED,
|
||||
CINELERRA_LOCKED
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Condition variables.
|
||||
*
|
||||
*/
|
||||
struct cinelerra_condition_struct
|
||||
{
|
||||
pthread_cond_t cond;
|
||||
pthread_mutex_t mutex;
|
||||
};
|
||||
typedef struct cinelerra_condition_struct cinelerra_condition;
|
||||
typedef cinelerra_condition* CinelerraCondition;
|
||||
|
||||
|
||||
/**
|
||||
* Initialize a condition variable
|
||||
* @param self is a pointer to the condition variable to be initialized
|
||||
* @return self as given
|
||||
*/
|
||||
CinelerraCondition
|
||||
cinelerra_condition_init (CinelerraCondition self);
|
||||
|
||||
|
||||
/**
|
||||
* destroy a condition variable
|
||||
* @param self is a pointer to the condition variable to be initialized
|
||||
* @return self on success or NULL at error
|
||||
*/
|
||||
CinelerraCondition
|
||||
cinelerra_condition_destroy (CinelerraCondition self);
|
||||
|
||||
|
||||
/**
|
||||
* signal a single waiting thread.
|
||||
* @param self condition variable to be signaled, must be given, all errors are fatal
|
||||
*/
|
||||
static inline void
|
||||
cinelerra_condition_signal (CinelerraCondition self)
|
||||
{
|
||||
REQUIRE (self);
|
||||
if (pthread_mutex_lock (&self->mutex))
|
||||
CINELERRA_DIE;
|
||||
pthread_cond_signal (&self->cond);
|
||||
if (pthread_mutex_unlock (&self->mutex))
|
||||
CINELERRA_DIE;
|
||||
}
|
||||
|
||||
/**
|
||||
* signal all waiting threads
|
||||
* @param self condition variable to be signaled, must be given, all errors are fatal
|
||||
*/
|
||||
static inline void
|
||||
cinelerra_condition_broadcast (CinelerraCondition self)
|
||||
{
|
||||
REQUIRE (self);
|
||||
if (pthread_mutex_lock (&self->mutex))
|
||||
CINELERRA_DIE;
|
||||
pthread_cond_broadcast (&self->cond);
|
||||
if (pthread_mutex_unlock (&self->mutex))
|
||||
CINELERRA_DIE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* conditionlock used to manage the state of a condition variable.
|
||||
*/
|
||||
struct cinelerra_conditionlock_struct
|
||||
{
|
||||
CinelerraCondition cond;
|
||||
enum cinelerra_lockstate state;
|
||||
};
|
||||
typedef struct cinelerra_conditionlock_struct cinelerra_conditionlock;
|
||||
typedef struct cinelerra_conditionlock_struct* CinelerraConditionlock;
|
||||
|
||||
/* helper function for nobug */
|
||||
static inline void
|
||||
cinelerra_conditionlock_ensureunlocked (CinelerraConditionlock self)
|
||||
{
|
||||
ENSURE (self->state == CINELERRA_UNLOCKED, "forgot to unlock the condition mutex");
|
||||
}
|
||||
|
||||
/* override with a macro to use the cleanup checker */
|
||||
#define cinelerra_conditionlock \
|
||||
cinelerra_conditionlock NOBUG_CLEANUP(cinelerra_conditionlock_ensureunlocked)
|
||||
|
||||
|
||||
/**
|
||||
* initialize a conditionlock state
|
||||
* @param self conditionlock to be initialized, must be an automatic variable
|
||||
* @param cond associated condition variable
|
||||
* @param state initial state of the mutex, either CINELERRA_LOCKED or CINELERRA_UNLOCKED
|
||||
* @return self as given
|
||||
* errors are fatal
|
||||
*/
|
||||
static inline CinelerraConditionlock
|
||||
cinelerra_conditionlock_init (CinelerraConditionlock self, CinelerraCondition cond, enum cinelerra_lockstate state)
|
||||
{
|
||||
REQUIRE (self);
|
||||
REQUIRE (cond);
|
||||
self->cond = cond;
|
||||
self->state = state;
|
||||
if (state == CINELERRA_LOCKED)
|
||||
if (pthread_mutex_lock (&cond->mutex))
|
||||
CINELERRA_DIE;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* lock the mutex.
|
||||
* must not already be locked
|
||||
* @param self conditionlock associated with a condition variable
|
||||
*/
|
||||
static inline void
|
||||
cinelerra_conditionlock_lock (CinelerraConditionlock self)
|
||||
{
|
||||
REQUIRE (self);
|
||||
REQUIRE (self->state == CINELERRA_UNLOCKED, "mutex already locked");
|
||||
|
||||
if (pthread_mutex_lock (&self->cond->mutex))
|
||||
CINELERRA_DIE;
|
||||
|
||||
self->state = CINELERRA_LOCKED;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* wait on a locked condition.
|
||||
* Waits until the condition variable gets signaled from another thread. Must already be locked.
|
||||
* @param self conditionlock associated with a condition variable
|
||||
*/
|
||||
static inline void
|
||||
cinelerra_conditionlock_wait (CinelerraConditionlock self)
|
||||
{
|
||||
REQUIRE (self);
|
||||
REQUIRE (self->state == CINELERRA_LOCKED, "mutex must be locked");
|
||||
pthread_cond_wait (&self->cond->cond, &self->cond->mutex);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* release mutex.
|
||||
* a conditionlock must be unlocked before leaving scope
|
||||
* @param self conditionlock associated with a condition variable
|
||||
*/
|
||||
static inline int
|
||||
cinelerra_conditionlock_unlock (CinelerraConditionlock self)
|
||||
{
|
||||
REQUIRE (self);
|
||||
REQUIRE (self->state == CINELERRA_LOCKED, "mutex was not locked");
|
||||
if (pthread_mutex_unlock (&self->cond->mutex))
|
||||
CINELERRA_DIE;
|
||||
self->state = CINELERRA_UNLOCKED;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* signal a single waiting thread
|
||||
* @param self conditionlock associated with the condition variable to be signaled
|
||||
*/
|
||||
static inline void
|
||||
cinelerra_conditionlock_signal (CinelerraConditionlock self)
|
||||
{
|
||||
REQUIRE (self);
|
||||
REQUIRE (self->state == CINELERRA_LOCKED, "mutex was not locked");
|
||||
pthread_cond_signal (&self->cond->cond);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* signal all waiting threads
|
||||
* @param self conditionlock associated with the condition variable to be signaled
|
||||
*/
|
||||
static inline int
|
||||
cinelerra_conditionlock_broadcast (CinelerraConditionlock self)
|
||||
{
|
||||
REQUIRE (self);
|
||||
REQUIRE (self->state == CINELERRA_LOCKED, "mutex was not locked");
|
||||
pthread_cond_broadcast (&self->cond->cond);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
8
tests/15locking.tests
Normal file
8
tests/15locking.tests
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
TESTING "Locking" ./test-locking
|
||||
|
||||
TEST "condition not unlocked asserts" conditionforgotunlock <<END
|
||||
return: 134
|
||||
END
|
||||
|
||||
|
||||
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
tests_srcdir = $(top_srcdir)/tests
|
||||
|
||||
check_PROGRAMS += test-error test-time
|
||||
check_PROGRAMS += test-error test-time test-locking
|
||||
|
||||
test_error_SOURCES = $(tests_srcdir)/error/errortest.c
|
||||
test_error_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/
|
||||
|
|
@ -28,4 +28,8 @@ test_time_SOURCES = $(tests_srcdir)/time/test-time.c
|
|||
test_time_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/
|
||||
test_time_LDADD = $(builddir)/libcin3.a -lnobugmt -lpthread -ldl -lm
|
||||
|
||||
test_locking_SOURCES = $(tests_srcdir)/locking/test-locking.c
|
||||
test_locking_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/
|
||||
test_locking_LDADD = $(builddir)/libcin3.a -lnobugmt -lpthread -ldl -lm
|
||||
|
||||
TESTS = $(tests_srcdir)/test.sh
|
||||
|
|
|
|||
Loading…
Reference in a new issue