Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Pfaff <tpfaff@gmx.net>2003-01-09 23:51:55 +0300
committerThomas Pfaff <tpfaff@gmx.net>2003-01-09 23:51:55 +0300
commit72fcbc3ee690a028128274d2bd0ad694961b5e5f (patch)
tree5e23945897f32019001c1f0c8e45101ae5b6ece8 /winsup/testsuite
parent8ccfe116d0c9ec3afb1cff7f8d41512c8f63466e (diff)
Add pthread_mutex tests
Diffstat (limited to 'winsup/testsuite')
-rw-r--r--winsup/testsuite/ChangeLog14
-rw-r--r--winsup/testsuite/winsup.api/pthread/mutex1d.c42
-rw-r--r--winsup/testsuite/winsup.api/pthread/mutex1e.c42
-rw-r--r--winsup/testsuite/winsup.api/pthread/mutex4.c67
-rw-r--r--winsup/testsuite/winsup.api/pthread/mutex5.c30
-rw-r--r--winsup/testsuite/winsup.api/pthread/mutex6d.c64
-rw-r--r--winsup/testsuite/winsup.api/pthread/mutex6e.c70
-rw-r--r--winsup/testsuite/winsup.api/pthread/mutex7.c50
-rw-r--r--winsup/testsuite/winsup.api/pthread/mutex7d.c51
-rw-r--r--winsup/testsuite/winsup.api/pthread/mutex7e.c70
-rw-r--r--winsup/testsuite/winsup.api/pthread/mutex7r.c68
11 files changed, 568 insertions, 0 deletions
diff --git a/winsup/testsuite/ChangeLog b/winsup/testsuite/ChangeLog
index 502c12bba..3ab06a71d 100644
--- a/winsup/testsuite/ChangeLog
+++ b/winsup/testsuite/ChangeLog
@@ -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!).
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;
+}