Files
haiku-beta6/src/system/kernel/timer.c
T

278 lines
6.1 KiB
C
Raw Normal View History

2002-07-09 12:24:59 +00:00
/* Policy info for timers */
2002-07-09 12:24:59 +00:00
/*
** Copyright 2002-2004, The OpenBeOS Team. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
**
2002-07-09 12:24:59 +00:00
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <OS.h>
2002-07-09 12:24:59 +00:00
#include <timer.h>
#include <arch/timer.h>
#include <smp.h>
2003-10-07 23:12:37 +00:00
#include <boot/kernel_args.h>
2002-07-09 12:24:59 +00:00
static timer * volatile sEvents[B_MAX_CPU_COUNT] = { NULL, };
static spinlock sTimerSpinlock[B_MAX_CPU_COUNT] = { 0, };
2002-07-09 12:24:59 +00:00
//#define TRACE_TIMER
#ifdef TRACE_TIMER
# define TRACE(x) dprintf x
#else
# define TRACE(x) ;
#endif
#if __INTEL__
# define PAUSE() asm volatile ("pause;")
#else
# define PAUSE()
#endif
status_t
timer_init(kernel_args *args)
2002-07-09 12:24:59 +00:00
{
TRACE(("timer_init: entry\n"));
2002-07-09 12:24:59 +00:00
return arch_init_timer(args);
2002-07-09 12:24:59 +00:00
}
/** NOTE: expects interrupts to be off */
static void
add_event_to_list(timer *event, timer * volatile *list)
2002-07-09 12:24:59 +00:00
{
timer *next;
timer *last = NULL;
2002-07-09 12:24:59 +00:00
// stick it in the event list
for (next = *list; next; last = next, next = (timer *)next->entry.next) {
if ((bigtime_t)next->entry.key >= (bigtime_t)event->entry.key)
break;
2002-07-09 12:24:59 +00:00
}
if (last != NULL) {
event->entry.next = last->entry.next;
last->entry.next = (qent*)event;
} else {
event->entry.next = (qent*)next;
2002-07-09 12:24:59 +00:00
*list = event;
}
}
int32
timer_interrupt()
2002-07-09 12:24:59 +00:00
{
timer *event;
spinlock *spinlock;
int currentCPU = smp_get_current_cpu();
int32 rc = B_HANDLED_INTERRUPT;
2002-07-09 12:24:59 +00:00
TRACE(("timer_interrupt: time 0x%x 0x%x, cpu %d\n", system_time(), smp_get_current_cpu()));
2002-07-09 12:24:59 +00:00
spinlock = &sTimerSpinlock[currentCPU];
2002-07-09 12:24:59 +00:00
acquire_spinlock(spinlock);
restart_scan:
event = sEvents[currentCPU];
if (event != NULL && ((bigtime_t)event->entry.key < system_time())) {
2002-07-09 12:24:59 +00:00
// this event needs to happen
int mode = event->flags;
2002-07-09 12:24:59 +00:00
sEvents[currentCPU] = (timer *)event->entry.next;
event->entry.key = 0;
2002-07-09 12:24:59 +00:00
release_spinlock(spinlock);
2007-12-25 14:52:49 +00:00
TRACE(("timer_interrupt: calling hook %p for event %p\n", event->hook, event));
2002-07-09 12:24:59 +00:00
// call the callback
// note: if the event is not periodic, it is ok
// to delete the event structure inside the callback
if (event->hook) {
rc = event->hook(event);
2002-07-19 16:07:36 +00:00
// if (event->func(event->data) == INT_RESCHEDULE)
// rc = INT_RESCHEDULE;
2002-07-09 12:24:59 +00:00
}
acquire_spinlock(spinlock);
if (mode == B_PERIODIC_TIMER) {
2002-07-09 12:24:59 +00:00
// we need to adjust it and add it back to the list
bigtime_t scheduleTime = system_time() + event->period;
if (scheduleTime == 0) {
// if we wrapped around and happen to hit zero, set
// it to one, since zero represents not scheduled
scheduleTime = 1;
}
event->entry.key = (int64)scheduleTime;
add_event_to_list(event, &sEvents[currentCPU]);
2002-07-09 12:24:59 +00:00
}
goto restart_scan; // the list may have changed
}
// setup the next hardware timer
if (sEvents[currentCPU] != NULL)
arch_timer_set_hardware_timer((bigtime_t)sEvents[currentCPU]->entry.key - system_time());
2002-07-09 12:24:59 +00:00
release_spinlock(spinlock);
return rc;
}
status_t
add_timer(timer *event, timer_hook hook, bigtime_t period, int32 flags)
2002-07-09 12:24:59 +00:00
{
bigtime_t scheduleTime;
bigtime_t currentTime = system_time();
cpu_status state;
int currentCPU;
if (event == NULL || hook == NULL || period < 0)
return B_BAD_VALUE;
2007-12-25 14:52:49 +00:00
TRACE(("add_timer: event %p\n", event));
scheduleTime = period;
if (flags != B_ONE_SHOT_ABSOLUTE_TIMER)
scheduleTime += currentTime;
if (scheduleTime == 0)
scheduleTime = 1;
event->entry.key = (int64)scheduleTime;
event->period = period;
event->hook = hook;
event->flags = flags;
2002-07-09 12:24:59 +00:00
state = disable_interrupts();
currentCPU = smp_get_current_cpu();
acquire_spinlock(&sTimerSpinlock[currentCPU]);
2002-07-09 12:24:59 +00:00
add_event_to_list(event, &sEvents[currentCPU]);
event->cpu = currentCPU;
2002-07-09 12:24:59 +00:00
// if we were stuck at the head of the list, set the hardware timer
if (event == sEvents[currentCPU])
arch_timer_set_hardware_timer(scheduleTime - currentTime);
2002-07-09 12:24:59 +00:00
release_spinlock(&sTimerSpinlock[currentCPU]);
restore_interrupts(state);
2002-07-09 12:24:59 +00:00
return B_OK;
2002-07-09 12:24:59 +00:00
}
/** This is a fast path to be called from reschedule() and from
* cancel_timer().
* Must always be invoked with interrupts disabled.
*/
status_t
_local_timer_cancel_event(int cpu, timer *event)
2002-07-09 12:24:59 +00:00
{
timer *last = NULL;
timer *current;
2002-07-09 12:24:59 +00:00
acquire_spinlock(&sTimerSpinlock[cpu]);
current = sEvents[cpu];
while (current != NULL) {
if (current == event) {
2002-07-09 12:24:59 +00:00
// we found it
if (current == sEvents[cpu])
sEvents[cpu] = (timer *)current->entry.next;
else
last->entry.next = current->entry.next;
current->entry.next = NULL;
2002-07-09 12:24:59 +00:00
// break out of the whole thing
break;
2002-07-09 12:24:59 +00:00
}
last = current;
current = (timer *)current->entry.next;
2002-07-09 12:24:59 +00:00
}
if (sEvents[cpu] == NULL)
2002-07-09 12:24:59 +00:00
arch_timer_clear_hardware_timer();
else
arch_timer_set_hardware_timer((bigtime_t)sEvents[cpu]->entry.key - system_time());
2002-07-09 12:24:59 +00:00
release_spinlock(&sTimerSpinlock[cpu]);
2002-07-09 12:24:59 +00:00
return current == event ? B_OK : B_ERROR;
2002-07-09 12:24:59 +00:00
}
bool
cancel_timer(timer *event)
{
int currentCPU = smp_get_current_cpu();
cpu_status state;
2002-07-09 12:24:59 +00:00
2007-12-25 14:52:49 +00:00
TRACE(("cancel_timer: event %p\n", event));
state = disable_interrupts();
2002-07-09 12:24:59 +00:00
// walk through all of the cpu's timer queues
//
// We start by peeking our own queue, aiming for
// a cheap match. If this fails, we start harassing
// other cpus.
if (_local_timer_cancel_event(currentCPU, event) < 0) {
int numCPUs = smp_get_num_cpus();
int cpu = 0;
timer *last = NULL;
timer *current;
for (cpu = 0; cpu < numCPUs; cpu++) {
if (cpu == currentCPU)
continue;
acquire_spinlock(&sTimerSpinlock[cpu]);
current = sEvents[cpu];
while (current != NULL) {
if (current == event) {
2002-07-09 12:24:59 +00:00
// we found it
if (current == sEvents[cpu])
sEvents[cpu] = (timer *)current->entry.next;
else
last->entry.next = current->entry.next;
current->entry.next = NULL;
2002-07-09 12:24:59 +00:00
// break out of the whole thing
release_spinlock(&sTimerSpinlock[cpu]);
restore_interrupts(state);
return (bigtime_t)event->entry.key < system_time();
2002-07-09 12:24:59 +00:00
}
last = current;
current = (timer *)current->entry.next;
2002-07-09 12:24:59 +00:00
}
release_spinlock(&sTimerSpinlock[cpu]);
2002-07-09 12:24:59 +00:00
}
}
restore_interrupts(state);
return false;
2002-07-09 12:24:59 +00:00
}
void
spin(bigtime_t microseconds)
2002-07-09 12:24:59 +00:00
{
bigtime_t time = system_time();
while((system_time() - time) < microseconds)
PAUSE();
2002-07-09 12:24:59 +00:00
}