matos/core/wait.c

66 lines
1.1 KiB
C
Raw Permalink Normal View History

2020-08-15 23:31:35 +02:00
#include "irq.h"
#include "kthread.h"
#include "list.h"
2020-08-18 14:12:45 +02:00
#include "wait.h"
2020-08-15 23:31:35 +02:00
2020-08-19 14:31:16 +02:00
static struct wait_queue *waitQueues = NULL;
int waitQueueInit(struct wait_queue *wq)
{
uint32_t flags;
disable_IRQs(flags);
list_add_tail(waitQueues, wq);
restore_IRQs(flags);
return 0;
}
int waitQueueFree(struct wait_queue *wq)
{
uint32_t flags;
disable_IRQs(flags);
list_delete(waitQueues, wq);
restore_IRQs(flags);
return 0;
}
int wakeUp(struct wait_queue *wq)
2020-08-15 23:31:35 +02:00
{
struct kthread *th;
uint32_t flags;
disable_IRQs(flags);
list_collapse(wq->thread, th)
{
kthreadAddThread(th);
}
restore_IRQs(flags);
return 0;
}
int wait(struct wait_queue *wq)
{
2020-08-18 14:12:45 +02:00
return waitTimeout(wq, 0);
}
int waitTimeout(struct wait_queue *wq, unsigned long msec)
{
struct kthread *current, *next;
2020-08-15 23:31:35 +02:00
uint32_t flags;
2020-08-18 14:12:45 +02:00
int ret;
2020-08-15 23:31:35 +02:00
disable_IRQs(flags);
2021-01-22 22:59:45 +01:00
current = getCurrentThread();
2020-08-18 14:12:45 +02:00
current->state = WAITING;
2021-01-22 22:59:45 +01:00
next = kthreadSelectNext();
2020-08-15 23:31:35 +02:00
kthreadUnsched(current);
2020-08-18 14:12:45 +02:00
2020-08-15 23:31:35 +02:00
list_add_tail(wq->thread, current);
2020-08-18 14:12:45 +02:00
ret = kthreadWait(current, next, msec);
2020-08-15 23:31:35 +02:00
restore_IRQs(flags);
2020-08-18 14:12:45 +02:00
return ret;
2020-08-15 23:31:35 +02:00
}