/branches/dynload/kernel/test/synch/rwlock1.c |
---|
35,39 → 35,39 |
#include <synch/waitq.h> |
#include <synch/rwlock.h> |
#define READERS 50 |
#define WRITERS 50 |
#define READERS 50 |
#define WRITERS 50 |
static rwlock_t rwlock; |
char * test_rwlock1(bool quiet) |
char *test_rwlock1(void) |
{ |
rwlock_initialize(&rwlock); |
rwlock_write_lock(&rwlock); |
rwlock_write_unlock(&rwlock); |
rwlock_write_unlock(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_unlock(&rwlock); |
rwlock_read_unlock(&rwlock); |
rwlock_read_unlock(&rwlock); |
rwlock_read_unlock(&rwlock); |
rwlock_read_unlock(&rwlock); |
rwlock_read_unlock(&rwlock); |
rwlock_write_lock(&rwlock); |
rwlock_write_unlock(&rwlock); |
rwlock_write_unlock(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_unlock(&rwlock); |
rwlock_write_lock(&rwlock); |
rwlock_write_unlock(&rwlock); |
rwlock_write_unlock(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_unlock(&rwlock); |
/branches/dynload/kernel/test/synch/rwlock2.c |
---|
34,38 → 34,34 |
#include <synch/rwlock.h> |
#define READERS 50 |
#define WRITERS 50 |
#define READERS 50 |
#define WRITERS 50 |
static rwlock_t rwlock; |
static bool sh_quiet; |
static void writer(void *arg) |
{ |
if (!sh_quiet) |
printf("Trying to lock rwlock for writing....\n"); |
TPRINTF("Trying to lock rwlock for writing....\n"); |
rwlock_write_lock(&rwlock); |
rwlock_write_unlock(&rwlock); |
if (!sh_quiet) |
printf("Trying to lock rwlock for reading....\n"); |
TPRINTF("Trying to lock rwlock for reading....\n"); |
rwlock_read_lock(&rwlock); |
rwlock_read_unlock(&rwlock); |
rwlock_read_unlock(&rwlock); |
} |
char * test_rwlock2(bool quiet) |
char *test_rwlock2(void) |
{ |
thread_t *thrd; |
sh_quiet = quiet; |
rwlock_initialize(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_lock(&rwlock); |
rwlock_read_lock(&rwlock); |
thrd = thread_create(writer, NULL, TASK, 0, "writer", false); |
if (thrd) |
72,7 → 68,7 |
thread_ready(thrd); |
else |
return "Could not create thread"; |
thread_sleep(1); |
rwlock_read_unlock(&rwlock); |
/branches/dynload/kernel/test/synch/rwlock3.c |
---|
34,41 → 34,35 |
#include <synch/rwlock.h> |
#define THREADS 4 |
#define THREADS 4 |
static atomic_t thread_count; |
static rwlock_t rwlock; |
static bool sh_quiet; |
static void reader(void *arg) |
{ |
thread_detach(THREAD); |
if (!sh_quiet) |
printf("cpu%u, tid %" PRIu64 ": trying to lock rwlock for reading....\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 ": trying to lock rwlock for reading....\n", CPU->id, THREAD->tid); |
rwlock_read_lock(&rwlock); |
rwlock_read_unlock(&rwlock); |
if (!sh_quiet) { |
printf("cpu%u, tid %" PRIu64 ": success\n", CPU->id, THREAD->tid); |
printf("cpu%u, tid %" PRIu64 ": trying to lock rwlock for writing....\n", CPU->id, THREAD->tid); |
} |
TPRINTF("cpu%u, tid %" PRIu64 ": success\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 ": trying to lock rwlock for writing....\n", CPU->id, THREAD->tid); |
rwlock_write_lock(&rwlock); |
rwlock_write_unlock(&rwlock); |
if (!sh_quiet) |
printf("cpu%u, tid %" PRIu64 ": success\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 ": success\n", CPU->id, THREAD->tid); |
atomic_dec(&thread_count); |
} |
char * test_rwlock3(bool quiet) |
char *test_rwlock3(void) |
{ |
int i; |
thread_t *thrd; |
sh_quiet = quiet; |
atomic_set(&thread_count, THREADS); |
79,16 → 73,15 |
thrd = thread_create(reader, NULL, TASK, 0, "reader", false); |
if (thrd) |
thread_ready(thrd); |
else if (!quiet) |
printf("Could not create reader %d\n", i); |
else |
TPRINTF("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: %ld\n", atomic_get(&thread_count)); |
TPRINTF("Threads left: %ld\n", atomic_get(&thread_count)); |
thread_sleep(1); |
} |
/branches/dynload/kernel/test/synch/semaphore1.c |
---|
35,9 → 35,9 |
#include <synch/waitq.h> |
#include <synch/semaphore.h> |
#define AT_ONCE 3 |
#define PRODUCERS 50 |
#define CONSUMERS 50 |
#define AT_ONCE 3 |
#define PRODUCERS 50 |
#define CONSUMERS 50 |
static semaphore_t sem; |
47,10 → 47,10 |
static void producer(void *arg) |
{ |
thread_detach(THREAD); |
thread_detach(THREAD); |
waitq_sleep(&can_start); |
semaphore_down(&sem); |
atomic_inc(&items_produced); |
thread_usleep(250); |
59,7 → 59,7 |
static void consumer(void *arg) |
{ |
thread_detach(THREAD); |
thread_detach(THREAD); |
waitq_sleep(&can_start); |
69,7 → 69,7 |
semaphore_up(&sem); |
} |
char * test_semaphore1(bool quiet) |
char *test_semaphore1(void) |
{ |
int i, j, k; |
int consumers, producers; |
76,10 → 76,10 |
waitq_initialize(&can_start); |
semaphore_initialize(&sem, AT_ONCE); |
for (i = 1; i <= 3; i++) { |
thread_t *thrd; |
atomic_set(&items_produced, 0); |
atomic_set(&items_consumed, 0); |
86,8 → 86,8 |
consumers = i * CONSUMERS; |
producers = (4 - i) * PRODUCERS; |
printf("Creating %d consumers and %d producers...", consumers, producers); |
TPRINTF("Creating %d consumers and %d producers...", consumers, producers); |
for (j = 0; j < (CONSUMERS + PRODUCERS) / 2; j++) { |
for (k = 0; k < i; k++) { |
thrd = thread_create(consumer, NULL, TASK, 0, "consumer", false); |
94,7 → 94,7 |
if (thrd) |
thread_ready(thrd); |
else |
printf("could not create consumer %d\n", i); |
TPRINTF("could not create consumer %d\n", i); |
} |
for (k = 0; k < (4 - i); k++) { |
thrd = thread_create(producer, NULL, TASK, 0, "producer", false); |
101,17 → 101,17 |
if (thrd) |
thread_ready(thrd); |
else |
printf("could not create producer %d\n", i); |
TPRINTF("could not create producer %d\n", i); |
} |
} |
printf("ok\n"); |
TPRINTF("ok\n"); |
thread_sleep(1); |
waitq_wakeup(&can_start, WAKEUP_ALL); |
while ((items_consumed.count != consumers) || (items_produced.count != producers)) { |
printf("%d consumers remaining, %d producers remaining\n", consumers - items_consumed.count, producers - items_produced.count); |
TPRINTF("%d consumers remaining, %d producers remaining\n", consumers - items_consumed.count, producers - items_produced.count); |
thread_sleep(1); |
} |
} |
/branches/dynload/kernel/test/synch/rwlock4.c |
---|
40,13 → 40,12 |
#include <synch/synch.h> |
#include <synch/spinlock.h> |
#define READERS 50 |
#define WRITERS 50 |
#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); |
57,8 → 56,8 |
static uint32_t random(uint32_t max) |
{ |
uint32_t rc; |
spinlock_lock(&rw_lock); |
spinlock_lock(&rw_lock); |
rc = seed % max; |
seed = (((seed << 2) ^ (seed >> 2)) * 487) + rc; |
spinlock_unlock(&rw_lock); |
70,43 → 69,39 |
int rc, to; |
thread_detach(THREAD); |
waitq_sleep(&can_start); |
to = random(40000); |
if (!sh_quiet) |
printf("cpu%u, tid %" PRIu64 " w+ (%d)\n", CPU->id, THREAD->tid, to); |
TPRINTF("cpu%u, tid %" PRIu64 " w+ (%d)\n", CPU->id, THREAD->tid, to); |
rc = rwlock_write_lock_timeout(&rwlock, to); |
if (SYNCH_FAILED(rc)) { |
if (!sh_quiet) |
printf("cpu%u, tid %" PRIu64 " w!\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 " w!\n", CPU->id, THREAD->tid); |
atomic_dec(&thread_count); |
return; |
} |
if (!sh_quiet) |
printf("cpu%u, tid %" PRIu64 " w=\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 " w=\n", CPU->id, THREAD->tid); |
if (rwlock.readers_in) { |
if (!sh_quiet) |
printf("Oops."); |
TPRINTF("Oops.\n"); |
atomic_inc(&threads_fault); |
atomic_dec(&thread_count); |
return; |
} |
thread_usleep(random(1000000)); |
if (rwlock.readers_in) { |
if (!sh_quiet) |
printf("Oops."); |
TPRINTF("Oops.\n"); |
atomic_inc(&threads_fault); |
atomic_dec(&thread_count); |
return; |
} |
rwlock_write_unlock(&rwlock); |
if (!sh_quiet) |
printf("cpu%u, tid %" PRIu64 " w-\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 " w-\n", CPU->id, THREAD->tid); |
atomic_dec(&thread_count); |
} |
118,33 → 113,28 |
to = random(2000); |
if (!sh_quiet) |
printf("cpu%u, tid %" PRIu64 " r+ (%d)\n", CPU->id, THREAD->tid, to); |
TPRINTF("cpu%u, tid %" PRIu64 " r+ (%d)\n", CPU->id, THREAD->tid, to); |
rc = rwlock_read_lock_timeout(&rwlock, to); |
if (SYNCH_FAILED(rc)) { |
if (!sh_quiet) |
printf("cpu%u, tid %" PRIu64 " r!\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 " r!\n", CPU->id, THREAD->tid); |
atomic_dec(&thread_count); |
return; |
} |
if (!sh_quiet) |
printf("cpu%u, tid %" PRIu64 " r=\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 " r=\n", CPU->id, THREAD->tid); |
thread_usleep(30000); |
rwlock_read_unlock(&rwlock); |
if (!sh_quiet) |
printf("cpu%u, tid %" PRIu64 " r-\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 " r-\n", CPU->id, THREAD->tid); |
atomic_dec(&thread_count); |
} |
char * test_rwlock4(bool quiet) |
char *test_rwlock4(void) |
{ |
context_t ctx; |
uint32_t i; |
sh_quiet = quiet; |
waitq_initialize(&can_start); |
rwlock_initialize(&rwlock); |
158,28 → 148,25 |
thread_t *thrd; |
context_save(&ctx); |
if (!quiet) { |
printf("sp=%#x, readers_in=%" PRIc "\n", ctx.sp, rwlock.readers_in); |
printf("Creating %" PRIu32 " readers\n", rd); |
} |
TPRINTF("sp=%#x, readers_in=%" PRIc "\n", ctx.sp, rwlock.readers_in); |
TPRINTF("Creating %" PRIu32 " readers\n", rd); |
for (i = 0; i < rd; i++) { |
thrd = thread_create(reader, NULL, TASK, 0, "reader", false); |
if (thrd) |
thread_ready(thrd); |
else if (!quiet) |
printf("Could not create reader %" PRIu32 "\n", i); |
else |
TPRINTF("Could not create reader %" PRIu32 "\n", i); |
} |
if (!quiet) |
printf("Creating %" PRIu32 " writers\n", wr); |
TPRINTF("Creating %" PRIu32 " writers\n", wr); |
for (i = 0; i < wr; i++) { |
thrd = thread_create(writer, NULL, TASK, 0, "writer", false); |
if (thrd) |
thread_ready(thrd); |
else if (!quiet) |
printf("Could not create writer %" PRIu32 "\n", i); |
else |
TPRINTF("Could not create writer %" PRIu32 "\n", i); |
} |
thread_usleep(20000); |
186,8 → 173,7 |
waitq_wakeup(&can_start, WAKEUP_ALL); |
while (atomic_get(&thread_count) > 0) { |
if (!quiet) |
printf("Threads left: %ld\n", atomic_get(&thread_count)); |
TPRINTF("Threads left: %ld\n", atomic_get(&thread_count)); |
thread_sleep(1); |
} |
/branches/dynload/kernel/test/synch/semaphore2.c |
---|
50,8 → 50,8 |
static uint32_t random(uint32_t max) |
{ |
uint32_t rc; |
spinlock_lock(&sem_lock); |
spinlock_lock(&sem_lock); |
rc = seed % max; |
seed = (((seed << 2) ^ (seed >> 2)) * 487) + rc; |
spinlock_unlock(&sem_lock); |
67,21 → 67,21 |
waitq_sleep(&can_start); |
to = random(20000); |
printf("cpu%u, tid %" PRIu64 " down+ (%d)\n", CPU->id, THREAD->tid, to); |
TPRINTF("cpu%u, tid %" PRIu64 " down+ (%d)\n", CPU->id, THREAD->tid, to); |
rc = semaphore_down_timeout(&sem, to); |
if (SYNCH_FAILED(rc)) { |
printf("cpu%u, tid %" PRIu64 " down!\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 " down!\n", CPU->id, THREAD->tid); |
return; |
} |
printf("cpu%u, tid %" PRIu64 " down=\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 " down=\n", CPU->id, THREAD->tid); |
thread_usleep(random(30000)); |
semaphore_up(&sem); |
printf("cpu%u, tid %" PRIu64 " up\n", CPU->id, THREAD->tid); |
TPRINTF("cpu%u, tid %" PRIu64 " up\n", CPU->id, THREAD->tid); |
} |
char * test_semaphore2(bool quiet) |
char *test_semaphore2(void) |
{ |
uint32_t i, k; |
91,13 → 91,13 |
thread_t *thrd; |
k = random(7) + 1; |
printf("Creating %" PRIu32 " consumers\n", k); |
TPRINTF("Creating %" PRIu32 " consumers\n", k); |
for (i = 0; i < k; i++) { |
thrd = thread_create(consumer, NULL, TASK, 0, "consumer", false); |
if (thrd) |
thread_ready(thrd); |
else |
printf("Error creating thread\n"); |
TPRINTF("Error creating thread\n"); |
} |
thread_usleep(20000); |
/branches/dynload/kernel/test/synch/rwlock5.c |
---|
35,8 → 35,8 |
#include <synch/waitq.h> |
#include <synch/rwlock.h> |
#define READERS 50 |
#define WRITERS 50 |
#define READERS 50 |
#define WRITERS 50 |
static rwlock_t rwlock; |
47,9 → 47,9 |
static void writer(void *arg) |
{ |
thread_detach(THREAD); |
waitq_sleep(&can_start); |
rwlock_write_lock(&rwlock); |
atomic_inc(&items_written); |
rwlock_write_unlock(&rwlock); |
58,7 → 58,7 |
static void reader(void *arg) |
{ |
thread_detach(THREAD); |
waitq_sleep(&can_start); |
rwlock_read_lock(&rwlock); |
66,7 → 66,7 |
rwlock_read_unlock(&rwlock); |
} |
char * test_rwlock5(bool quiet) |
char *test_rwlock5(void) |
{ |
int i, j, k; |
long readers, writers; |
76,15 → 76,15 |
for (i = 1; i <= 3; i++) { |
thread_t *thrd; |
atomic_set(&items_read, 0); |
atomic_set(&items_written, 0); |
readers = i * READERS; |
writers = (4 - i) * WRITERS; |
printf("Creating %ld readers and %ld writers...", readers, writers); |
TPRINTF("Creating %ld readers and %ld writers...", readers, writers); |
for (j = 0; j < (READERS + WRITERS) / 2; j++) { |
for (k = 0; k < i; k++) { |
thrd = thread_create(reader, NULL, TASK, 0, "reader", false); |
91,7 → 91,7 |
if (thrd) |
thread_ready(thrd); |
else |
printf("Could not create reader %d\n", k); |
TPRINTF("Could not create reader %d\n", k); |
} |
for (k = 0; k < (4 - i); k++) { |
thrd = thread_create(writer, NULL, TASK, 0, "writer", false); |
98,17 → 98,17 |
if (thrd) |
thread_ready(thrd); |
else |
printf("Could not create writer %d\n", k); |
TPRINTF("Could not create writer %d\n", k); |
} |
} |
printf("ok\n"); |
TPRINTF("ok\n"); |
thread_sleep(1); |
waitq_wakeup(&can_start, WAKEUP_ALL); |
while ((items_read.count != readers) || (items_written.count != writers)) { |
printf("%d readers remaining, %d writers remaining, readers_in=%d\n", readers - items_read.count, writers - items_written.count, rwlock.readers_in); |
TPRINTF("%d readers remaining, %d writers remaining, readers_in=%d\n", readers - items_read.count, writers - items_written.count, rwlock.readers_in); |
thread_usleep(100000); |
} |
} |