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 {
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 .timeout = target_val,
53 .thread = current_thread,
54 .ticked = false,
55 .callback = timer_do_wakeup,
56 .arg = NULL,
57 };
58
62
63 while (!timer.ticked)
64 {
67 {
69 list_remove(&timer);
71 return -EINTR; // interrupted by signal
72 }
73 }
74
76 return 0;
77}
#define MOS_ASSERT(cond)
Definition assert.hpp:14
clocksource_t * active_clocksource
#define active_clocksource_ticks()
bool signal_has_pending(void)
Return true if there's a pending signal.
Definition signal.cpp:296
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:32
void blocked_reschedule(void)
Mark the current task as blocked and reschedule.
Definition schedule.cpp:164
void reschedule(void)
reschedule.
Definition schedule.cpp:107
void scheduler_wake_thread(Thread *thread)
Wake a thread.
Definition schedule.cpp:91
#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