diff options
author | rpj <rpj> | 2001-02-07 13:08:54 +0000 |
---|---|---|
committer | rpj <rpj> | 2001-02-07 13:08:54 +0000 |
commit | 3c0fbfafe563b7b312d60c42c0d58a3a1f4d5930 (patch) | |
tree | 942ff52059442065ed3dd3c00d77a37ed72c6084 /tests/rwlock7.c | |
parent | 7a31ef49c345dfb39de7cd6d43eeb29c34129495 (diff) |
Change the purpose of rwlock6 test and add new rwlock7 test.
Diffstat (limited to 'tests/rwlock7.c')
-rw-r--r-- | tests/rwlock7.c | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/tests/rwlock7.c b/tests/rwlock7.c new file mode 100644 index 0000000..88abcfe --- /dev/null +++ b/tests/rwlock7.c @@ -0,0 +1,173 @@ +/* + * rwlock7.c + * + * Hammer on a bunch of rwlocks to test robustness and fairness. + * Printed stats should be roughly even for each thread. + */ + +#include "test.h" +#include <sys/timeb.h> + +#define THREADS 5 +#define DATASIZE 15 +#define ITERATIONS 10000 + +/* + * Keep statistics for each thread. + */ +typedef struct thread_tag { + int thread_num; + pthread_t thread_id; + int updates; + int reads; + int interval; +} thread_t; + +/* + * Read-write lock and shared data + */ +typedef struct data_tag { + rwlock_t lock; + int data; + int updates; +} data_t; + +static thread_t threads[THREADS]; +static data_t data[DATASIZE]; + +/* + * Thread start routine that uses read-write locks + */ +void *thread_routine (void *arg) +{ + thread_t *self = (thread_t*)arg; + int repeats = 0; + int iteration; + int element = 0; + + for (iteration = 0; iteration < ITERATIONS; iteration++) + { + /* + * Each "self->interval" iterations, perform an + * update operation (write lock instead of read + * lock). + */ + if ((iteration % self->interval) == 0) + { + assert(pthread_rwlock_wrlock (&data[element].lock) == 0); + data[element].data = self->thread_num; + data[element].updates++; + self->updates++; + assert(pthread_rwlock_unlock (&data[element].lock) == 0); + } else { + /* + * Look at the current data element to see whether + * the current thread last updated it. Count the + * times, to report later. + */ + assert(pthread_rwlock_rdlock (&data[element].lock) == 0); + + self->reads++; + + if (data[element].data == self->thread_num) + { + repeats++; + } + + assert(pthread_rwlock_unlock (&data[element].lock) == 0); + } + + element++; + + if (element >= DATASIZE) + { + element = 0; + } + } + + if (repeats > 0) + { + printf ("Thread %d found unchanged elements %d times\n", + self->thread_num, repeats); + } + + return NULL; +} + +int +main (int argc, char *argv[]) +{ + int count; + int data_count; + int status; + unsigned int seed = 1; + int thread_updates = 0; + int data_updates = 0; + + struct _timeb currSysTime1; + struct _timeb currSysTime2; + + /* + * Initialize the shared data. + */ + for (data_count = 0; data_count < DATASIZE; data_count++) + { + data[data_count].data = 0; + data[data_count].updates = 0; + + assert(pthread_rwlock_init (&data[data_count].lock) == 0); + } + + _ftime(&currSysTime1); + + /* + * Create THREADS threads to access shared data. + */ + for (count = 0; count < THREADS; count++) + { + threads[count].thread_num = count; + threads[count].updates = 0; + threads[count].reads = 0; + threads[count].interval = rand_r (&seed) % 71; + + assert(pthread_create (&threads[count].thread_id, + NULL, thread_routine, (void*)&threads[count]) == 0); + } + + /* + * Wait for all threads to complete, and collect + * statistics. + */ + for (count = 0; count < THREADS; count++) + { + assert(pthread_join (threads[count].thread_id, NULL) == 0); + thread_updates += threads[count].updates; + printf ("%02d: interval %d, updates %d, reads %d\n", + count, threads[count].interval, + threads[count].updates, threads[count].reads); + } + + /* + * Collect statistics for the data. + */ + for (data_count = 0; data_count < DATASIZE; data_count++) + { + data_updates += data[data_count].updates; + printf ("data %02d: value %d, %d updates\n", + data_count, data[data_count].data, data[data_count].updates); + assert(pthread_rwlock_destroy (&data[data_count].lock) == 0); + } + + printf ("%d thread updates, %d data updates\n", + thread_updates, data_updates); + + _ftime(&currSysTime2); + + printf( "\nstart: %d/%d, stop: %d/%d, duration:%d\n", + currSysTime1.time,currSysTime1.millitm, + currSysTime2.time,currSysTime2.millitm, + (currSysTime2.time*1000+currSysTime2.millitm) - + (currSysTime1.time*1000+currSysTime1.millitm)); + + return 0; +} |