MOS Source Code
Loading...
Searching...
No Matches
timer.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/device/timer.h"
4
10#include "mos/tasks/signal.h"
11
14
15static bool timer_do_wakeup(ktimer_t *timer, void *arg)
16{
17 MOS_UNUSED(arg);
18
19 if (timer->thread != NULL)
21
22 return true;
23}
24
26{
29 {
30 if (active_clocksource_ticks() >= (u64) timer->timeout)
31 {
32 if (timer->callback(timer, timer->arg))
33 timer->ticked = true, list_remove(timer);
34 }
35 }
37}
38
40{
42 {
43 spinlock_acquire(&current_thread->state_lock);
44 reschedule(); // simulate a reschedule if no clocksource is available
45 return 0;
46 }
47
48 const u64 offset = ms * active_clocksource->frequency / 1000;
49 const u64 target_val = active_clocksource_ticks() + offset;
50
51 ktimer_t timer = {
52 .list_node = LIST_NODE_INIT(timer),
53 .timeout = target_val,
54 .thread = current_thread,
55 .ticked = false,
56 .callback = timer_do_wakeup,
57 .arg = NULL,
58 };
59
63
64 while (!timer.ticked)
65 {
68 {
70 list_remove(&timer);
72 return -EINTR; // interrupted by signal
73 }
74 }
75
77 return 0;
78}
#define MOS_ASSERT(cond)
Definition assert.h:14
clocksource_t * active_clocksource
Definition clocksource.c:8
#define active_clocksource_ticks()
Definition clocksource.h:18
bool signal_has_pending(void)
Return true if there's a pending signal.
Definition signal.c:301
#define LIST_HEAD_INIT(container)
Definition list.h:38
MOSAPI void list_node_append(list_node_t *head, list_node_t *item)
Definition list.c:68
#define list_foreach(t, v, h)
Iterate over a list.
Definition list.h:83
#define list_node(element)
Get the ‘list_node’ of a list element. This is exactly the reverse of ‘list_entry’ above.
Definition list.h:68
list_node_t list_head
A linked list head.
Definition list.h:23
MOSAPI bool list_is_empty(const list_node_t *head)
Definition list.c:21
#define LIST_NODE_INIT(container)
Definition list.h:41
#define list_remove(element)
Definition list.h:74
#define MOS_UNUSED(x)
Definition mos_global.h:64
#define NULL
Definition pb_syshdr.h:46
#define current_thread
Definition platform.h:30
void blocked_reschedule(void)
Mark the current task as blocked and reschedule.
Definition schedule.c:165
void scheduler_wake_thread(thread_t *thread)
Wake a thread.
Definition schedule.c:91
void reschedule(void)
reschedule.
Definition schedule.c:107
#define spinlock_acquire(lock)
Definition spinlock.h:61
#define SPINLOCK_INIT
Definition spinlock.h:28
#define spinlock_release(lock)
Definition spinlock.h:62
thread_t * thread
Definition timer.h:16
bool ticked
Definition timer.h:17
void timer_tick()
Definition timer.c:25
long timer_msleep(u64 ms)
Definition timer.c:39
static spinlock_t timer_queue_lock
Definition timer.c:13
static bool timer_do_wakeup(ktimer_t *timer, void *arg)
Definition timer.c:15
static list_head timer_queue
list of timers that are waiting to be executed
Definition timer.c:12
unsigned long long u64
Definition types.h:23