From 31ffda0a683a54d2570a0ed47a996e1174c75cdd Mon Sep 17 00:00:00 2001
From: Alexandre Abadie <alexandre.abadie@inria.fr>
Date: Sun, 29 Oct 2017 12:22:44 +0100
Subject: [PATCH] tests/pthread_rwlock: improve test behaviour

---
 tests/pthread_rwlock/Makefile |  2 --
 tests/pthread_rwlock/main.c   | 45 ++++++++++++++++++++++++++---------
 2 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/tests/pthread_rwlock/Makefile b/tests/pthread_rwlock/Makefile
index 6ed396f260..16f796ed2d 100644
--- a/tests/pthread_rwlock/Makefile
+++ b/tests/pthread_rwlock/Makefile
@@ -8,8 +8,6 @@ USEMODULE += pthread
 USEMODULE += xtimer
 USEMODULE += random
 
-CFLAGS += -DNATIVE_AUTO_EXIT
-
 BOARD_INSUFFICIENT_MEMORY += airfy-beacon chronos mbed_lpc1768 msb-430 msb-430h \
                              nrf51dongle nrf6310 nucleo32-f031 nucleo32-f042 nucleo32-l031 \
                              nucleo-f030 nucleo-f334 nucleo-l053 pca10000 pca10005 \
diff --git a/tests/pthread_rwlock/main.c b/tests/pthread_rwlock/main.c
index 5635062e23..3955841f63 100644
--- a/tests/pthread_rwlock/main.c
+++ b/tests/pthread_rwlock/main.c
@@ -53,21 +53,32 @@
 static pthread_rwlock_t rwlock;
 static volatile unsigned counter;
 
-#define PRINTF(FMT, ...) \
-    printf("%c%" PRIkernel_pid " (prio=%u): " FMT "\n", __func__[0], sched_active_pid, sched_active_thread->priority, __VA_ARGS__)
+static kernel_pid_t main_thread_pid;
+
+#define PRINTF(FMT, ...)                                \
+    printf("%c%" PRIkernel_pid " (prio=%u): " FMT "\n", \
+           __func__[0], sched_active_pid,               \
+           sched_active_thread->priority,               \
+           (int)__VA_ARGS__)
+
+static void _notify_main_thread(void)
+{
+    msg_t msg;
+    msg_send(&msg, main_thread_pid);
+}
 
 static void do_sleep(int factor)
 {
     uint32_t timeout_us = (random_uint32() % 100000) * factor;
-    /* PRINTF("sleep for % 8i µs.", timeout_us); */
+    PRINTF("sleep for % 8i µs.", timeout_us);
     xtimer_usleep(timeout_us);
 }
 
 static void *writer(void *arg)
 {
     (void) arg;
-    /* PRINTF("%s", "start"); */
-    for (int i = 0; i < NUM_ITERATIONS; ++i) {
+    puts("start");
+    for (unsigned i = 0; i < NUM_ITERATIONS; ++i) {
         pthread_rwlock_wrlock(&rwlock);
         unsigned cur = ++counter;
         do_sleep(3); /* simulate time that it takes to write the value */
@@ -75,15 +86,16 @@ static void *writer(void *arg)
         pthread_rwlock_unlock(&rwlock);
         do_sleep(2);
     }
-    /* PRINTF("%s", "done"); */
+    puts("done");
+    _notify_main_thread();
     return NULL;
 }
 
 static void *reader(void *arg)
 {
     (void) arg;
-    /* PRINTF("%s", "start"); */
-    for (int i = 0; i < NUM_ITERATIONS; ++i) {
+    puts("start");
+    for (unsigned i = 0; i < NUM_ITERATIONS; ++i) {
         pthread_rwlock_rdlock(&rwlock);
         unsigned cur = counter;
         do_sleep(1); /* simulate time that it takes to read the value */
@@ -91,7 +103,9 @@ static void *reader(void *arg)
         pthread_rwlock_unlock(&rwlock);
         do_sleep(1);
     }
-    /* PRINTF("%s", "done"); */
+    puts("done");
+    _notify_main_thread();
+
     return NULL;
 }
 
@@ -99,7 +113,9 @@ int main(void)
 {
     static char stacks[NUM_CHILDREN][THREAD_STACKSIZE_MAIN];
 
-    puts("Main start.");
+    puts("START");
+    /* Get main thread pid */
+    main_thread_pid = thread_getpid();
 
     for (unsigned i = 0; i < NUM_CHILDREN; ++i) {
         int prio;
@@ -132,6 +148,13 @@ int main(void)
                       fun, NULL, name);
     }
 
-    puts("Main done.");
+    /* Block until all children threads are done */
+    for (unsigned i = 0; i < NUM_CHILDREN; ++i) {
+        msg_t msg;
+        msg_receive(&msg);
+    }
+
+    puts("SUCCESS");
+
     return 0;
 }
-- 
GitLab