From 72fcbc3ee690a028128274d2bd0ad694961b5e5f Mon Sep 17 00:00:00 2001 From: Thomas Pfaff Date: Thu, 9 Jan 2003 20:51:55 +0000 Subject: Add pthread_mutex tests --- winsup/testsuite/winsup.api/pthread/mutex1d.c | 42 ++++++++++++++++ winsup/testsuite/winsup.api/pthread/mutex1e.c | 42 ++++++++++++++++ winsup/testsuite/winsup.api/pthread/mutex4.c | 67 +++++++++++++++++++++++++ winsup/testsuite/winsup.api/pthread/mutex5.c | 30 ++++++++++++ winsup/testsuite/winsup.api/pthread/mutex6d.c | 64 ++++++++++++++++++++++++ winsup/testsuite/winsup.api/pthread/mutex6e.c | 70 +++++++++++++++++++++++++++ winsup/testsuite/winsup.api/pthread/mutex7.c | 50 +++++++++++++++++++ winsup/testsuite/winsup.api/pthread/mutex7d.c | 51 +++++++++++++++++++ winsup/testsuite/winsup.api/pthread/mutex7e.c | 70 +++++++++++++++++++++++++++ winsup/testsuite/winsup.api/pthread/mutex7r.c | 68 ++++++++++++++++++++++++++ 10 files changed, 554 insertions(+) create mode 100644 winsup/testsuite/winsup.api/pthread/mutex1d.c create mode 100644 winsup/testsuite/winsup.api/pthread/mutex1e.c create mode 100644 winsup/testsuite/winsup.api/pthread/mutex4.c create mode 100644 winsup/testsuite/winsup.api/pthread/mutex5.c create mode 100644 winsup/testsuite/winsup.api/pthread/mutex6d.c create mode 100644 winsup/testsuite/winsup.api/pthread/mutex6e.c create mode 100644 winsup/testsuite/winsup.api/pthread/mutex7.c create mode 100644 winsup/testsuite/winsup.api/pthread/mutex7d.c create mode 100644 winsup/testsuite/winsup.api/pthread/mutex7e.c create mode 100644 winsup/testsuite/winsup.api/pthread/mutex7r.c (limited to 'winsup/testsuite/winsup.api/pthread') diff --git a/winsup/testsuite/winsup.api/pthread/mutex1d.c b/winsup/testsuite/winsup.api/pthread/mutex1d.c new file mode 100644 index 000000000..354b106d1 --- /dev/null +++ b/winsup/testsuite/winsup.api/pthread/mutex1d.c @@ -0,0 +1,42 @@ +/* + * mutex1d.c + * + * As for mutex1.c but with type set to PTHREAD_MUTEX_DEFAULT. + * + * Create a simple mutex object, lock it, unlock it, then destroy it. + * This is the simplest test of the pthread mutex family that we can do. + * + * Depends on API functions: + * pthread_mutexattr_settype() + * pthread_mutex_init() + * pthread_mutex_destroy() + */ + +#include "test.h" + +pthread_mutex_t mutex = NULL; +pthread_mutexattr_t mxAttr; + +int +main() +{ + assert(pthread_mutexattr_init(&mxAttr) == 0); + + assert(pthread_mutexattr_settype(&mxAttr, PTHREAD_MUTEX_DEFAULT) == 0); + + assert(mutex == NULL); + + assert(pthread_mutex_init(&mutex, &mxAttr) == 0); + + assert(mutex != NULL); + + assert(pthread_mutex_lock(&mutex) == 0); + + assert(pthread_mutex_unlock(&mutex) == 0); + + assert(pthread_mutex_destroy(&mutex) == 0); + + assert(mutex == NULL); + + return 0; +} diff --git a/winsup/testsuite/winsup.api/pthread/mutex1e.c b/winsup/testsuite/winsup.api/pthread/mutex1e.c new file mode 100644 index 000000000..2feb16c08 --- /dev/null +++ b/winsup/testsuite/winsup.api/pthread/mutex1e.c @@ -0,0 +1,42 @@ +/* + * mutex1e.c + * + * As for mutex1.c but with type set to PTHREAD_MUTEX_ERRORCHECK. + * + * Create a simple mutex object, lock it, unlock it, then destroy it. + * This is the simplest test of the pthread mutex family that we can do. + * + * Depends on API functions: + * pthread_mutexattr_settype() + * pthread_mutex_init() + * pthread_mutex_destroy() + */ + +#include "test.h" + +pthread_mutex_t mutex = NULL; +pthread_mutexattr_t mxAttr; + +int +main() +{ + assert(pthread_mutexattr_init(&mxAttr) == 0); + + assert(pthread_mutexattr_settype(&mxAttr, PTHREAD_MUTEX_ERRORCHECK) == 0); + + assert(mutex == NULL); + + assert(pthread_mutex_init(&mutex, &mxAttr) == 0); + + assert(mutex != NULL); + + assert(pthread_mutex_lock(&mutex) == 0); + + assert(pthread_mutex_unlock(&mutex) == 0); + + assert(pthread_mutex_destroy(&mutex) == 0); + + assert(mutex == NULL); + + return 0; +} diff --git a/winsup/testsuite/winsup.api/pthread/mutex4.c b/winsup/testsuite/winsup.api/pthread/mutex4.c new file mode 100644 index 000000000..2cc858015 --- /dev/null +++ b/winsup/testsuite/winsup.api/pthread/mutex4.c @@ -0,0 +1,67 @@ +/* + * mutex4.c + * + * Thread A locks mutex - thread B tries to unlock. + * + * Depends on API functions: + * pthread_mutex_lock() + * pthread_mutex_trylock() + * pthread_mutex_unlock() + */ + +#include "test.h" + +static int wasHere = 0; + +static pthread_mutex_t mutex1; + +void * unlocker(void * arg) +{ + int expectedResult = (int) arg; + + wasHere++; + assert(pthread_mutex_unlock(&mutex1) == expectedResult); + wasHere++; + return NULL; +} + +int +main() +{ + pthread_t t; + pthread_mutexattr_t ma; + + assert(pthread_mutexattr_init(&ma) == 0); + + wasHere = 0; + assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_DEFAULT) == 0); + assert(pthread_mutex_init(&mutex1, &ma) == 0); + assert(pthread_mutex_lock(&mutex1) == 0); + assert(pthread_create(&t, NULL, unlocker, (void *) EPERM) == 0); + assert(pthread_join(t, NULL) == 0); + assert(pthread_mutex_unlock(&mutex1) == 0); + assert(pthread_mutex_destroy(&mutex1) == 0); + assert(wasHere == 2); + + wasHere = 0; + assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK) == 0); + assert(pthread_mutex_init(&mutex1, &ma) == 0); + assert(pthread_mutex_lock(&mutex1) == 0); + assert(pthread_create(&t, NULL, unlocker, (void *) EPERM) == 0); + assert(pthread_join(t, NULL) == 0); + assert(pthread_mutex_unlock(&mutex1) == 0); + assert(pthread_mutex_destroy(&mutex1) == 0); + assert(wasHere == 2); + + wasHere = 0; + assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE) == 0); + assert(pthread_mutex_init(&mutex1, &ma) == 0); + assert(pthread_mutex_lock(&mutex1) == 0); + assert(pthread_create(&t, NULL, unlocker, (void *) EPERM) == 0); + assert(pthread_join(t, NULL) == 0); + assert(pthread_mutex_unlock(&mutex1) == 0); + assert(pthread_mutex_destroy(&mutex1) == 0); + assert(wasHere == 2); + + return 0; +} diff --git a/winsup/testsuite/winsup.api/pthread/mutex5.c b/winsup/testsuite/winsup.api/pthread/mutex5.c new file mode 100644 index 000000000..5cb1478b8 --- /dev/null +++ b/winsup/testsuite/winsup.api/pthread/mutex5.c @@ -0,0 +1,30 @@ +/* + * mutex5.c + * + * Confirm the equality/inequality of the various mutex types, + * and the default not-set value. + */ + +#include "test.h" + +static pthread_mutexattr_t mxAttr; + +int +main() +{ + int mxType = -1; + int success = 0; /* Use to quell GNU compiler warnings. */ + + assert(success = PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_ERRORCHECK); + assert(success = PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_RECURSIVE); + assert(success = PTHREAD_MUTEX_RECURSIVE != PTHREAD_MUTEX_ERRORCHECK); + + if (success == success) + { + assert(pthread_mutexattr_init(&mxAttr) == 0); + assert(pthread_mutexattr_gettype(&mxAttr, &mxType) == 0); + assert(mxType == PTHREAD_MUTEX_ERRORCHECK); + } + + return 0; +} diff --git a/winsup/testsuite/winsup.api/pthread/mutex6d.c b/winsup/testsuite/winsup.api/pthread/mutex6d.c new file mode 100644 index 000000000..bcbcd9767 --- /dev/null +++ b/winsup/testsuite/winsup.api/pthread/mutex6d.c @@ -0,0 +1,64 @@ +/* + * mutex6d.c + * + * Tests PTHREAD_MUTEX_DEFAULT mutex type. + * The thread should behave the same way than an errorchecking mutex. + * + * Depends on API functions: + * pthread_create() + * pthread_mutexattr_init() + * pthread_mutexattr_settype() + * pthread_mutexattr_gettype() + * pthread_mutex_init() + * pthread_mutex_lock() + * pthread_mutex_unlock() + */ + +#include "test.h" + +static int lockCount = 0; + +static pthread_mutex_t mutex; +static pthread_mutexattr_t mxAttr; + +void * locker(void * arg) +{ + assert(pthread_mutex_lock(&mutex) == 0); + lockCount++; + assert(pthread_mutex_lock(&mutex) == EDEADLK); + lockCount++; + assert(pthread_mutex_unlock(&mutex) == 0); + assert(pthread_mutex_unlock(&mutex) == EPERM); + + return (void *) 555; +} + +int +main() +{ + pthread_t t; + int result = 0; + int mxType = -1; + + assert(pthread_mutexattr_init(&mxAttr) == 0); + assert(pthread_mutexattr_settype(&mxAttr, PTHREAD_MUTEX_DEFAULT) == 0); + assert(pthread_mutexattr_gettype(&mxAttr, &mxType) == 0); + assert(mxType == PTHREAD_MUTEX_DEFAULT); + + assert(pthread_mutex_init(&mutex, &mxAttr) == 0); + + assert(pthread_create(&t, NULL, locker, NULL) == 0); + + assert(pthread_join(t, (void **) &result) == 0); + assert(result == 555); + + assert(lockCount == 2); + + assert(pthread_mutex_destroy(&mutex) == 0); + assert(pthread_mutexattr_destroy(&mxAttr) == 0); + + exit(0); + + /* Never reached */ + return 0; +} diff --git a/winsup/testsuite/winsup.api/pthread/mutex6e.c b/winsup/testsuite/winsup.api/pthread/mutex6e.c new file mode 100644 index 000000000..e22550f7a --- /dev/null +++ b/winsup/testsuite/winsup.api/pthread/mutex6e.c @@ -0,0 +1,70 @@ +/* + * mutex6e.c + * + * Tests PTHREAD_MUTEX_ERRORCHECK mutex type. + * Thread locks mutex twice (recursive lock). + * This should fail with an EDEADLK error. + * The second unlock attempt should fail with an EPERM error. + * + * Depends on API functions: + * pthread_create() + * pthread_join() + * pthread_mutexattr_init() + * pthread_mutexattr_destroy() + * pthread_mutexattr_settype() + * pthread_mutexattr_gettype() + * pthread_mutex_init() + * pthread_mutex_destroy() + * pthread_mutex_lock() + * pthread_mutex_unlock() + */ + +#include "test.h" + +static int lockCount = 0; + +static pthread_mutex_t mutex; +static pthread_mutexattr_t mxAttr; + +void * locker(void * arg) +{ + assert(pthread_mutex_lock(&mutex) == 0); + lockCount++; + assert(pthread_mutex_lock(&mutex) == EDEADLK); + lockCount++; + assert(pthread_mutex_unlock(&mutex) == 0); + assert(pthread_mutex_unlock(&mutex) == EPERM); + + return (void *) 555; +} + +int +main() +{ + pthread_t t; + int result = 0; + int mxType = -1; + + assert(pthread_mutexattr_init(&mxAttr) == 0); + assert(pthread_mutexattr_settype(&mxAttr, PTHREAD_MUTEX_ERRORCHECK) == 0); + assert(pthread_mutexattr_gettype(&mxAttr, &mxType) == 0); + assert(mxType == PTHREAD_MUTEX_ERRORCHECK); + + assert(pthread_mutex_init(&mutex, &mxAttr) == 0); + + assert(pthread_create(&t, NULL, locker, NULL) == 0); + + assert(pthread_join(t, (void **) &result) == 0); + assert(result == 555); + + assert(lockCount == 2); + + assert(pthread_mutex_destroy(&mutex) == 0); + assert(pthread_mutexattr_destroy(&mxAttr) == 0); + + exit(0); + + /* Never reached */ + return 0; +} + diff --git a/winsup/testsuite/winsup.api/pthread/mutex7.c b/winsup/testsuite/winsup.api/pthread/mutex7.c new file mode 100644 index 000000000..d2c9f8bee --- /dev/null +++ b/winsup/testsuite/winsup.api/pthread/mutex7.c @@ -0,0 +1,50 @@ +/* + * mutex7.c + * + * Test the default (type not set) mutex type. + * Should be the same as PTHREAD_MUTEX_ERRORCHECK. + * Thread locks then trylocks mutex (attempted recursive lock). + * The thread should lock first time and EBUSY second time. + * + * Depends on API functions: + * pthread_mutex_lock() + * pthread_mutex_trylock() + * pthread_mutex_unlock() + */ + +#include "test.h" + +static int lockCount = 0; + +static pthread_mutex_t mutex; + +void * locker(void * arg) +{ + assert(pthread_mutex_lock(&mutex) == 0); + lockCount++; + assert(pthread_mutex_trylock(&mutex) == EBUSY); + lockCount++; + assert(pthread_mutex_unlock(&mutex) == 0); + assert(pthread_mutex_unlock(&mutex) == EPERM); + + return 0; +} + +int +main() +{ + pthread_t t; + + assert(pthread_mutex_init(&mutex, NULL) == 0); + + assert(pthread_create(&t, NULL, locker, NULL) == 0); + + Sleep(1000); + + assert(lockCount == 2); + + exit(0); + + /* Never reached */ + return 0; +} diff --git a/winsup/testsuite/winsup.api/pthread/mutex7d.c b/winsup/testsuite/winsup.api/pthread/mutex7d.c new file mode 100644 index 000000000..906d0f043 --- /dev/null +++ b/winsup/testsuite/winsup.api/pthread/mutex7d.c @@ -0,0 +1,51 @@ +/* + * mutex7d.c + * + * Test the default (type not set) mutex type. + * Should be the same as PTHREAD_MUTEX_ERRORCHECK. + * Thread locks then trylocks mutex (attempted recursive lock). + * The thread should lock first time and EBUSY second time. + * + * Depends on API functions: + * pthread_mutex_lock() + * pthread_mutex_trylock() + * pthread_mutex_unlock() + */ + +#include "test.h" + +static int lockCount = 0; + +static pthread_mutex_t mutex; + +void * locker(void * arg) +{ + assert(pthread_mutex_lock(&mutex) == 0); + lockCount++; + assert(pthread_mutex_trylock(&mutex) == EBUSY); + lockCount++; + assert(pthread_mutex_unlock(&mutex) == 0); + assert(pthread_mutex_unlock(&mutex) == EPERM); + + return 0; +} + +int +main() +{ + int result = 0; + pthread_t t; + + assert(pthread_mutex_init(&mutex, NULL) == 0); + + assert(pthread_create(&t, NULL, locker, NULL) == 0); + + assert(pthread_join(t, (void **) &result) == 0); + + assert(lockCount == 2); + + exit(0); + + /* Never reached */ + return 0; +} diff --git a/winsup/testsuite/winsup.api/pthread/mutex7e.c b/winsup/testsuite/winsup.api/pthread/mutex7e.c new file mode 100644 index 000000000..05722512d --- /dev/null +++ b/winsup/testsuite/winsup.api/pthread/mutex7e.c @@ -0,0 +1,70 @@ +/* + * mutex7e.c + * + * Tests PTHREAD_MUTEX_ERRORCHECK mutex type. + * Thread locks and then trylocks mutex (attempted recursive lock). + * Trylock should fail with an EBUSY error. + * The second unlock attempt should fail with an EPERM error. + * + * Depends on API functions: + * pthread_create() + * pthread_join() + * pthread_mutexattr_init() + * pthread_mutexattr_destroy() + * pthread_mutexattr_settype() + * pthread_mutexattr_gettype() + * pthread_mutex_init() + * pthread_mutex_destroy() + * pthread_mutex_lock() + * pthread_mutex_unlock() + */ + +#include "test.h" + +static int lockCount = 0; + +static pthread_mutex_t mutex; +static pthread_mutexattr_t mxAttr; + +void * locker(void * arg) +{ + assert(pthread_mutex_lock(&mutex) == 0); + lockCount++; + assert(pthread_mutex_trylock(&mutex) == EBUSY); + lockCount++; + assert(pthread_mutex_unlock(&mutex) == 0); + assert(pthread_mutex_unlock(&mutex) == EPERM); + + return (void *) 555; +} + +int +main() +{ + pthread_t t; + int result = 0; + int mxType = -1; + + assert(pthread_mutexattr_init(&mxAttr) == 0); + assert(pthread_mutexattr_settype(&mxAttr, PTHREAD_MUTEX_ERRORCHECK) == 0); + assert(pthread_mutexattr_gettype(&mxAttr, &mxType) == 0); + assert(mxType == PTHREAD_MUTEX_ERRORCHECK); + + assert(pthread_mutex_init(&mutex, &mxAttr) == 0); + + assert(pthread_create(&t, NULL, locker, NULL) == 0); + + assert(pthread_join(t, (void **) &result) == 0); + assert(result == 555); + + assert(lockCount == 2); + + assert(pthread_mutex_destroy(&mutex) == 0); + assert(pthread_mutexattr_destroy(&mxAttr) == 0); + + exit(0); + + /* Never reached */ + return 0; +} + diff --git a/winsup/testsuite/winsup.api/pthread/mutex7r.c b/winsup/testsuite/winsup.api/pthread/mutex7r.c new file mode 100644 index 000000000..6f9ed744b --- /dev/null +++ b/winsup/testsuite/winsup.api/pthread/mutex7r.c @@ -0,0 +1,68 @@ +/* + * mutex7r.c + * + * Tests PTHREAD_MUTEX_RECURSIVE mutex type. + * Thread locks mutex then trylocks mutex (recursive lock twice). + * Both locks and unlocks should succeed. + * + * Depends on API functions: + * pthread_create() + * pthread_join() + * pthread_mutexattr_init() + * pthread_mutexattr_destroy() + * pthread_mutexattr_settype() + * pthread_mutexattr_gettype() + * pthread_mutex_init() + * pthread_mutex_destroy() + * pthread_mutex_lock() + * pthread_mutex_unlock() + */ + +#include "test.h" + +static int lockCount = 0; + +static pthread_mutex_t mutex; +static pthread_mutexattr_t mxAttr; + +void * locker(void * arg) +{ + assert(pthread_mutex_lock(&mutex) == 0); + lockCount++; + assert(pthread_mutex_trylock(&mutex) == 0); + lockCount++; + assert(pthread_mutex_unlock(&mutex) == 0); + assert(pthread_mutex_unlock(&mutex) == 0); + + return (void *) 555; +} + +int +main() +{ + pthread_t t; + int result = 0; + int mxType = -1; + + assert(pthread_mutexattr_init(&mxAttr) == 0); + assert(pthread_mutexattr_settype(&mxAttr, PTHREAD_MUTEX_RECURSIVE) == 0); + assert(pthread_mutexattr_gettype(&mxAttr, &mxType) == 0); + assert(mxType == PTHREAD_MUTEX_RECURSIVE); + + assert(pthread_mutex_init(&mutex, &mxAttr) == 0); + + assert(pthread_create(&t, NULL, locker, NULL) == 0); + + assert(pthread_join(t, (void **) &result) == 0); + assert(result == 555); + + assert(lockCount == 2); + + assert(pthread_mutex_destroy(&mutex) == 0); + assert(pthread_mutexattr_destroy(&mxAttr) == 0); + + exit(0); + + /* Never reached */ + return 0; +} -- cgit v1.2.3