MOS Source Code
Loading...
Searching...
No Matches
timer.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
4
10#include "mos/tasks/signal.hpp"
11
14
15static bool timer_do_wakeup(ktimer_t *timer, void *arg)
16{
17 MOS_UNUSED(arg);
18
19 if (timer->thread)
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 return -ENOTSUP;
43
44 const u64 offset = ms * active_clocksource->frequency / 1000;
45 const u64 target_val = active_clocksource_ticks() + offset;
46
47 ktimer_t timer = {
48 .timeout = target_val,
49 .thread = current_thread,
50 .ticked = false,
51 .callback = timer_do_wakeup,
52 .arg = NULL,
53 };
54
58
59 while (!timer.ticked)
60 {
63 {
65 list_remove(&timer);
67 return -EINTR; // interrupted by signal
68 }
69 }
70
72 return 0;
73}
#define MOS_ASSERT(cond)
Definition assert.hpp:19
clocksource_t * active_clocksource
#define active_clocksource_ticks()
bool signal_has_pending(void)
Return true if there's a pending signal.
Definition signal.cpp:307
MOSAPI void list_node_append(list_node_t *head, list_node_t *item)
Definition list.cpp:68
#define list_foreach(t, v, h)
Iterate over a list.
Definition list.hpp:89
#define list_node(element)
Get the ‘list_node’ of a list element. This is exactly the reverse of ‘list_entry’ above.
Definition list.hpp:74
list_node_t list_head
A linked list head.
Definition list.hpp:23
MOSAPI bool list_is_empty(const list_node_t *head)
Definition list.cpp:21
#define list_remove(element)
Definition list.hpp:80
#define MOS_UNUSED(x)
Definition mos_global.h:65
#define NULL
Definition pb_syshdr.h:46
#define current_thread
Definition platform.hpp:33
void blocked_reschedule(void)
Mark the current task as blocked and reschedule.
Definition schedule.cpp:163
void scheduler_wake_thread(Thread *thread)
Wake a thread.
Definition schedule.cpp:90
#define spinlock_acquire(lock)
Definition spinlock.hpp:64
#define spinlock_release(lock)
Definition spinlock.hpp:65
Thread * thread
Definition timer.hpp:16
bool ticked
Definition timer.hpp:17
void timer_tick()
Definition timer.cpp:25
long timer_msleep(u64 ms)
Definition timer.cpp:39
static spinlock_t timer_queue_lock
Definition timer.cpp:13
static bool timer_do_wakeup(ktimer_t *timer, void *arg)
Definition timer.cpp:15
static list_head timer_queue
list of timers that are waiting to be executed
Definition timer.cpp:12
unsigned long long u64
Definition types.h:19