From: Bart Van Assche Date: Thu, 13 Mar 2008 17:34:43 +0000 (+0000) Subject: The dots (one per detached thread) are again all printed on one line instead of one... X-Git-Tag: svn/VALGRIND_3_4_0~887 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=36893db5e659ee75e357b3020ccac3fedcc15d5d;p=thirdparty%2Fvalgrind.git The dots (one per detached thread) are again all printed on one line instead of one per line. Changed termination test from a counter protected by a mutex to a counting semaphore. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@7669 --- diff --git a/exp-drd/tests/pth_detached.c b/exp-drd/tests/pth_detached.c index 721f4df8ac..cbc6b6e37d 100644 --- a/exp-drd/tests/pth_detached.c +++ b/exp-drd/tests/pth_detached.c @@ -5,15 +5,15 @@ #include #include +#include #include #include #include #include "../drd_clientreq.h" -static int s_finished_count; -static int s_set_thread_name; -static pthread_mutex_t s_mutex; +static int s_set_thread_name; +static sem_t s_sem; static void set_thread_name(const char* const fmt, const int arg) @@ -29,26 +29,15 @@ static void set_thread_name(const char* const fmt, const int arg) } } -void increment_finished_count() +static void increment_finished_count() { - pthread_mutex_lock(&s_mutex); - s_finished_count++; - pthread_mutex_unlock(&s_mutex); -} - -int get_finished_count() -{ - int result; - pthread_mutex_lock(&s_mutex); - result = s_finished_count; - pthread_mutex_unlock(&s_mutex); - return result; + sem_post(&s_sem); } static void* thread_func1(void* arg) { set_thread_name("thread_func1[%d]", *(int*)arg); - write(STDOUT_FILENO, ".\n", 2); + write(STDOUT_FILENO, ".", 1); increment_finished_count(); return 0; } @@ -57,7 +46,7 @@ static void* thread_func2(void* arg) { set_thread_name("thread_func2[%d]", *(int*)arg); pthread_detach(pthread_self()); - write(STDOUT_FILENO, ".\n", 2); + write(STDOUT_FILENO, ".", 1); increment_finished_count(); return 0; } @@ -79,7 +68,7 @@ int main(int argc, char** argv) for (i = 0; i < count1 || i < count2; i++) thread_arg[i] = i; - pthread_mutex_init(&s_mutex, 0); + sem_init(&s_sem, 0, 0); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); @@ -105,15 +94,14 @@ int main(int argc, char** argv) pthread_attr_destroy(&attr); // Wait until all detached threads have written their output to stdout. - while (get_finished_count() < count1 + count2) + for (i = 0; i < count1 + count2; i++) { - struct timespec delay = { 0, 1 * 1000 * 1000 }; - nanosleep(&delay, 0); + sem_wait(&s_sem); } - printf("\n"); + write(STDOUT_FILENO, "\n", 1); - pthread_mutex_destroy(&s_mutex); + sem_destroy(&s_sem); return 0; } diff --git a/exp-drd/tests/pth_detached.stdout.exp b/exp-drd/tests/pth_detached.stdout.exp index 727e81ea6c..f3229c5b98 100644 --- a/exp-drd/tests/pth_detached.stdout.exp +++ b/exp-drd/tests/pth_detached.stdout.exp @@ -1,3 +1 @@ -. -. - +.. diff --git a/exp-drd/tests/pth_detached2.stdout.exp b/exp-drd/tests/pth_detached2.stdout.exp index b6c290864f..01bfb0256c 100644 --- a/exp-drd/tests/pth_detached2.stdout.exp +++ b/exp-drd/tests/pth_detached2.stdout.exp @@ -1,21 +1 @@ -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. -. - +....................