Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 2052 → Rev 2053

/trunk/kernel/test/synch/rwlock1.c
59,7 → 59,6
rwlock_read_unlock(&rwlock);
rwlock_read_unlock(&rwlock);
rwlock_write_lock(&rwlock);
rwlock_write_unlock(&rwlock);
 
/trunk/kernel/test/synch/rwlock2.c
38,26 → 38,27
#define WRITERS 50
 
static rwlock_t rwlock;
static bool sh_quiet;
 
static void writer(void *arg)
{
 
thread_detach(THREAD);
 
printf("Trying to lock rwlock for writing....\n");
 
if (!sh_quiet)
printf("Trying to lock rwlock for writing....\n");
rwlock_write_lock(&rwlock);
rwlock_write_unlock(&rwlock);
printf("Trying to lock rwlock for reading....\n");
if (!sh_quiet)
printf("Trying to lock rwlock for reading....\n");
rwlock_read_lock(&rwlock);
rwlock_read_unlock(&rwlock);
printf("Test passed.\n");
}
 
char * test_rwlock2(bool quiet)
{
thread_t *thrd;
sh_quiet = quiet;
rwlock_initialize(&rwlock);
 
79,5 → 80,8
rwlock_read_unlock(&rwlock);
rwlock_read_unlock(&rwlock);
thread_join(thrd);
thread_detach(thrd);
return NULL;
}
/trunk/kernel/test/synch/rwlock3.c
34,25 → 34,34
 
#include <synch/rwlock.h>
 
#define READERS 50
#define WRITERS 50
#define THREADS 4
 
static atomic_t thread_count;
static rwlock_t rwlock;
static bool sh_quiet;
 
static void reader(void *arg)
{
thread_detach(THREAD);
 
printf("cpu%d, tid %d: trying to lock rwlock for reading....\n", CPU->id, THREAD->tid);
if (!sh_quiet)
printf("cpu%d, tid %d: trying to lock rwlock for reading....\n", CPU->id, THREAD->tid);
rwlock_read_lock(&rwlock);
rwlock_read_unlock(&rwlock);
printf("cpu%d, tid %d: success\n", CPU->id, THREAD->tid);
rwlock_read_unlock(&rwlock);
if (!sh_quiet) {
printf("cpu%d, tid %d: success\n", CPU->id, THREAD->tid);
printf("cpu%d, tid %d: trying to lock rwlock for writing....\n", CPU->id, THREAD->tid);
}
 
printf("cpu%d, tid %d: trying to lock rwlock for writing....\n", CPU->id, THREAD->tid);
 
rwlock_write_lock(&rwlock);
rwlock_write_unlock(&rwlock);
printf("cpu%d, tid %d: success\n", CPU->id, THREAD->tid);
if (!sh_quiet)
printf("cpu%d, tid %d: success\n", CPU->id, THREAD->tid);
atomic_dec(&thread_count);
}
 
char * test_rwlock3(bool quiet)
59,21 → 68,29
{
int i;
thread_t *thrd;
sh_quiet = quiet;
atomic_set(&thread_count, THREADS);
rwlock_initialize(&rwlock);
rwlock_write_lock(&rwlock);
for (i = 0; i < 4; i++) {
for (i = 0; i < THREADS; i++) {
thrd = thread_create(reader, NULL, TASK, 0, "reader", false);
if (thrd)
thread_ready(thrd);
else
else if (!quiet)
printf("Could not create reader %d\n", i);
}
 
thread_sleep(1);
rwlock_write_unlock(&rwlock);
while (atomic_get(&thread_count) > 0) {
if (!quiet)
printf("Threads left: %d\n", atomic_get(&thread_count));
thread_sleep(1);
}
return NULL;
}
/trunk/kernel/test/synch/rwlock4.c
43,8 → 43,10
#define READERS 50
#define WRITERS 50
 
static atomic_t thread_count;
static rwlock_t rwlock;
static atomic_t threads_fault;
static bool sh_quiet;
 
SPINLOCK_INITIALIZE(rw_lock);
 
70,28 → 72,42
waitq_sleep(&can_start);
 
to = random(40000);
printf("cpu%d, tid %d w+ (%d)\n", CPU->id, THREAD->tid, to);
if (!sh_quiet)
printf("cpu%d, tid %d w+ (%d)\n", CPU->id, THREAD->tid, to);
rc = rwlock_write_lock_timeout(&rwlock, to);
if (SYNCH_FAILED(rc)) {
printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid);
if (!sh_quiet)
printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid);
atomic_dec(&thread_count);
return;
}
printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid);
if (!sh_quiet)
printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid);
 
if (rwlock.readers_in) {
printf("Oops.");
if (!sh_quiet)
printf("Oops.");
atomic_inc(&threads_fault);
atomic_dec(&thread_count);
return;
}
thread_usleep(random(1000000));
if (rwlock.readers_in) {
printf("Oops.");
if (!sh_quiet)
printf("Oops.");
atomic_inc(&threads_fault);
atomic_dec(&thread_count);
return;
}
 
rwlock_write_unlock(&rwlock);
printf("cpu%d, tid %d w-\n", CPU->id, THREAD->tid);
if (!sh_quiet)
printf("cpu%d, tid %d w-\n", CPU->id, THREAD->tid);
atomic_dec(&thread_count);
}
 
static void reader(void *arg)
101,49 → 117,68
waitq_sleep(&can_start);
to = random(2000);
printf("cpu%d, tid %d r+ (%d)\n", CPU->id, THREAD->tid, to);
if (!sh_quiet)
printf("cpu%d, tid %d r+ (%d)\n", CPU->id, THREAD->tid, to);
rc = rwlock_read_lock_timeout(&rwlock, to);
if (SYNCH_FAILED(rc)) {
printf("cpu%d, tid %d r!\n", CPU->id, THREAD->tid);
if (!sh_quiet)
printf("cpu%d, tid %d r!\n", CPU->id, THREAD->tid);
atomic_dec(&thread_count);
return;
}
printf("cpu%d, tid %d r=\n", CPU->id, THREAD->tid);
if (!sh_quiet)
printf("cpu%d, tid %d r=\n", CPU->id, THREAD->tid);
thread_usleep(30000);
rwlock_read_unlock(&rwlock);
printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid);
if (!sh_quiet)
printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid);
atomic_dec(&thread_count);
}
 
char * test_rwlock4(bool quiet)
{
context_t ctx;
uint32_t i, k;
uint32_t i;
sh_quiet = quiet;
waitq_initialize(&can_start);
rwlock_initialize(&rwlock);
atomic_set(&threads_fault, 0);
uint32_t rd = random(7) + 1;
uint32_t wr = random(5) + 1;
atomic_set(&thread_count, rd + wr);
thread_t *thrd;
context_save(&ctx);
printf("sp=%#x, readers_in=%d\n", ctx.sp, rwlock.readers_in);
if (!quiet) {
printf("sp=%#x, readers_in=%d\n", ctx.sp, rwlock.readers_in);
printf("Creating %d readers\n", rd);
}
k = random(7) + 1;
printf("Creating %d readers\n", k);
for (i = 0; i < k; i++) {
for (i = 0; i < rd; i++) {
thrd = thread_create(reader, NULL, TASK, 0, "reader", false);
if (thrd)
thread_ready(thrd);
else
else if (!quiet)
printf("Could not create reader %d\n", i);
}
 
k = random(5) + 1;
printf("Creating %d writers\n", k);
for (i = 0; i < k; i++) {
if (!quiet)
printf("Creating %d writers\n", wr);
for (i = 0; i < wr; i++) {
thrd = thread_create(writer, NULL, TASK, 0, "writer", false);
if (thrd)
thread_ready(thrd);
else
else if (!quiet)
printf("Could not create writer %d\n", i);
}
150,6 → 185,12
thread_usleep(20000);
waitq_wakeup(&can_start, WAKEUP_ALL);
while (atomic_get(&thread_count) > 0) {
if (!quiet)
printf("Threads left: %d\n", atomic_get(&thread_count));
thread_sleep(1);
}
if (atomic_get(&threads_fault) == 0)
return NULL;