Merge branch 'library'

This commit is contained in:
Christian Thaeter 2007-09-03 18:31:07 +02:00
commit 3dcd7c625a
12 changed files with 638 additions and 28 deletions

View file

@ -26,6 +26,8 @@ libcin3_a_SOURCES = \
$(libcin3_a_srcdir)/error.c \
$(libcin3_a_srcdir)/time.c \
$(libcin3_a_srcdir)/framerate.c \
$(libcin3_a_srcdir)/mutex.c \
$(libcin3_a_srcdir)/rwlock.c \
$(libcin3_a_srcdir)/condition.c
@ -35,5 +37,7 @@ noinst_HEADERS += \
$(libcin3_a_srcdir)/time.h \
$(libcin3_a_srcdir)/framerate.h \
$(libcin3_a_srcdir)/locking.h \
$(libcin3_a_srcdir)/mutex.h \
$(libcin3_a_srcdir)/rwlock.h \
$(libcin3_a_srcdir)/condition.h

View file

@ -91,38 +91,38 @@ cinelerra_condition_broadcast (CinelerraCondition self)
/**
* conditionlock used to manage the state of a condition variable.
* conditionacquirer used to manage the state of a condition variable.
*/
struct cinelerra_conditionlock_struct
struct cinelerra_conditionacquirer_struct
{
CinelerraCondition cond;
enum cinelerra_lockstate state;
};
typedef struct cinelerra_conditionlock_struct cinelerra_conditionlock;
typedef struct cinelerra_conditionlock_struct* CinelerraConditionlock;
typedef struct cinelerra_conditionacquirer_struct cinelerra_conditionacquirer;
typedef struct cinelerra_conditionacquirer_struct* CinelerraConditionacquirer;
/* helper function for nobug */
static inline void
cinelerra_conditionlock_ensureunlocked (CinelerraConditionlock self)
cinelerra_conditionacquirer_ensureunlocked (CinelerraConditionacquirer 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)
#define cinelerra_conditionacquirer \
cinelerra_conditionacquirer NOBUG_CLEANUP(cinelerra_conditionacquirer_ensureunlocked)
/**
* initialize a conditionlock state
* @param self conditionlock to be initialized, must be an automatic variable
* initialize a conditionacquirer state
* @param self conditionacquirer 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)
static inline CinelerraConditionacquirer
cinelerra_conditionacquirer_init (CinelerraConditionacquirer self, CinelerraCondition cond, enum cinelerra_lockstate state)
{
REQUIRE (self);
REQUIRE (cond);
@ -138,10 +138,10 @@ cinelerra_conditionlock_init (CinelerraConditionlock self, CinelerraCondition co
/**
* lock the mutex.
* must not already be locked
* @param self conditionlock associated with a condition variable
* @param self conditionacquirer associated with a condition variable
*/
static inline void
cinelerra_conditionlock_lock (CinelerraConditionlock self)
cinelerra_conditionacquirer_lock (CinelerraConditionacquirer self)
{
REQUIRE (self);
REQUIRE (self->state == CINELERRA_UNLOCKED, "mutex already locked");
@ -156,10 +156,10 @@ cinelerra_conditionlock_lock (CinelerraConditionlock self)
/**
* 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
* @param self conditionacquirer associated with a condition variable
*/
static inline void
cinelerra_conditionlock_wait (CinelerraConditionlock self)
cinelerra_conditionacquirer_wait (CinelerraConditionacquirer self)
{
REQUIRE (self);
REQUIRE (self->state == CINELERRA_LOCKED, "mutex must be locked");
@ -169,11 +169,11 @@ cinelerra_conditionlock_wait (CinelerraConditionlock self)
/**
* release mutex.
* a conditionlock must be unlocked before leaving scope
* @param self conditionlock associated with a condition variable
* a conditionacquirer must be unlocked before leaving scope
* @param self conditionacquirer associated with a condition variable
*/
static inline int
cinelerra_conditionlock_unlock (CinelerraConditionlock self)
cinelerra_conditionacquirer_unlock (CinelerraConditionacquirer self)
{
REQUIRE (self);
REQUIRE (self->state == CINELERRA_LOCKED, "mutex was not locked");
@ -185,10 +185,10 @@ cinelerra_conditionlock_unlock (CinelerraConditionlock self)
/**
* signal a single waiting thread
* @param self conditionlock associated with the condition variable to be signaled
* @param self conditionacquirer associated with the condition variable to be signaled
*/
static inline void
cinelerra_conditionlock_signal (CinelerraConditionlock self)
cinelerra_conditionacquirer_signal (CinelerraConditionacquirer self)
{
REQUIRE (self);
REQUIRE (self->state == CINELERRA_LOCKED, "mutex was not locked");
@ -198,10 +198,10 @@ cinelerra_conditionlock_signal (CinelerraConditionlock self)
/**
* signal all waiting threads
* @param self conditionlock associated with the condition variable to be signaled
* @param self conditionacquirer associated with the condition variable to be signaled
*/
static inline int
cinelerra_conditionlock_broadcast (CinelerraConditionlock self)
cinelerra_conditionacquirer_broadcast (CinelerraConditionacquirer self)
{
REQUIRE (self);
REQUIRE (self->state == CINELERRA_LOCKED, "mutex was not locked");

View file

@ -36,8 +36,8 @@ enum cinelerra_lockstate
{
CINELERRA_UNLOCKED,
CINELERRA_LOCKED,
CINELERRA_RLOCKED,
CINELERRA_WLOCKED
CINELERRA_RDLOCKED,
CINELERRA_WRLOCKED
};
#endif

44
src/lib/mutex.c Normal file
View file

@ -0,0 +1,44 @@
/*
mutex.c - mutex
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/mutex.h"
CinelerraMutex
cinelerra_mutex_init (CinelerraMutex self)
{
if (self)
{
pthread_mutex_init (&self->mutex, NULL);
}
return self;
}
CinelerraMutex
cinelerra_mutex_destroy (CinelerraMutex self)
{
if (self)
{
if (pthread_mutex_destroy (&self->mutex))
CINELERRA_DIE;
}
return self;
}

136
src/lib/mutex.h Normal file
View file

@ -0,0 +1,136 @@
/*
mutex.h - mutal exclusion locking
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_MUTEX_H
#define CINELERRA_MUTEX_H
#include "lib/locking.h"
/**
* Mutex.
*
*/
struct cinelerra_mutex_struct
{
pthread_mutex_t mutex;
};
typedef struct cinelerra_mutex_struct cinelerra_mutex;
typedef cinelerra_mutex* CinelerraMutex;
/**
* Initialize a mutex variable
* @param self is a pointer to the mutex to be initialized
* @return self as given
*/
CinelerraMutex
cinelerra_mutex_init (CinelerraMutex self);
/**
* Destroy a mutex variable
* @param self is a pointer to the mutex to be destroyed
* @return self as given
*/
CinelerraMutex
cinelerra_mutex_destroy (CinelerraMutex self);
/**
* mutexacquirer used to manage the state of a mutex variable.
*/
struct cinelerra_mutexacquirer_struct
{
CinelerraMutex mutex;
enum cinelerra_lockstate state;
};
typedef struct cinelerra_mutexacquirer_struct cinelerra_mutexacquirer;
typedef struct cinelerra_mutexacquirer_struct* CinelerraMutexacquirer;
/* helper function for nobug */
static inline void
cinelerra_mutexacquirer_ensureunlocked (CinelerraMutexacquirer self)
{
ENSURE (self->state == CINELERRA_UNLOCKED, "forgot to unlock mutex");
}
/* override with a macro to use the cleanup checker */
#define cinelerra_mutexacquirer \
cinelerra_mutexacquirer NOBUG_CLEANUP(cinelerra_mutexacquirer_ensureunlocked)
/**
* initialize a mutexacquirer state
* @param self mutexacquirer to be initialized, must be an automatic variable
* @param mutex associated mutex
* @param state initial state of the mutex, either CINELERRA_LOCKED or CINELERRA_UNLOCKED
* @return self as given
* errors are fatal
*/
static inline CinelerraMutexacquirer
cinelerra_mutexacquirer_init (CinelerraMutexacquirer self, CinelerraMutex mutex, enum cinelerra_lockstate state)
{
REQUIRE (self);
REQUIRE (mutex);
self->mutex = mutex;
self->state = state;
if (state == CINELERRA_LOCKED)
if (pthread_mutex_lock (&mutex->mutex))
CINELERRA_DIE;
return self;
}
/**
* lock the mutex.
* must not already be locked
* @param self mutexacquirer associated with a mutex variable
*/
static inline void
cinelerra_mutexacquirer_lock (CinelerraMutexacquirer self)
{
REQUIRE (self);
REQUIRE (self->state == CINELERRA_UNLOCKED, "mutex already locked");
if (pthread_mutex_lock (&self->mutex->mutex))
CINELERRA_DIE;
self->state = CINELERRA_LOCKED;
}
/**
* release mutex.
* a mutexacquirer must be unlocked before leaving scope
* @param self mutexacquirer associated with a mutex variable
*/
static inline int
cinelerra_mutexacquirer_unlock (CinelerraMutexacquirer self)
{
REQUIRE (self);
REQUIRE (self->state == CINELERRA_LOCKED, "mutex was not locked");
if (pthread_mutex_unlock (&self->mutex->mutex))
CINELERRA_DIE;
self->state = CINELERRA_UNLOCKED;
}
#endif

142
src/lib/rwlock.c Normal file
View file

@ -0,0 +1,142 @@
/*
rwlock.c - read/write locks
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.
*/
#define _GNU_SOURCE
#include <errno.h>
#include "lib/rwlock.h"
CINELERRA_ERROR_DEFINE(RWLOCK_AGAIN, "maximum number of readlocks exceed");
CINELERRA_ERROR_DEFINE(RWLOCK_DEADLOCK, "deadlock detected");
CinelerraRWLock
cinelerra_rwlock_init (CinelerraRWLock self)
{
if (self)
{
pthread_rwlock_init (&self->rwlock, NULL);
}
return self;
}
CinelerraRWLock
cinelerra_rwlock_destroy (CinelerraRWLock self)
{
if (self)
{
if (pthread_rwlock_destroy (&self->rwlock))
CINELERRA_DIE;
}
return self;
}
CinelerraRWLockacquirer
cinelerra_rwlockacquirer_init (CinelerraRWLockacquirer self, CinelerraRWLock rwlock, enum cinelerra_lockstate state)
{
REQUIRE (self);
REQUIRE (rwlock);
REQUIRE (state != CINELERRA_LOCKED, "illegal state for rwlock");
self->rwlock = rwlock;
self->state = state;
switch (state)
{
case CINELERRA_RDLOCKED:
switch (pthread_rwlock_rdlock (&rwlock->rwlock))
{
case 0:
break;
case EAGAIN:
cinelerra_error_set (CINELERRA_ERROR_RWLOCK_AGAIN);
return NULL;
case EDEADLK:
cinelerra_error_set (CINELERRA_ERROR_RWLOCK_DEADLOCK);
return NULL;
default:
CINELERRA_DIE;
}
case CINELERRA_WRLOCKED:
switch (pthread_rwlock_wrlock (&rwlock->rwlock))
{
case 0:
break;
case EDEADLK:
cinelerra_error_set (CINELERRA_ERROR_RWLOCK_DEADLOCK);
return NULL;
default:
CINELERRA_DIE;
}
default:
break;
}
return self;
}
CinelerraRWLockacquirer
cinelerra_rwlockacquirer_rdlock (CinelerraRWLockacquirer self)
{
REQUIRE (self);
REQUIRE (self->state == CINELERRA_UNLOCKED, "rwlock already locked");
switch (pthread_rwlock_rdlock (&self->rwlock->rwlock))
{
case 0:
break;
case EAGAIN:
cinelerra_error_set (CINELERRA_ERROR_RWLOCK_AGAIN);
return NULL;
case EDEADLK:
cinelerra_error_set (CINELERRA_ERROR_RWLOCK_DEADLOCK);
return NULL;
default:
CINELERRA_DIE;
}
self->state = CINELERRA_RDLOCKED;
return self;
}
CinelerraRWLockacquirer
cinelerra_rwlockacquirer_wrlock (CinelerraRWLockacquirer self)
{
REQUIRE (self);
REQUIRE (self->state == CINELERRA_UNLOCKED, "rwlock already locked");
switch (pthread_rwlock_wrlock (&self->rwlock->rwlock))
{
case 0:
break;
case EDEADLK:
cinelerra_error_set (CINELERRA_ERROR_RWLOCK_DEADLOCK);
return NULL;
default:
CINELERRA_DIE;
}
self->state = CINELERRA_WRLOCKED;
return self;
}

139
src/lib/rwlock.h Normal file
View file

@ -0,0 +1,139 @@
/*
rwlock.h - read/write locks
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_RWLOCK_H
#define CINELERRA_RWLOCK_H
#ifndef _GNU_SOURCE
#error "This header must be included with _GNU_SOURCE or _POSIX_C_SOURCE >= 200112L defined"
#endif
#include <pthread.h>
#include <nobug.h>
#include "lib/locking.h"
CINELERRA_ERROR_DECLARE(RWLOCK_AGAIN);
CINELERRA_ERROR_DECLARE(RWLOCK_DEADLOCK);
/**
* RWLock.
*
*/
struct cinelerra_rwlock_struct
{
pthread_rwlock_t rwlock;
};
typedef struct cinelerra_rwlock_struct cinelerra_rwlock;
typedef cinelerra_rwlock* CinelerraRWLock;
/**
* Initialize a rwlock
* @param self is a pointer to the rwlock to be initialized
* @return self as given
*/
CinelerraRWLock
cinelerra_rwlock_init (CinelerraRWLock self);
/**
* destroy a rwlock
* @param self is a pointer to the rwlock to be initialized
* @return self on success or NULL at error
*/
CinelerraRWLock
cinelerra_rwlock_destroy (CinelerraRWLock self);
/**
* rwlockacquirer used to manage the state of a rwlock variable.
*/
struct cinelerra_rwlockacquirer_struct
{
CinelerraRWLock rwlock;
enum cinelerra_lockstate state;
};
typedef struct cinelerra_rwlockacquirer_struct cinelerra_rwlockacquirer;
typedef struct cinelerra_rwlockacquirer_struct* CinelerraRWLockacquirer;
/* helper function for nobug */
static inline void
cinelerra_rwlockacquirer_ensureunlocked (CinelerraRWLockacquirer self)
{
ENSURE (self->state == CINELERRA_UNLOCKED, "forgot to unlock the rwlock mutex");
}
/* override with a macro to use the cleanup checker */
#define cinelerra_rwlockacquirer \
cinelerra_rwlockacquirer NOBUG_CLEANUP(cinelerra_rwlockacquirer_ensureunlocked)
/**
* initialize a rwlockacquirer state
* @param self rwlockacquirer to be initialized, must be an automatic variable
* @param cond associated rwlock
* @param state initial state of the mutex, either CINELERRA_RDLOCKED, CINELERRA_WRLOCKED or CINELERRA_UNLOCKED
* @return self as given or NULL on error
*/
CinelerraRWLockacquirer
cinelerra_rwlockacquirer_init (CinelerraRWLockacquirer self, CinelerraRWLock rwlock, enum cinelerra_lockstate state);
/**
* readlock the rwlock.
* must not already be locked
* @param self rwlockacquirer associated with a rwlock
* @return self as given or NULL on error
*/
CinelerraRWLockacquirer
cinelerra_rwlockacquirer_rdlock (CinelerraRWLockacquirer self);
/**
* writelock the rwlock.
* must not already be locked
* @param self rwlockacquirer associated with a rwlock
* @return self as given or NULL on error
*/
CinelerraRWLockacquirer
cinelerra_rwlockacquirer_wrlock (CinelerraRWLockacquirer self);
/**
* release rwlock.
* a rwlockacquirer must be unlocked before leaving scope
* @param self rwlockacquirer associated with a rwlock variable
*/
static inline void
cinelerra_rwlockacquirer_unlock (CinelerraRWLockacquirer self)
{
REQUIRE (self);
REQUIRE (self->state != CINELERRA_UNLOCKED, "rwlock was not locked");
if (pthread_rwlock_unlock (&self->rwlock->rwlock))
CINELERRA_DIE;
self->state = CINELERRA_UNLOCKED;
}
#endif

View file

@ -6,3 +6,11 @@ return: 134
END
TEST "mutex not unlocked asserts" mutexforgotunlock <<END
return: 134
END

View file

@ -18,7 +18,7 @@
tests_srcdir = $(top_srcdir)/tests
check_PROGRAMS += test-error test-time test-condition
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,8 +28,11 @@ 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_condition_SOURCES = $(tests_srcdir)/locking/test-condition.c
test_condition_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/
test_condition_LDADD = $(builddir)/libcin3.a -lnobugmt -lpthread -ldl -lm
test_locking_SOURCES = \
$(tests_srcdir)/locking/test-locking.c \
$(tests_srcdir)/locking/mutex.c \
$(tests_srcdir)/locking/condition.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

49
tests/locking/condition.c Normal file
View file

@ -0,0 +1,49 @@
/*
test condition functions
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/condition.h"
#if 0
waiting_thread()
{
lock;
wait;
unlock;
}
signaling_thread()
{
signal();
}
#endif
int
conditionforgotunlock ()
{
cinelerra_condition c;
cinelerra_condition_init (&c);
cinelerra_conditionacquirer l;
cinelerra_conditionacquirer_init (&l, &c, CINELERRA_LOCKED);
return 0;
}

32
tests/locking/mutex.c Normal file
View file

@ -0,0 +1,32 @@
/*
test mutex functions
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/mutex.h"
int mutexforgotunlock()
{
cinelerra_mutex m;
cinelerra_mutex_init (&m);
cinelerra_mutexacquirer l;
cinelerra_mutexacquirer_init (&l, &m, CINELERRA_LOCKED);
return 0;
}

View file

@ -0,0 +1,53 @@
/*
test-locking.c - test locking functions
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 <stdio.h>
#include <string.h>
#include "lib/error.h"
CINELERRA_ERROR_DEFINE(TEST, "test error");
int conditionforgotunlock ();
int mutexforgotunlock ();
int
main (int argc, char** argv)
{
NOBUG_INIT;
if (argc == 1)
return 0;
if (!strcmp(argv[1], "conditionforgotunlock"))
{
return conditionforgotunlock ();
}
if (!strcmp(argv[1], "mutexforgotunlock"))
{
return mutexforgotunlock ();
}
else
return 1;
return 0;
}