HelenOS sources
This source file includes following definitions.
- timeout_init
- timeout_initialize
- timeout_deadline_in_usec
- timeout_register_deadline_locked
- timeout_register
- timeout_register_deadline
- timeout_unregister
#include <time/timeout.h>
#include <typedefs.h>
#include <config.h>
#include <panic.h>
#include <synch/spinlock.h>
#include <halt.h>
#include <cpu.h>
#include <arch/asm.h>
#include <arch.h>
void timeout_init(void)
{
irq_spinlock_initialize(&CPU->timeoutlock, "cpu.timeoutlock");
list_initialize(&CPU->timeout_active_list);
}
void timeout_initialize(timeout_t *timeout)
{
link_initialize(&timeout->link);
timeout->cpu = NULL;
}
deadline_t timeout_deadline_in_usec(uint32_t usec)
{
if (usec == 0)
return 0;
return CPU_LOCAL->current_clock_tick + us2ticks(usec);
}
static void timeout_register_deadline_locked(timeout_t *timeout, deadline_t deadline,
timeout_handler_t handler, void *arg)
{
assert(!link_in_use(&timeout->link));
*timeout = (timeout_t) {
.cpu = CPU,
.deadline = deadline,
.handler = handler,
.arg = arg,
.finished = ATOMIC_VAR_INIT(false),
};
link_t *last = list_last(&CPU->timeout_active_list);
if (last == NULL || timeout->deadline >= list_get_instance(last, timeout_t, link)->deadline) {
list_append(&timeout->link, &CPU->timeout_active_list);
} else {
for (link_t *cur = list_first(&CPU->timeout_active_list); cur != NULL;
cur = list_next(cur, &CPU->timeout_active_list)) {
if (timeout->deadline < list_get_instance(cur, timeout_t, link)->deadline) {
list_insert_before(&timeout->link, cur);
break;
}
}
}
}
void timeout_register(timeout_t *timeout, uint64_t time,
timeout_handler_t handler, void *arg)
{
irq_spinlock_lock(&CPU->timeoutlock, true);
timeout_register_deadline_locked(timeout, timeout_deadline_in_usec(time), handler, arg);
irq_spinlock_unlock(&CPU->timeoutlock, true);
}
void timeout_register_deadline(timeout_t *timeout, deadline_t deadline,
timeout_handler_t handler, void *arg)
{
irq_spinlock_lock(&CPU->timeoutlock, true);
timeout_register_deadline_locked(timeout, deadline, handler, arg);
irq_spinlock_unlock(&CPU->timeoutlock, true);
}
bool timeout_unregister(timeout_t *timeout)
{
if (atomic_load_explicit(&timeout->finished, memory_order_acquire))
return false;
assert(timeout->cpu);
irq_spinlock_lock(&timeout->cpu->timeoutlock, true);
bool success = link_in_use(&timeout->link);
if (success) {
list_remove(&timeout->link);
}
irq_spinlock_unlock(&timeout->cpu->timeoutlock, true);
if (!success) {
while (!atomic_load_explicit(&timeout->finished, memory_order_acquire))
cpu_spin_hint();
}
return success;
}
HelenOS homepage, sources at GitHub