matos/core/wait.c

66 lines
1.1 KiB
C
Raw Normal View History

2020-08-15 23:31:35 +02:00
#include "irq.h"
2021-10-30 14:18:21 +02:00
#include "thread.h"
2020-08-15 23:31:35 +02:00
#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
{
2021-10-30 14:08:12 +02:00
struct thread *th;
2020-08-15 23:31:35 +02:00
uint32_t flags;
disable_IRQs(flags);
list_collapse(wq->thread, th)
{
2021-10-30 14:18:21 +02:00
threadAddThread(th);
2020-08-15 23:31:35 +02:00
}
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)
{
2021-10-30 14:08:12 +02:00
struct thread *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-10-30 14:18:21 +02:00
next = threadSelectNext();
threadUnsched(current);
2020-08-18 14:12:45 +02:00
2020-08-15 23:31:35 +02:00
list_add_tail(wq->thread, current);
2021-10-30 14:18:21 +02:00
ret = threadWait(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
}