[kernel] sync with external source. support for dynamic timer
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Travis Geiselbrecht
|
||||
* Copyright (c) 2008-2009 Travis Geiselbrecht
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Travis Geiselbrecht
|
||||
* Copyright (c) 2008-2009 Travis Geiselbrecht
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
@@ -60,6 +60,11 @@ typedef unsigned long time_t;
|
||||
typedef unsigned long long bigtime_t;
|
||||
#define INFINITE_TIME ULONG_MAX
|
||||
|
||||
#define TIME_GTE(a, b) ((long)((a) - (b)) >= 0)
|
||||
#define TIME_LTE(a, b) ((long)((a) - (b)) <= 0)
|
||||
#define TIME_GT(a, b) ((long)((a) - (b)) > 0)
|
||||
#define TIME_LT(a, b) ((long)((a) - (b)) < 0)
|
||||
|
||||
enum handler_return {
|
||||
INT_NO_RESCHEDULE = 0,
|
||||
INT_RESCHEDULE,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Travis Geiselbrecht
|
||||
* Copyright (c) 2008-2009 Travis Geiselbrecht
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
@@ -26,7 +26,7 @@
|
||||
#include <kernel/timer.h>
|
||||
#include <platform.h>
|
||||
|
||||
#if defined(WITH_LIB_CONSOLE)
|
||||
#if WITH_LIB_CONSOLE
|
||||
#include <lib/console.h>
|
||||
|
||||
static int cmd_threads(int argc, const cmd_args *argv);
|
||||
@@ -35,11 +35,11 @@ static int cmd_threadload(int argc, const cmd_args *argv);
|
||||
|
||||
STATIC_COMMAND_START
|
||||
#if DEBUGLEVEL > 1
|
||||
{ "threads", "list kernel threads", &cmd_threads },
|
||||
STATIC_COMMAND("threads", "list kernel threads", &cmd_threads)
|
||||
#endif
|
||||
#if THREAD_STATS
|
||||
{ "threadstats", "thread level statistics", &cmd_threadstats },
|
||||
{ "threadload", "toggle thread load display", &cmd_threadload },
|
||||
STATIC_COMMAND("threadstats", "thread level statistics", &cmd_threadstats)
|
||||
STATIC_COMMAND("threadload", "toggle thread load display", &cmd_threadload)
|
||||
#endif
|
||||
STATIC_COMMAND_END(kernel);
|
||||
|
||||
@@ -75,8 +75,6 @@ static enum handler_return threadload(struct timer *t, time_t now, void *arg)
|
||||
static struct thread_stats old_stats;
|
||||
static bigtime_t last_idle_time;
|
||||
|
||||
timer_set_oneshot(t, 1000, &threadload, NULL);
|
||||
|
||||
bigtime_t idle_time = thread_stats.idle_time;
|
||||
if (current_thread == idle_thread) {
|
||||
idle_time += current_time_hires() - thread_stats.last_idle_timestamp;
|
||||
@@ -108,7 +106,7 @@ static int cmd_threadload(int argc, const cmd_args *argv)
|
||||
if (showthreadload == false) {
|
||||
// start the display
|
||||
timer_initialize(&tltimer);
|
||||
timer_set_oneshot(&tltimer, 1000, &threadload, NULL);
|
||||
timer_set_periodic(&tltimer, 1000, &threadload, NULL);
|
||||
showthreadload = true;
|
||||
} else {
|
||||
timer_cancel(&tltimer);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Travis Geiselbrecht
|
||||
* Copyright (c) 2008-2009 Travis Geiselbrecht
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Travis Geiselbrecht
|
||||
* Copyright (c) 2008-2009 Travis Geiselbrecht
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
@@ -49,9 +49,9 @@ void mutex_destroy(mutex_t *m)
|
||||
ASSERT(m->magic == MUTEX_MAGIC);
|
||||
#endif
|
||||
|
||||
if (m->holder != 0 && current_thread != m->holder)
|
||||
panic("mutex_destroy: thread %p (%s) tried to release mutex %p it doesn't own. owned by %p (%s)\n",
|
||||
current_thread, current_thread->name, m, m->holder, m->holder ? m->holder->name : "none");
|
||||
// if (m->holder != 0 && current_thread != m->holder)
|
||||
// panic("mutex_destroy: thread %p (%s) tried to release mutex %p it doesn't own. owned by %p (%s)\n",
|
||||
// current_thread, current_thread->name, m, m->holder, m->holder ? m->holder->name : "none");
|
||||
|
||||
m->magic = 0;
|
||||
m->count = 0;
|
||||
|
||||
102
kernel/timer.c
102
kernel/timer.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Travis Geiselbrecht
|
||||
* Copyright (c) 2008-2009 Travis Geiselbrecht
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
@@ -29,6 +29,8 @@
|
||||
|
||||
static struct list_node timer_queue;
|
||||
|
||||
static enum handler_return timer_tick(void *arg, time_t now);
|
||||
|
||||
void timer_initialize(timer_t *timer)
|
||||
{
|
||||
timer->magic = TIMER_MAGIC;
|
||||
@@ -43,8 +45,10 @@ static void insert_timer_in_queue(timer_t *timer)
|
||||
{
|
||||
timer_t *entry;
|
||||
|
||||
// TRACEF("timer %p, scheduled %d, periodic %d\n", timer, timer->scheduled_time, timer->periodic_time);
|
||||
|
||||
list_for_every_entry(&timer_queue, entry, timer_t, node) {
|
||||
if (entry->scheduled_time > timer->scheduled_time) {
|
||||
if (TIME_GT(entry->scheduled_time, timer->scheduled_time)) {
|
||||
list_add_before(&entry->node, &timer->node);
|
||||
return;
|
||||
}
|
||||
@@ -54,11 +58,11 @@ static void insert_timer_in_queue(timer_t *timer)
|
||||
list_add_tail(&timer_queue, &timer->node);
|
||||
}
|
||||
|
||||
void timer_set_oneshot(timer_t *timer, time_t delay, timer_callback callback, void *arg)
|
||||
static void timer_set(timer_t *timer, time_t delay, time_t period, timer_callback callback, void *arg)
|
||||
{
|
||||
time_t now;
|
||||
|
||||
// TRACEF("delay %d, callback %p, arg %p\n", delay, callback, arg);
|
||||
// TRACEF("timer %p, delay %d, period %d, callback %p, arg %p, now %d\n", timer, delay, period, callback, arg);
|
||||
|
||||
DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
|
||||
|
||||
@@ -68,7 +72,7 @@ void timer_set_oneshot(timer_t *timer, time_t delay, timer_callback callback, vo
|
||||
|
||||
now = current_time();
|
||||
timer->scheduled_time = now + delay;
|
||||
timer->periodic_time = 0;
|
||||
timer->periodic_time = period;
|
||||
timer->callback = callback;
|
||||
timer->arg = arg;
|
||||
|
||||
@@ -78,18 +82,71 @@ void timer_set_oneshot(timer_t *timer, time_t delay, timer_callback callback, vo
|
||||
|
||||
insert_timer_in_queue(timer);
|
||||
|
||||
#if PLATFORM_HAS_DYNAMIC_TIMER
|
||||
if (list_peek_head_type(&timer_queue, timer_t, node) == timer) {
|
||||
/* we just modified the head of the timer queue */
|
||||
// TRACEF("setting new timer for %u msecs\n", (uint)delay);
|
||||
platform_set_oneshot_timer(timer_tick, NULL, delay);
|
||||
}
|
||||
#endif
|
||||
|
||||
exit_critical_section();
|
||||
}
|
||||
|
||||
void timer_set_oneshot(timer_t *timer, time_t delay, timer_callback callback, void *arg)
|
||||
{
|
||||
if (delay == 0)
|
||||
delay = 1;
|
||||
timer_set(timer, delay, 0, callback, arg);
|
||||
}
|
||||
|
||||
void timer_set_periodic(timer_t *timer, time_t period, timer_callback callback, void *arg)
|
||||
{
|
||||
if (period == 0)
|
||||
period = 1;
|
||||
timer_set(timer, period, period, callback, arg);
|
||||
}
|
||||
|
||||
void timer_cancel(timer_t *timer)
|
||||
{
|
||||
DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
|
||||
|
||||
enter_critical_section();
|
||||
|
||||
#if PLATFORM_HAS_DYNAMIC_TIMER
|
||||
timer_t *oldhead = list_peek_head_type(&timer_queue, timer_t, node);
|
||||
#endif
|
||||
|
||||
if (list_in_list(&timer->node))
|
||||
list_delete(&timer->node);
|
||||
|
||||
/* to keep it from being reinserted into the queue if called from
|
||||
* periodic timer callback.
|
||||
*/
|
||||
timer->periodic_time = 0;
|
||||
timer->callback = NULL;
|
||||
timer->arg = NULL;
|
||||
|
||||
#if PLATFORM_HAS_DYNAMIC_TIMER
|
||||
/* see if we've just modified the head of the timer queue */
|
||||
timer_t *newhead = list_peek_head_type(&timer_queue, timer_t, node);
|
||||
if (newhead == NULL) {
|
||||
// TRACEF("clearing old hw timer, nothing in the queue\n");
|
||||
platform_stop_timer();
|
||||
} else if (newhead != oldhead) {
|
||||
time_t delay;
|
||||
time_t now = current_time();
|
||||
|
||||
if (TIME_LT(newhead->scheduled_time, now))
|
||||
delay = 0;
|
||||
else
|
||||
delay = newhead->scheduled_time - now;
|
||||
|
||||
// TRACEF("setting new timer to %d\n", delay);
|
||||
platform_set_oneshot_timer(timer_tick, NULL, delay);
|
||||
}
|
||||
#endif
|
||||
|
||||
exit_critical_section();
|
||||
}
|
||||
|
||||
@@ -103,10 +160,12 @@ static enum handler_return timer_tick(void *arg, time_t now)
|
||||
thread_stats.timer_ints++;
|
||||
#endif
|
||||
|
||||
// TRACEF("now %d\n", now);
|
||||
|
||||
for (;;) {
|
||||
/* see if there's an event to process */
|
||||
timer = list_peek_head_type(&timer_queue, timer_t, node);
|
||||
if (likely(!timer || now < timer->scheduled_time))
|
||||
if (likely(!timer || TIME_LT(now, timer->scheduled_time)))
|
||||
break;
|
||||
|
||||
/* process it */
|
||||
@@ -115,19 +174,48 @@ static enum handler_return timer_tick(void *arg, time_t now)
|
||||
// timer = list_remove_head_type(&timer_queue, timer_t, node);
|
||||
// ASSERT(timer);
|
||||
|
||||
// TRACEF("dequeued timer %p, scheduled %d periodic %d\n", timer, timer->scheduled_time, timer->periodic_time);
|
||||
|
||||
#if THREAD_STATS
|
||||
thread_stats.timers++;
|
||||
#endif
|
||||
|
||||
// TRACEF("firing callback %p, arg %p\n", timer->callback, timer->arg);
|
||||
bool periodic = timer->periodic_time > 0;
|
||||
|
||||
// TRACEF("timer %p firing callback %p, arg %p\n", timer, timer->callback, timer->arg);
|
||||
if (timer->callback(timer, now, timer->arg) == INT_RESCHEDULE)
|
||||
ret = INT_RESCHEDULE;
|
||||
|
||||
/* if it was a periodic timer and it hasn't been requeued
|
||||
* by the callback put it back in the list
|
||||
*/
|
||||
if (periodic && !list_in_list(&timer->node) && timer->periodic_time > 0) {
|
||||
// TRACEF("periodic timer, period %u\n", (uint)timer->periodic_time);
|
||||
timer->scheduled_time = now + timer->periodic_time;
|
||||
insert_timer_in_queue(timer);
|
||||
}
|
||||
}
|
||||
|
||||
#if PLATFORM_HAS_DYNAMIC_TIMER
|
||||
/* reset the timer to the next event */
|
||||
timer = list_peek_head_type(&timer_queue, timer_t, node);
|
||||
if (timer) {
|
||||
/* has to be the case or it would have fired already */
|
||||
ASSERT(TIME_GT(timer->scheduled_time, now));
|
||||
|
||||
time_t delay = timer->scheduled_time - now;
|
||||
|
||||
// TRACEF("setting new timer for %u msecs for event %p\n", (uint)delay, timer);
|
||||
platform_set_oneshot_timer(timer_tick, NULL, delay);
|
||||
}
|
||||
#else
|
||||
/* let the scheduler have a shot to do quantum expiration, etc */
|
||||
/* in case of dynamic timer, the scheduler will set up a periodic timer */
|
||||
if (thread_timer_tick() == INT_RESCHEDULE)
|
||||
ret = INT_RESCHEDULE;
|
||||
#endif
|
||||
|
||||
// XXX fix this, should return ret
|
||||
return INT_RESCHEDULE;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user