Add pthread_mutex tests

This commit is contained in:
Thomas Pfaff 2003-01-09 20:51:55 +00:00
parent 8ccfe116d0
commit 72fcbc3ee6
11 changed files with 568 additions and 0 deletions

View File

@ -1,3 +1,17 @@
2003-01-09 Thomas Pfaff <tpfaff@gmx.net>
* winsup.api/pthread/mutex1d.c: New test. Port from pthreads-win32
project.
* winsup.api/pthread/mutex1e.c: Ditto.
* winsup.api/pthread/mutex4.c: Ditto.
* winsup.api/pthread/mutex5.c: Ditto.
* winsup.api/pthread/mutex6d.c: Ditto.
* winsup.api/pthread/mutex6e.c: Ditto.
* winsup.api/pthread/mutex7.c: Ditto.
* winsup.api/pthread/mutex7d.c: Ditto.
* winsup.api/pthread/mutex7e.c: Ditto.
* winsup.api/pthread/mutex7r.c: Ditto.
2002-11-25 Robert Collins <rbtcollins@hotmail.com>
* readme: Document running portions of the test suite (Thanks Egor!).

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}