matos/core/wait.c

46 lines
781 B
C
Raw 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
int wake_up(struct wait_queue *wq)
{
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);
2020-08-16 00:24:59 +02:00
current = getCurrentThread();
2020-08-18 14:12:45 +02:00
current->state = WAITING;
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
}