From 0a31c7d2db41f6a5e06a557da87ce55ef6dfaa22 Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Sun, 2 Sep 2007 17:52:30 +0200 Subject: [PATCH 1/7] mutex implementation --- src/lib/mutex.c | 44 ++++++++++++ src/lib/mutex.h | 136 +++++++++++++++++++++++++++++++++++ tests/15locking.tests | 8 +++ tests/Makefile.am | 11 +-- tests/locking/condition.c | 49 +++++++++++++ tests/locking/mutex.c | 32 +++++++++ tests/locking/test-locking.c | 53 ++++++++++++++ 7 files changed, 329 insertions(+), 4 deletions(-) create mode 100644 src/lib/mutex.c create mode 100644 src/lib/mutex.h create mode 100644 tests/locking/condition.c create mode 100644 tests/locking/mutex.c create mode 100644 tests/locking/test-locking.c diff --git a/src/lib/mutex.c b/src/lib/mutex.c new file mode 100644 index 000000000..be36f58d2 --- /dev/null +++ b/src/lib/mutex.c @@ -0,0 +1,44 @@ +/* + mutex.c - mutex + + Copyright (C) CinelerraCV + 2007, Christian Thaeter + + 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; +} + diff --git a/src/lib/mutex.h b/src/lib/mutex.h new file mode 100644 index 000000000..d14f82c78 --- /dev/null +++ b/src/lib/mutex.h @@ -0,0 +1,136 @@ +/* + mutex.h - mutal exclusion locking + + Copyright (C) CinelerraCV + 2007, Christian Thaeter + + 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); + + + +/** + * mutexlock used to manage the state of a mutex variable. + */ +struct cinelerra_mutexlock_struct +{ + CinelerraMutex mutex; + enum cinelerra_lockstate state; +}; +typedef struct cinelerra_mutexlock_struct cinelerra_mutexlock; +typedef struct cinelerra_mutexlock_struct* CinelerraMutexlock; + +/* helper function for nobug */ +static inline void +cinelerra_mutexlock_ensureunlocked (CinelerraMutexlock self) +{ + ENSURE (self->state == CINELERRA_UNLOCKED, "forgot to unlock mutex"); +} + +/* override with a macro to use the cleanup checker */ +#define cinelerra_mutexlock \ +cinelerra_mutexlock NOBUG_CLEANUP(cinelerra_mutexlock_ensureunlocked) + + +/** + * initialize a mutexlock state + * @param self mutexlock 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 CinelerraMutexlock +cinelerra_mutexlock_init (CinelerraMutexlock 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 mutexlock associated with a mutex variable + */ +static inline void +cinelerra_mutexlock_lock (CinelerraMutexlock 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 mutexlock must be unlocked before leaving scope + * @param self mutexlock associated with a mutex variable + */ +static inline int +cinelerra_mutexlock_unlock (CinelerraMutexlock 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 diff --git a/tests/15locking.tests b/tests/15locking.tests index 1ef4938c5..7bea487d5 100644 --- a/tests/15locking.tests +++ b/tests/15locking.tests @@ -6,3 +6,11 @@ return: 134 END + + + +TEST "mutex not unlocked asserts" mutexforgotunlock < + + 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_conditionlock l; + cinelerra_conditionlock_init (&l, &c, CINELERRA_LOCKED); + return 0; +} diff --git a/tests/locking/mutex.c b/tests/locking/mutex.c new file mode 100644 index 000000000..3a1ff964f --- /dev/null +++ b/tests/locking/mutex.c @@ -0,0 +1,32 @@ +/* + test mutex functions + + Copyright (C) CinelerraCV + 2007, Christian Thaeter + + 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_mutexlock l; + cinelerra_mutexlock_init (&l, &m, CINELERRA_LOCKED); + return 0; +} diff --git a/tests/locking/test-locking.c b/tests/locking/test-locking.c new file mode 100644 index 000000000..78bc99779 --- /dev/null +++ b/tests/locking/test-locking.c @@ -0,0 +1,53 @@ +/* + test-locking.c - test locking functions + + Copyright (C) CinelerraCV + 2007, Christian Thaeter + + 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 +#include + +#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; +} From 04f9424ad89517e4d064d95e1ebd40bb7a68f72d Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Sun, 2 Sep 2007 23:25:38 +0200 Subject: [PATCH 2/7] forgotten to commit the Makefile.am with the last commit --- src/lib/Makefile.am | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 320a579c2..42524b322 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -26,6 +26,7 @@ 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)/condition.c @@ -35,5 +36,6 @@ 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)/condition.h From d508f7cb7a3267640f43bc44a938ee0200626525 Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Mon, 3 Sep 2007 06:35:13 +0200 Subject: [PATCH 3/7] rwlock first implementation --- src/lib/Makefile.am | 2 + src/lib/locking.h | 4 +- src/lib/rwlock.c | 142 ++++++++++++++++++++++++++++++++++++++++++++ src/lib/rwlock.h | 139 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 285 insertions(+), 2 deletions(-) create mode 100644 src/lib/rwlock.c create mode 100644 src/lib/rwlock.h diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 42524b322..46ae62363 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -27,6 +27,7 @@ libcin3_a_SOURCES = \ $(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 @@ -37,5 +38,6 @@ noinst_HEADERS += \ $(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 diff --git a/src/lib/locking.h b/src/lib/locking.h index aa722a568..5c6549f31 100644 --- a/src/lib/locking.h +++ b/src/lib/locking.h @@ -36,8 +36,8 @@ enum cinelerra_lockstate { CINELERRA_UNLOCKED, CINELERRA_LOCKED, - CINELERRA_RLOCKED, - CINELERRA_WLOCKED + CINELERRA_RDLOCKED, + CINELERRA_WRLOCKED }; #endif diff --git a/src/lib/rwlock.c b/src/lib/rwlock.c new file mode 100644 index 000000000..018c69f63 --- /dev/null +++ b/src/lib/rwlock.c @@ -0,0 +1,142 @@ +/* + rwlock.c - read/write locks + + Copyright (C) CinelerraCV + 2007, Christian Thaeter + + 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 +#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; +} + diff --git a/src/lib/rwlock.h b/src/lib/rwlock.h new file mode 100644 index 000000000..9bc07f9f3 --- /dev/null +++ b/src/lib/rwlock.h @@ -0,0 +1,139 @@ +/* + rwlock.h - read/write locks + + Copyright (C) CinelerraCV + 2007, Christian Thaeter + + 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 +#include + +#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 From f1aba12af80e589029bb0bd15f68a188fbaafec1 Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Mon, 3 Sep 2007 06:40:44 +0200 Subject: [PATCH 4/7] renamed *lock to *acquirer --- src/lib/condition.h | 44 +++++++++++++++++++-------------------- src/lib/mutex.h | 32 ++++++++++++++-------------- tests/locking/condition.c | 4 ++-- tests/locking/mutex.c | 4 ++-- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/lib/condition.h b/src/lib/condition.h index e55c8ed0b..a34ce45ff 100644 --- a/src/lib/condition.h +++ b/src/lib/condition.h @@ -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"); diff --git a/src/lib/mutex.h b/src/lib/mutex.h index d14f82c78..3110e399d 100644 --- a/src/lib/mutex.h +++ b/src/lib/mutex.h @@ -56,38 +56,38 @@ cinelerra_mutex_destroy (CinelerraMutex self); /** - * mutexlock used to manage the state of a mutex variable. + * mutexacquirer used to manage the state of a mutex variable. */ -struct cinelerra_mutexlock_struct +struct cinelerra_mutexacquirer_struct { CinelerraMutex mutex; enum cinelerra_lockstate state; }; -typedef struct cinelerra_mutexlock_struct cinelerra_mutexlock; -typedef struct cinelerra_mutexlock_struct* CinelerraMutexlock; +typedef struct cinelerra_mutexacquirer_struct cinelerra_mutexacquirer; +typedef struct cinelerra_mutexacquirer_struct* CinelerraMutexacquirer; /* helper function for nobug */ static inline void -cinelerra_mutexlock_ensureunlocked (CinelerraMutexlock self) +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_mutexlock \ -cinelerra_mutexlock NOBUG_CLEANUP(cinelerra_mutexlock_ensureunlocked) +#define cinelerra_mutexacquirer \ +cinelerra_mutexacquirer NOBUG_CLEANUP(cinelerra_mutexacquirer_ensureunlocked) /** - * initialize a mutexlock state - * @param self mutexlock to be initialized, must be an automatic variable + * 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 CinelerraMutexlock -cinelerra_mutexlock_init (CinelerraMutexlock self, CinelerraMutex mutex, enum cinelerra_lockstate state) +static inline CinelerraMutexacquirer +cinelerra_mutexacquirer_init (CinelerraMutexacquirer self, CinelerraMutex mutex, enum cinelerra_lockstate state) { REQUIRE (self); REQUIRE (mutex); @@ -103,10 +103,10 @@ cinelerra_mutexlock_init (CinelerraMutexlock self, CinelerraMutex mutex, enum ci /** * lock the mutex. * must not already be locked - * @param self mutexlock associated with a mutex variable + * @param self mutexacquirer associated with a mutex variable */ static inline void -cinelerra_mutexlock_lock (CinelerraMutexlock self) +cinelerra_mutexacquirer_lock (CinelerraMutexacquirer self) { REQUIRE (self); REQUIRE (self->state == CINELERRA_UNLOCKED, "mutex already locked"); @@ -120,11 +120,11 @@ cinelerra_mutexlock_lock (CinelerraMutexlock self) /** * release mutex. - * a mutexlock must be unlocked before leaving scope - * @param self mutexlock associated with a mutex variable + * a mutexacquirer must be unlocked before leaving scope + * @param self mutexacquirer associated with a mutex variable */ static inline int -cinelerra_mutexlock_unlock (CinelerraMutexlock self) +cinelerra_mutexacquirer_unlock (CinelerraMutexacquirer self) { REQUIRE (self); REQUIRE (self->state == CINELERRA_LOCKED, "mutex was not locked"); diff --git a/tests/locking/condition.c b/tests/locking/condition.c index 2b4e69518..72b56285c 100644 --- a/tests/locking/condition.c +++ b/tests/locking/condition.c @@ -43,7 +43,7 @@ conditionforgotunlock () cinelerra_condition c; cinelerra_condition_init (&c); - cinelerra_conditionlock l; - cinelerra_conditionlock_init (&l, &c, CINELERRA_LOCKED); + cinelerra_conditionacquirer l; + cinelerra_conditionacquirer_init (&l, &c, CINELERRA_LOCKED); return 0; } diff --git a/tests/locking/mutex.c b/tests/locking/mutex.c index 3a1ff964f..af4b563d9 100644 --- a/tests/locking/mutex.c +++ b/tests/locking/mutex.c @@ -26,7 +26,7 @@ int mutexforgotunlock() cinelerra_mutex m; cinelerra_mutex_init (&m); - cinelerra_mutexlock l; - cinelerra_mutexlock_init (&l, &m, CINELERRA_LOCKED); + cinelerra_mutexacquirer l; + cinelerra_mutexacquirer_init (&l, &m, CINELERRA_LOCKED); return 0; } From 3ce5d3cedd7a36275588d37ab5671132b8eb289b Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Wed, 5 Sep 2007 02:44:23 +0200 Subject: [PATCH 5/7] revived, beefed up and documented a old linked list implementation, UNTESTED --- src/lib/llist.h | 524 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 524 insertions(+) create mode 100644 src/lib/llist.h diff --git a/src/lib/llist.h b/src/lib/llist.h new file mode 100644 index 000000000..49d531492 --- /dev/null +++ b/src/lib/llist.h @@ -0,0 +1,524 @@ +/* + llist.h - simple intrusive cyclic double linked list + + Copyright (C) + 2003, 2005 Christian Thaeter + Copyright (C) CinelerraCV + 2007, Christian Thaeter + + 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 LLIST_H +#define LLIST_H + +#include + +/** + * @file Intrusive cyclic double linked list + * There is only one node type which contains a forward and a backward pointer. In a empty initialized node, + * this pointers point to the node itself. Note that these pointers can never ever become NULL. + * This lists are used by using one node as 'root' node where its both pointers are the head/tail pointer to the actual list. + * Care needs to be taken to ensure not to apply any operations meant to be applied to data nodes to the root node. + * This way is the prefered way to use this lists. + * Alternatively one can store only a chain of data nodes and use a LList pointer to point to the first item + * (which might be NULL in case no data is stored). When using the 2nd approach care must be taken since most functions + * below expect lists to have a root node. + * + * This header can be used in 2 different ways: + * 1) (prerefered) just including it provides all functions as static inlined functions. This is the default + * 2) #define LLIST_INTERFACE before including this header gives only the declarations + * #define LLIST_IMPLEMENTATION before including this header yields in definitions + * this can be used to generate a library. This is currently untested and not recommended. + * The rationale for using inlined functions is that most functions are very small and likely to be used in performance critical parts. + * Inlining can give a hughe performance and optimization improvement here. + * The few functions which are slightly larger are expected to be the less common used ones, so inlining them too shouldn't be a problem either + */ + + +/* TODO __STDC_VERSION__ 199901L +150) This macro was not specified in ISO/IEC 9899:1990 and was specified as 199409L in +ISO/IEC 9899/AMD1:1995. The intention is that this will remain an integer constant of type long +int that is increased with each revision of this International Standard. +*/ +#ifdef HAVE_INLINE +# define LLIST_MACRO static inline +#else +# ifdef __GNUC__ +# define LLIST_MACRO static __inline__ +# else +# define LLIST_MACRO static +# endif +#endif + +#if defined(LLIST_INTERFACE) +/* only the interface is generated */ +#define LLIST_FUNC(proto, ...) proto +#elif defined(LLIST_IMPLEMENTATION) +/* generate a non inlined implementation */ +#define LLIST_FUNC(proto, ...) proto { __VA_ARGS__ } +#else +/* all functions are macro-like inlined */ +#define LLIST_FUNC(proto, ...) LLIST_MACRO proto { __VA_ARGS__ } +#endif + + +/* + * Type of a llist node. + */ +struct llist_struct +{ + struct llist_struct *next; + struct llist_struct *prev; +}; +typedef struct llist_struct llist; +typedef llist * LList; +typedef const llist * const_LList; +typedef llist ** LList_ref; + + +/** + * cast back from a member of a structure to a pointer of the structure + */ +/* example: + struct foo + { + int bar; + llist l; + } x; + LLIST_TO_STRUCTP (&x.l, foo, l)->bar +*/ +#define LLIST_TO_STRUCTP(llist, type, member) \ + ((type*)(((char*)(llist)) - offsetof(type, member))) + +/** + * Iterate forward over a list. + * @param list the root node of the list to be iterated + * @param node pointer to the iterated node + */ +#define LLIST_FOREACH(list, node) \ + if (!list); else \ + for (LList node = llist_get_head (list); \ + ! llist_is_end (node, list); \ + llist_forward (&node)) + +/** + * Iterate backward over a list. + * @param list the root node of the list to be iterated + * @param node pointer to the iterated node + */ +#define LLIST_FOREACH_REV(list, node) \ + if (!list); else \ + for (LList node = llist_get_tail (list); \ + ! llist_is_end (node, list); \ + llist_backward (&node)) + +/** + * Consume a list from head. + * The body of this statement should remove the head from the list, else it would be a infinite loop + * @param list the root node of the list to be consumed + * @param head pointer to the head node + */ +#define LLIST_WHILE_HEAD(list, head) \ + if (!list); else \ + for (LList head = llist_get_head (list); \ + !llist_is_empty (list); \ + head = llist_get_head (list)) + +/** + * Consume a list from tail. + * The body of this statement should remove the tail from the list, else it would be a infinite loop + * @param list the root node of the list to be consumed + * @param tail pointer to the tail node + */ +#define LLIST_WHILE_TAIL(list, tail) \ + if (!list); else \ + for (LList tail = llist_get_tail (list); \ + !llist_is_empty (list); \ + tail = llist_get_tail (list)) + + +/** + * Macro to instantiate a local llist. + * @param name becomes a LList handle + * the underlying instance is hidden as name_llist_, this list is statically initialized + */ +#define LLIST_AUTO(name) \ +llist name##_llist_ = LLIST_STATIC_INITIALIZER(name##_llist_);\ +LList name = &name##_llist_ +#define LLIST_STATIC_INITIALIZER(name) {&name,&name} + + + +/** + * Initialize a new llist. + * Must not be applied to a list node which is not empty! Lists need to be initialized + * before any other operation on them is called. + * @param self node to be initialized + */ +LLIST_FUNC (void llist_init (LList self), + self->next = self->prev = self; +); + +/** + * Check if a node is not linked with some other node. + */ +LLIST_FUNC (int llist_is_empty (const_LList self), + return self->next == self; +); + +/** + * Check if self is the only node in a list or self is not in a list. + * @param self node to be checked + */ +LLIST_FUNC (int llist_is_single (const_LList self), + return self->next->next == self; +); + +/** + * Check for the head of a list. + * @param self root of the list + * @param head expected head of the list + */ +LLIST_FUNC (int llist_is_head (const_LList self, const_LList head), + return self->next == head; +); + +/** + * Check for the tail of a list. + * @param self root of the list + * @param tail expected tail of the list + */ +LLIST_FUNC (int llist_is_tail (const_LList self, const_LList tail), + return self->prev == tail; +); + +/** + * Check for the end of a list. + * The end is by definition one past the tail of a list, which is the root node itself. + * @param self root node of the list + * @param end expected end of the list + */ +LLIST_FUNC (int llist_is_end (const_LList self, const_LList end), + return self == end; +); + +/** + * Check if a node is a member of a list. + * @param self root node of the list + * @param member node to be searched + */ +LLIST_FUNC (int llist_is_member (const_LList self, const_LList member), + for (const_LList i = member->next; i != member; i = i->next) + { + if (i == self) + return 1; + } + return 0; +); + +/** + * Check the order of elements in a list. + * @param self root node of the list + * @param before expected to be before after + * @param after expected to be after before + */ +LLIST_FUNC (int llist_is_before_after (const_LList self, const_LList before, const_LList after), + for (const_LList i = before->next; i != self; i = i->next) + { + if (i == after) + return 1; + } + return 0; +); + +/** + * Count the nodes of a list. + * @param self root node of the list + * @return number of nodes in self + */ +LLIST_FUNC (unsigned llist_count (const_LList self), + unsigned cnt = 0; + const_LList i = self; + for (; i->next != self; ++cnt, i = i->next); + return cnt; +); + +/* private, unlink self some any list but leaves self in a uninitialized state */ +LLIST_FUNC (void llist_unlink_fast_ (LList self), + LList nxt = self->next, pre = self->prev; + nxt->prev = pre; + pre->next = nxt; +); + +/** + * Remove a node from a list. + * @param self node to be removed + * @return self + */ +LLIST_FUNC (LList llist_unlink (LList self), + llist_unlink_fast_ (self); + return self->next = self->prev = self; +); + +/** + * Fix a node which got relocated in memory. + * It is supported to realloc/move list nodes in memory but one must call 'list_relocate' after doing so. + * @param self node which got relocated + * @return self + */ +LLIST_FUNC (LList llist_relocate (LList self), + return self->next->prev = self->prev->next = self; +); + +/** + * Insert a node after another. + * @param self node after which we want to insert + * @param next node which shall be inserted after self. Could already linked to a list from where it will be removed. + * @return self + */ +LLIST_FUNC (LList llist_insert_next (LList self, LList next), + llist_unlink_fast_ (next); + self->next->prev = next; + next->prev = self; + next->next = self->next; + self->next = next; + return self; +); + +/** + * Insert a node before another. + * @param self node before which we want to insert + * @param prev node which shall be inserted nefore self. Could already linked to a list from where it will be removed. + * @return self + */ +LLIST_FUNC (LList llist_insert_prev (LList self, LList prev), + llist_unlink_fast_ (prev); + self->prev->next = prev; + prev->next = self; + prev->prev = self->prev; + self->prev = prev; + return self; +); + + +/** + * Move the content of a list after a node in another list. + * @param self node after which we want to insert a list + * @param next rootnode of the list which shall be inserted after self + * @return self + * next is a empty list after this call + */ +LLIST_FUNC (LList llist_insertlist_next (LList self, LList next), + if (!llist_is_empty (next)) + { + self->next->prev = next->prev; + self->next = next->next; + + next->next->prev = self; + next->prev->next = self->next; + + next->prev = next->next = next; + } + return self; +); + +/** + * Move the content of a list before a node in another list. + * @param self node before which we want to insert a list + * @param prev rootnode of the list which shall be inserted before self + * @return self + * prev is a empty list after this call + */ +LLIST_FUNC (LList llist_insertlist_prev (LList self, LList prev), + if (!llist_is_empty (prev)) + { + self->prev->next = prev->next; + self->prev = prev->prev; + + prev->prev->next = self; + prev->next->prev = self->prev; + + prev->prev = prev->next = prev; + } + return self; +); + +#if 0 //BUG("needs temporary") +/** + * Move a range of nodes after a given node. + * @param self node after which the range shall be inserted + * @param start first node in range to be moved + * @param end node after the last node of the range + */ +LLIST_FUNC (LList llist_insertafter_range (LList self, LList start, LList end), + self->next->prev = end->prev; + end->prev->next = self->next; + end->prev = start->prev; + start->prev->next = end; + self->next = start; + start->prev = self; + return self; +); +#endif + +#if 0 //BUG("needs temporary") +/** + * Move a range of nodes before a given node. + * @param self node before which the range shall be inserted + * @param start first node in range to be moved + * @param end node after the last node of the range + */ +LLIST_FUNC (LList llist_inserbefore_range (LList self, LList start, LList end), + self->prev->next = start; + start->prev->next = end; + end->prev = start->prev; + start->prev = self->prev; + self->prev = end->prev; + end->prev->next = self; + return self; +); +#endif + +/** + * Swap a node with its next node. + * @param self node to be advaced + * @return self + * advancing will not stop at tail, one has to check that if this is intended + */ +LLIST_FUNC (LList llist_advance (LList self), + LList tmp = self->next->next; + tmp->prev = self; + self->next->prev = self->prev; + self->prev->next = self->next; + self->prev = self->next; + self->next->next = self; + self->next = tmp; + return self; +); + +/** + * Swap a node with its previous node. + * @param self node to be retreated + * @return self + * retreating will not stop at head, one has to check that if this is intended + */ +LLIST_FUNC (LList llist_retreat (LList self), + LList tmp = self->prev->prev; + tmp->next = self; + self->prev->next = self->next; + self->next->prev = self->prev; + self->next = self->prev; + self->prev->prev = self; + self->prev = tmp; + return self; +); + + +/** + * Get next node. + * @param self current node + * @return node after self + * Will not stop at tail + */ +LLIST_FUNC (LList llist_next (const_LList self), + return self->next; +); + +/** + * Get previous node. + * @param self current node + * @return node before self + * Will not stop at head + */ +LLIST_FUNC (LList llist_prev (const_LList self), + return self->prev; +); + +/** + * Advance a pointer to a node to its next node. + * @param self pointer-to-pointer to the current node + * *self will point to the next node after this call + */ +LLIST_FUNC (void llist_forward (LList_ref self), + *self = (*self)->next; +); + +/** + * Retreat a pointer to a node to its previous node. + * @param self pointer-to-pointer to the current node + * *self will point to the previous node after this call + */ +LLIST_FUNC (void llist_backward (LList_ref self), + *self = (*self)->prev; +); + +/** + * Get the nth element of a list. + * @param self list to be queried + * @param n nth element after (positive n) or before (negative n) self + * this function does not stop at head/tail. + */ +LLIST_FUNC (LList llist_nth (LList self, int n), + if (n>0) + while (n--) + self = llist_get_next (self); + else + while (n++) + self = llist_get_prev (self); + return self; +); + +/** + * Get the nth element of a list with a stop node. + * @param self list to be queried + * @param n nth element after (positive n) or before (negative n) self + * @param stop node which will abort the iteration + */ +LLIST_FUNC (LList llist_get_nth_stop (LList self, int n, const_LList stop), + if (n>0) + while (n--) + { + self = llist_get_next (self); + if (self == stop) + return NULL; + } + else + while (n++) + { + self = llist_get_prev (self); + if (self == stop) + return NULL; + } + return self; +); + +/* + some macros for convenience +*/ +#define llist_insert_head(list, element) llist_insert_next (list, element) +#define llist_insert_tail(list, element) llist_insert_prev (list, element) +#define llist_head llist_next +#define llist_tail llist_prev + +#endif /* LLIST_H */ +/* +// Local Variables: +// mode: C +// c-file-style: "gnu" +// End: +// arch-tag: e8fe4a59-fd55-4c45-b860-5cd1e0771213 +// end_of_file +*/ From 2aaad10bbdf03dade4f8e821f3dccdb80431eac8 Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Wed, 5 Sep 2007 07:07:52 +0200 Subject: [PATCH 6/7] fixed typos and added some basic tests for the llist.h --- src/lib/llist.h | 39 +++++++++----------- tests/15list.tests | 55 ++++++++++++++++++++++++++++ tests/Makefile.am | 6 +++- tests/library/test-llist.c | 73 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 24 deletions(-) create mode 100644 tests/15list.tests create mode 100644 tests/library/test-llist.c diff --git a/src/lib/llist.h b/src/lib/llist.h index 49d531492..4480e507b 100644 --- a/src/lib/llist.h +++ b/src/lib/llist.h @@ -88,6 +88,12 @@ typedef llist * LList; typedef const llist * const_LList; typedef llist ** LList_ref; +/** + * Macro to instantiate a local llist. + * @param name of the llist node + */ +#define LLIST_AUTO(name) llist name = {&name,&name} + /** * cast back from a member of a structure to a pointer of the structure @@ -110,7 +116,7 @@ typedef llist ** LList_ref; */ #define LLIST_FOREACH(list, node) \ if (!list); else \ - for (LList node = llist_get_head (list); \ + for (LList node = llist_head (list); \ ! llist_is_end (node, list); \ llist_forward (&node)) @@ -121,7 +127,7 @@ typedef llist ** LList_ref; */ #define LLIST_FOREACH_REV(list, node) \ if (!list); else \ - for (LList node = llist_get_tail (list); \ + for (LList node = llist_tail (list); \ ! llist_is_end (node, list); \ llist_backward (&node)) @@ -133,9 +139,9 @@ typedef llist ** LList_ref; */ #define LLIST_WHILE_HEAD(list, head) \ if (!list); else \ - for (LList head = llist_get_head (list); \ + for (LList head = llist_head (list); \ !llist_is_empty (list); \ - head = llist_get_head (list)) + head = llist_head (list)) /** * Consume a list from tail. @@ -145,22 +151,9 @@ typedef llist ** LList_ref; */ #define LLIST_WHILE_TAIL(list, tail) \ if (!list); else \ - for (LList tail = llist_get_tail (list); \ + for (LList tail = llist_tail (list); \ !llist_is_empty (list); \ - tail = llist_get_tail (list)) - - -/** - * Macro to instantiate a local llist. - * @param name becomes a LList handle - * the underlying instance is hidden as name_llist_, this list is statically initialized - */ -#define LLIST_AUTO(name) \ -llist name##_llist_ = LLIST_STATIC_INITIALIZER(name##_llist_);\ -LList name = &name##_llist_ -#define LLIST_STATIC_INITIALIZER(name) {&name,&name} - - + tail = llist_tail (list)) /** * Initialize a new llist. @@ -474,10 +467,10 @@ LLIST_FUNC (void llist_backward (LList_ref self), LLIST_FUNC (LList llist_nth (LList self, int n), if (n>0) while (n--) - self = llist_get_next (self); + self = llist_next (self); else while (n++) - self = llist_get_prev (self); + self = llist_prev (self); return self; ); @@ -491,14 +484,14 @@ LLIST_FUNC (LList llist_get_nth_stop (LList self, int n, const_LList stop), if (n>0) while (n--) { - self = llist_get_next (self); + self = llist_next (self); if (self == stop) return NULL; } else while (n++) { - self = llist_get_prev (self); + self = llist_prev (self); if (self == stop) return NULL; } diff --git a/tests/15list.tests b/tests/15list.tests new file mode 100644 index 000000000..6d43064f7 --- /dev/null +++ b/tests/15list.tests @@ -0,0 +1,55 @@ +TESTING "Linked Lists" ./test-llist + +TEST "init nodes" basic < + + 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 +//#include + +#include "lib/llist.h" +#include "lib/framerate.h" + + +CINELERRA_ERROR_DEFINE(TEST, "test error"); + +int +main (int argc, char** argv) +{ + NOBUG_INIT; + + if (argc == 1) + return 0; + + if (!strcmp(argv[1], "basic")) + { + LLIST_AUTO (node1); + + llist node2; + llist_init (&node2); + + printf ("%d\n", llist_is_empty (&node1)); + printf ("%d\n", llist_is_empty (&node2)); + } + else if (!strcmp(argv[1], "nodeinsert")) + { + LLIST_AUTO (list); + LLIST_AUTO (node1); + LLIST_AUTO (node2); + LLIST_AUTO (node3); + + llist_insert_next (&list, &node1); + printf ("%d\n", llist_is_empty (&list)); + printf ("%d\n", llist_is_empty (&node1)); + printf ("%d\n", llist_is_single (&node1)); + llist_insert_next (&node1, &node2); + printf ("%d\n", llist_is_single (&node1)); + llist_insert_prev (&node1, &node3); + printf ("%d\n", llist_next (&list) == &node3); + printf ("%d\n", llist_next (&node3) == &node1); + printf ("%d\n", llist_next (&node1) == &node2); + printf ("%d\n", llist_prev (&list) == &node2); + + } + else + return 1; + + return 0; +} From 65ce98c87da602cc707deabf84eb8720ad959e7a Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Wed, 5 Sep 2007 22:59:01 +0200 Subject: [PATCH 7/7] enough lists tests for basic functionality, more are added as needed --- tests/15list.tests | 72 +++++++++++---------- tests/library/test-llist.c | 124 ++++++++++++++++++++++++++++++++++++- 2 files changed, 161 insertions(+), 35 deletions(-) diff --git a/tests/15list.tests b/tests/15list.tests index 6d43064f7..88aef3c49 100644 --- a/tests/15list.tests +++ b/tests/15list.tests @@ -3,7 +3,6 @@ TESTING "Linked Lists" ./test-llist TEST "init nodes" basic <