|
From: <sv...@va...> - 2006-12-17 17:40:09
|
Author: sewardj
Date: 2006-12-17 17:40:06 +0000 (Sun, 17 Dec 2006)
New Revision: 6407
Log:
Add locking so this produces repeatable results (Bart Van Assche).
Modified:
trunk/none/tests/pth_detached.c
trunk/none/tests/pth_detached.stdout.exp
Modified: trunk/none/tests/pth_detached.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/none/tests/pth_detached.c 2006-12-17 14:20:31 UTC (rev 6406)
+++ trunk/none/tests/pth_detached.c 2006-12-17 17:40:06 UTC (rev 6407)
@@ -8,20 +8,37 @@
#include <stdlib.h>
#include <unistd.h>
=20
-static int s_finished_count;
+static int s_finished_count =3D 0;
+static pthread_spinlock_t s_spinlock;
=20
+void increment_finished_count()
+{
+ pthread_spin_lock(&s_spinlock);
+ s_finished_count++;
+ pthread_spin_unlock(&s_spinlock);
+}
+
+int get_finished_count()
+{
+ int result;
+ pthread_spin_lock(&s_spinlock);
+ result =3D s_finished_count;
+ pthread_spin_unlock(&s_spinlock);
+ return result;
+}
+
static void* thread_func1(void* arg)
{
write(STDOUT_FILENO, ".", 1);
- s_finished_count++;
+ increment_finished_count();
return 0;
}
=20
static void* thread_func2(void* arg)
{
pthread_detach(pthread_self());
- write(STDOUT_FILENO, "*", 1);
- s_finished_count++;
+ write(STDOUT_FILENO, ".", 1);
+ increment_finished_count();
return 0;
}
=20
@@ -32,7 +49,9 @@
int i;
int detachstate;
pthread_attr_t attr;
- =20
+
+ pthread_spin_init(&s_spinlock, 0);
+
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
assert(pthread_attr_getdetachstate(&attr, &detachstate) =3D=3D 0);
@@ -57,12 +76,15 @@
pthread_attr_destroy(&attr);
=20
// Wait until all detached threads have written their output to stdout=
.
- while (s_finished_count < count1 + count2)
+ while (get_finished_count() < count1 + count2)
{
struct timespec delay =3D { 0, 1 * 1000 * 1000 };
nanosleep(&delay, 0);
}
=20
printf("\n");
+
+ pthread_spin_destroy(&s_spinlock);
+
return 0;
}
Modified: trunk/none/tests/pth_detached.stdout.exp
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/none/tests/pth_detached.stdout.exp 2006-12-17 14:20:31 UTC (rev=
6406)
+++ trunk/none/tests/pth_detached.stdout.exp 2006-12-17 17:40:06 UTC (rev=
6407)
@@ -1 +1 @@
-........................................................................=
............................*********************************************=
*******************************************************
+........................................................................=
.........................................................................=
.......................................................
|