summaryrefslogtreecommitdiff
path: root/tests/rwlock7.c
diff options
context:
space:
mode:
authorrpj <rpj>2004-10-16 02:34:44 +0000
committerrpj <rpj>2004-10-16 02:34:44 +0000
commit45b1b8cb2a6588f9316f780d8cefe11c181a9a17 (patch)
tree24753e298d9933d48d764177baf183ef97f04156 /tests/rwlock7.c
parent9da8fdcb33373b4b2e1de2a8b7af3ed4b5811245 (diff)
Mutex speedups cont'd
Diffstat (limited to 'tests/rwlock7.c')
-rw-r--r--tests/rwlock7.c56
1 files changed, 35 insertions, 21 deletions
diff --git a/tests/rwlock7.c b/tests/rwlock7.c
index 8706d4a..91466e4 100644
--- a/tests/rwlock7.c
+++ b/tests/rwlock7.c
@@ -13,7 +13,7 @@
#endif
#define THREADS 5
-#define DATASIZE 15
+#define DATASIZE 7
#define ITERATIONS 1000000
/*
@@ -24,7 +24,8 @@ typedef struct thread_tag {
pthread_t thread_id;
int updates;
int reads;
- int interval;
+ int changed;
+ int seed;
} thread_t;
/*
@@ -45,9 +46,12 @@ static data_t data[DATASIZE];
void *thread_routine (void *arg)
{
thread_t *self = (thread_t*)arg;
- int repeats = 0;
int iteration;
int element = 0;
+ int seed = self->seed;
+ int interval = 1 + rand_r (&seed) % 71;
+
+ self->changed = 0;
for (iteration = 0; iteration < ITERATIONS; iteration++)
{
@@ -61,12 +65,13 @@ void *thread_routine (void *arg)
* update operation (write lock instead of read
* lock).
*/
- if ((iteration % self->interval) == 0)
+ if ((iteration % interval) == 0)
{
assert(pthread_rwlock_wrlock (&data[element].lock) == 0);
data[element].data = self->thread_num;
data[element].updates++;
self->updates++;
+ interval = 1 + rand_r (&seed) % 71;
assert(pthread_rwlock_unlock (&data[element].lock) == 0);
} else {
/*
@@ -78,27 +83,17 @@ void *thread_routine (void *arg)
self->reads++;
- if (data[element].data == self->thread_num)
+ if (data[element].data != self->thread_num)
{
- repeats++;
+ self->changed++;
+ interval = 1 + self->changed % 71;
}
assert(pthread_rwlock_unlock (&data[element].lock) == 0);
}
- element++;
-
- if (element >= DATASIZE)
- {
- element = 0;
- }
- }
+ element = (element + 1) % DATASIZE;
- if (repeats > 0)
- {
- printf ("\nThread %d found unchanged elements %d times",
- self->thread_num, repeats);
- fflush(stdout);
}
return NULL;
@@ -137,7 +132,7 @@ main (int argc, char *argv[])
threads[count].thread_num = count;
threads[count].updates = 0;
threads[count].reads = 0;
- threads[count].interval = rand_r (&seed) % 71;
+ threads[count].seed = 1 + rand_r (&seed) % 71;
assert(pthread_create (&threads[count].thread_id,
NULL, thread_routine, (void*)&threads[count]) == 0);
@@ -150,9 +145,28 @@ main (int argc, char *argv[])
for (count = 0; count < THREADS; count++)
{
assert(pthread_join (threads[count].thread_id, NULL) == 0);
+ }
+
+ putchar('\n');
+ fflush(stdout);
+
+ for (count = 0; count < THREADS; count++)
+ {
+ if (threads[count].changed > 0)
+ {
+ printf ("Thread %d found changed elements %d times\n",
+ count, threads[count].changed);
+ }
+ }
+
+ putchar('\n');
+ fflush(stdout);
+
+ for (count = 0; count < THREADS; count++)
+ {
thread_updates += threads[count].updates;
- printf ("\n%02d: interval %d, updates %d, reads %d\n",
- count, threads[count].interval,
+ printf ("%02d: seed %d, updates %d, reads %d\n",
+ count, threads[count].seed,
threads[count].updates, threads[count].reads);
}