46 lines
823 B
C
46 lines
823 B
C
#include <rt/assert.h>
|
|
#include <rt/log.h>
|
|
#include <rt/rwlock.h>
|
|
#include <rt/task.h>
|
|
#include <rt/trap.h>
|
|
|
|
static RT_RWLOCK(lock);
|
|
static unsigned x = 0;
|
|
static unsigned y = 0;
|
|
|
|
static void reader(void)
|
|
{
|
|
rt_task_drop_privilege();
|
|
for (;;)
|
|
{
|
|
rt_rwlock_rdlock(&lock);
|
|
rt_assert(x == y, "x and y do not match");
|
|
rt_rwlock_unlock(&lock);
|
|
}
|
|
}
|
|
|
|
static void writer(void)
|
|
{
|
|
rt_task_drop_privilege();
|
|
for (;;)
|
|
{
|
|
rt_rwlock_wrlock(&lock);
|
|
++x;
|
|
++y;
|
|
rt_rwlock_unlock(&lock);
|
|
rt_task_sleep(1);
|
|
}
|
|
}
|
|
|
|
static void timeout(void)
|
|
{
|
|
rt_task_drop_privilege();
|
|
rt_task_sleep(1000);
|
|
rt_trap();
|
|
}
|
|
|
|
RT_TASK(reader, RT_STACK_MIN, 1);
|
|
RT_TASK(reader, RT_STACK_MIN, 1);
|
|
RT_TASK(writer, RT_STACK_MIN, 1);
|
|
RT_TASK(timeout, RT_STACK_MIN, 2);
|