Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libpthread Merge and fix the timed mutex tests to ...



details:   https://anonhg.NetBSD.org/src/rev/fc70f2b327ae
branches:  trunk
changeset: 348641:fc70f2b327ae
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Oct 31 16:21:23 2016 +0000

description:
Merge and fix the timed mutex tests to use absolute time.
NB: the new tests are broken?

diffstat:

 tests/lib/libpthread/t_mutex.c      |  128 ++++++-
 tests/lib/libpthread/t_timedmutex.c |  610 +-----------------------------------
 2 files changed, 107 insertions(+), 631 deletions(-)

diffs (truncated from 918 to 300 lines):

diff -r 9fa40bcb960f -r fc70f2b327ae tests/lib/libpthread/t_mutex.c
--- a/tests/lib/libpthread/t_mutex.c    Mon Oct 31 15:27:24 2016 +0000
+++ b/tests/lib/libpthread/t_mutex.c    Mon Oct 31 16:21:23 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_mutex.c,v 1.11 2016/10/30 16:17:16 kamil Exp $ */
+/* $NetBSD: t_mutex.c,v 1.12 2016/10/31 16:21:23 christos Exp $ */
 
 /*
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -26,17 +26,16 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-/* Please sync code from this test with t_timedmutex.c */
-
 #include <sys/cdefs.h>
 __COPYRIGHT("@(#) Copyright (c) 2008\
  The NetBSD Foundation, inc. All rights reserved.");
-__RCSID("$NetBSD: t_mutex.c,v 1.11 2016/10/30 16:17:16 kamil Exp $");
+__RCSID("$NetBSD: t_mutex.c,v 1.12 2016/10/31 16:21:23 christos Exp $");
 
 #include <pthread.h>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <time.h>
 #include <unistd.h>
 #include <sys/sched.h>
 #include <sys/param.h>
@@ -49,6 +48,31 @@
 static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
 static int global_x;
 
+#ifdef TIMEDMUTEX
+/* This code is used for verifying non-timed specific code */
+static struct timespec ts_lengthy = {
+       .tv_sec = UINT16_MAX,
+       .tv_nsec = 0
+};
+/* This code is used for verifying timed-only specific code */
+static struct timespec ts_shortlived = {
+       .tv_sec = 0,
+       .tv_nsec = 120
+};
+
+static int
+mutex_lock(pthread_mutex_t *m, const struct timespec *ts)
+{
+       struct timespec ts_wait;
+       ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ts_wait) != -1);
+       timespecadd(&ts_wait, ts, &ts_wait);
+
+       return pthread_mutex_timedlock(m, &ts_wait);
+}
+#else
+#define mutex_lock(a, b) pthread_mutex_lock(a)
+#endif
+
 static void *
 mutex1_threadfunc(void *arg)
 {
@@ -58,7 +82,7 @@
 
        param = arg;
        printf("2: Locking mutex\n");
-       pthread_mutex_lock(&mutex);
+       mutex_lock(&mutex, &ts_lengthy);
        printf("2: Got mutex. *param = %d\n", *param);
        ATF_REQUIRE_EQ(*param, 20);
        (*param)++;
@@ -83,7 +107,7 @@
 
        PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
        x = 1;
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
+       PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
        PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x));
        printf("1: Before changing the value.\n");
        sleep(2);
@@ -94,7 +118,7 @@
        printf("1: After releasing the mutex.\n");
        PTHREAD_REQUIRE(pthread_join(new, &joinval));
 
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
+       PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
        printf("1: Thread joined. X was %d. Return value (int) was %d\n",
                x, *(int *)joinval);
        ATF_REQUIRE_EQ(x, 21);
@@ -110,7 +134,7 @@
        printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
 
        while (count--) {
-               PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
+               PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
                global_x++;
                PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
        }
@@ -143,7 +167,7 @@
        global_x = 0;
        count = count2 = 10000000;
 
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
+       PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
        PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
 
        printf("1: Thread %p\n", pthread_self());
@@ -151,14 +175,14 @@
        PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
 
        while (count--) {
-               PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
+               PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
                global_x++;
                PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
        }
 
        PTHREAD_REQUIRE(pthread_join(new, &joinval));
 
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
+       PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
        printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
                global_x, (long)joinval);
        ATF_REQUIRE_EQ(global_x, 20000000);
@@ -180,7 +204,7 @@
        printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
 
        while (count--) {
-               PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
+               PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
                global_x++;
                PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
        }
@@ -212,7 +236,7 @@
        global_x = 0;
        count = count2 = 10000000;
 
-       PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
+       PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
        PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
 
        printf("1: Thread %p\n", pthread_self());
@@ -220,14 +244,14 @@
        PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
 
        while (count--) {
-               PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
+               PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
                global_x++;
                PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
        }
 
        PTHREAD_REQUIRE(pthread_join(new, &joinval));
 
-       PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
+       PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
        printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
                global_x, (long)joinval);
        ATF_REQUIRE_EQ(global_x, 20000000);
@@ -250,7 +274,7 @@
 
        param = arg;
        printf("2: Locking mutex\n");
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
+       PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
        printf("2: Got mutex. *param = %d\n", *param);
        (*param)++;
 
@@ -281,11 +305,11 @@
        PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
 
        x = 1;
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
+       PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
        PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
 
        printf("1: Before recursively acquiring the mutex.\n");
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
+       PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
 
        printf("1: Before releasing the mutex once.\n");
        sleep(2);
@@ -301,7 +325,7 @@
 
        PTHREAD_REQUIRE(pthread_join(new, &joinval));
 
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
+       PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
        printf("1: Thread joined. X was %d. Return value (int) was %d\n",
                x, *(int *)joinval);
        ATF_REQUIRE_EQ(x, 21);
@@ -322,7 +346,7 @@
        res = _sched_protect(-2);
        ATF_REQUIRE_EQ_MSG(res, -1, "sched_protect returned %d", res);
        ATF_REQUIRE_EQ(errno, ENOENT);
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex5));
+       PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
        printf("child is owning resource\n");
        res = _sched_protect(-2);
        ATF_REQUIRE_EQ(res,  max_fifo_prio);
@@ -365,7 +389,7 @@
            max_fifo_prio));
        
        PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5));
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex5));
+       PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
        printf("enter critical section for main\n");
        PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL));
        printf("main starts to sleep\n");
@@ -403,7 +427,7 @@
                high_cnt = 0;
                sleep(1);
        }
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex6));
+       PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
        if (start == 0) start = 2;
        PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
 
@@ -435,7 +459,7 @@
                low_cnt = 0;
                sleep(1);
        }
-       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex6));
+       PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
        if (start == 0)
                start = 1;
        PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
@@ -553,6 +577,57 @@
        }
 }
 
+#ifdef TIMEDMUTEX
+ATF_TC(timedmutex1);
+ATF_TC_HEAD(timedmutex1, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Checks timeout on selflock");
+}
+
+ATF_TC_BODY(timedmutex1, tc)
+{
+
+       printf("Timed mutex-test 1\n");
+
+       PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
+
+       printf("Before acquiring timed-mutex\n");
+       PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
+
+       printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
+       PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
+           ETIMEDOUT);
+
+       printf("Unlocking timed-mutex\n");
+       PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
+}
+
+ATF_TC(timedmutex2);
+ATF_TC_HEAD(timedmutex2, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Checks timeout on selflock with timedlock");
+}
+
+ATF_TC_BODY(timedmutex2, tc)
+{
+
+       printf("Timed mutex-test 1\n");
+
+       PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
+
+       printf("Before acquiring timed-mutex with timedlock\n");
+       PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
+
+       printf("Before endavor to reacquire timed-mutex (timeout expected)\n");
+       PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
+           ETIMEDOUT);
+
+       printf("Unlocking timed-mutex\n");
+       PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
+}
+#endif
+
 ATF_TP_ADD_TCS(tp)
 {
        ATF_TP_ADD_TC(tp, mutex1);
@@ -563,6 +638,11 @@
        ATF_TP_ADD_TC(tp, mutex6);
        ATF_TP_ADD_TC(tp, mutexattr1);
        ATF_TP_ADD_TC(tp, mutexattr2);
-       
+
+#ifdef TIMEDMUTEX
+       ATF_TP_ADD_TC(tp, timedmutex1);
+       ATF_TP_ADD_TC(tp, timedmutex2);
+#endif
+
        return atf_no_error();
 }
diff -r 9fa40bcb960f -r fc70f2b327ae tests/lib/libpthread/t_timedmutex.c
--- a/tests/lib/libpthread/t_timedmutex.c       Mon Oct 31 15:27:24 2016 +0000
+++ b/tests/lib/libpthread/t_timedmutex.c       Mon Oct 31 16:21:23 2016 +0000



Home | Main Index | Thread Index | Old Index