2002-07-09 16:24:59 +04:00
|
|
|
/* Policy info for timers */
|
2004-03-14 21:02:22 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
/*
|
2004-03-14 21:02:22 +03:00
|
|
|
** Copyright 2002-2004, The OpenBeOS Team. All rights reserved.
|
|
|
|
** Distributed under the terms of the OpenBeOS License.
|
|
|
|
**
|
2002-07-09 16:24:59 +04:00
|
|
|
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
|
|
|
|
** Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
2003-05-03 20:20:38 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
|
2003-05-03 20:20:38 +04:00
|
|
|
#include <OS.h>
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <timer.h>
|
2004-03-14 21:02:22 +03:00
|
|
|
#include <arch/timer.h>
|
|
|
|
#include <smp.h>
|
2003-10-08 03:12:37 +04:00
|
|
|
#include <boot/kernel_args.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
static timer * volatile sEvents[B_MAX_CPU_COUNT] = { NULL, };
|
|
|
|
static spinlock sTimerSpinlock[B_MAX_CPU_COUNT] = { 0, };
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
//#define TRACE_TIMER
|
|
|
|
#ifdef TRACE_TIMER
|
|
|
|
# define TRACE(x) dprintf x
|
|
|
|
#else
|
|
|
|
# define TRACE(x) ;
|
|
|
|
#endif
|
2003-05-03 20:20:38 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
|
|
|
|
status_t
|
|
|
|
timer_init(kernel_args *args)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-03-14 21:02:22 +03:00
|
|
|
TRACE(("timer_init: entry\n"));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
return arch_init_timer(args);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
|
|
|
|
/** NOTE: expects interrupts to be off */
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_event_to_list(timer *event, timer * volatile *list)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-21 02:52:04 +04:00
|
|
|
timer *next;
|
|
|
|
timer *last = NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// stick it in the event list
|
2002-07-21 02:52:04 +04:00
|
|
|
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 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
if (last != NULL) {
|
|
|
|
(timer *)event->entry.next = (timer *)last->entry.next;
|
|
|
|
(timer *)last->entry.next = event;
|
2004-03-14 21:02:22 +03:00
|
|
|
} else {
|
2002-07-21 02:52:04 +04:00
|
|
|
(timer *)event->entry.next = next;
|
2002-07-09 16:24:59 +04:00
|
|
|
*list = event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
|
|
|
|
int32
|
|
|
|
timer_interrupt()
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-21 02:52:04 +04:00
|
|
|
timer *event;
|
2002-10-26 20:13:36 +04:00
|
|
|
spinlock *spinlock;
|
2004-03-14 21:02:22 +03:00
|
|
|
int currentCPU = smp_get_current_cpu();
|
|
|
|
int32 rc = B_HANDLED_INTERRUPT;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
TRACE(("timer_interrupt: time 0x%x 0x%x, cpu %d\n", system_time(), smp_get_current_cpu()));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
spinlock = &sTimerSpinlock[currentCPU];
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
acquire_spinlock(spinlock);
|
|
|
|
|
|
|
|
restart_scan:
|
2004-03-14 21:02:22 +03:00
|
|
|
event = sEvents[currentCPU];
|
|
|
|
if (event != NULL && ((bigtime_t)event->entry.key < system_time())) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// this event needs to happen
|
2002-07-21 02:52:04 +04:00
|
|
|
int mode = event->flags;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
sEvents[currentCPU] = (timer *)event->entry.next;
|
2002-07-21 02:52:04 +04:00
|
|
|
event->entry.key = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
release_spinlock(spinlock);
|
|
|
|
|
|
|
|
// call the callback
|
|
|
|
// note: if the event is not periodic, it is ok
|
|
|
|
// to delete the event structure inside the callback
|
2002-07-21 02:52:04 +04:00
|
|
|
if (event->hook) {
|
|
|
|
rc = event->hook(event);
|
2002-07-19 20:07:36 +04:00
|
|
|
// if (event->func(event->data) == INT_RESCHEDULE)
|
|
|
|
// rc = INT_RESCHEDULE;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
acquire_spinlock(spinlock);
|
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
if (mode == B_PERIODIC_TIMER) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// we need to adjust it and add it back to the list
|
2004-03-14 21:02:22 +03:00
|
|
|
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 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
goto restart_scan; // the list may have changed
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the next hardware timer
|
2004-03-14 21:02:22 +03:00
|
|
|
if (sEvents[currentCPU] != NULL)
|
|
|
|
arch_timer_set_hardware_timer((bigtime_t)sEvents[currentCPU]->entry.key - system_time());
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
release_spinlock(spinlock);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
|
|
|
|
status_t
|
|
|
|
add_timer(timer *event, timer_hook hook, bigtime_t period, int32 flags)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-03-14 21:02:22 +03:00
|
|
|
bigtime_t scheduleTime;
|
|
|
|
bigtime_t currentTime = system_time();
|
|
|
|
cpu_status state;
|
|
|
|
int currentCPU;
|
2002-07-21 02:52:04 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
if (event == NULL || hook == NULL || period < 0)
|
2002-07-21 02:52:04 +04:00
|
|
|
return B_BAD_VALUE;
|
2004-03-14 21:02:22 +03:00
|
|
|
|
|
|
|
scheduleTime = period;
|
2002-07-21 02:52:04 +04:00
|
|
|
if (flags != B_ONE_SHOT_ABSOLUTE_TIMER)
|
2004-03-14 21:02:22 +03:00
|
|
|
scheduleTime += currentTime;
|
|
|
|
if (scheduleTime == 0)
|
|
|
|
scheduleTime = 1;
|
|
|
|
|
|
|
|
event->entry.key = (int64)scheduleTime;
|
|
|
|
event->period = period;
|
|
|
|
event->hook = hook;
|
|
|
|
event->flags = flags;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2004-03-14 21:02:22 +03:00
|
|
|
currentCPU = smp_get_current_cpu();
|
|
|
|
acquire_spinlock(&sTimerSpinlock[currentCPU]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
add_event_to_list(event, &sEvents[currentCPU]);
|
|
|
|
event->cpu = currentCPU;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// if we were stuck at the head of the list, set the hardware timer
|
2004-03-14 21:02:22 +03:00
|
|
|
if (event == sEvents[currentCPU])
|
|
|
|
arch_timer_set_hardware_timer(scheduleTime - currentTime);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
release_spinlock(&sTimerSpinlock[currentCPU]);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-21 02:52:04 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2004-03-14 21:02:22 +03: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 16:24:59 +04:00
|
|
|
{
|
2002-07-21 02:52:04 +04:00
|
|
|
timer *last = NULL;
|
2004-03-14 21:02:22 +03:00
|
|
|
timer *current;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
acquire_spinlock(&sTimerSpinlock[cpu]);
|
|
|
|
current = sEvents[cpu];
|
|
|
|
while (current != NULL) {
|
|
|
|
if (current == event) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// we found it
|
2004-03-14 21:02:22 +03:00
|
|
|
if (current == sEvents[cpu])
|
|
|
|
sEvents[cpu] = (timer *)current->entry.next;
|
2002-07-21 02:52:04 +04:00
|
|
|
else
|
2004-03-14 21:02:22 +03:00
|
|
|
(timer *)last->entry.next = (timer *)current->entry.next;
|
|
|
|
current->entry.next = NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
// break out of the whole thing
|
2002-07-21 02:52:04 +04:00
|
|
|
break;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-03-14 21:02:22 +03:00
|
|
|
last = current;
|
|
|
|
current = (timer *)current->entry.next;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
if (sEvents[cpu] == NULL)
|
2002-07-09 16:24:59 +04:00
|
|
|
arch_timer_clear_hardware_timer();
|
2002-07-21 02:52:04 +04:00
|
|
|
else
|
2004-03-14 21:02:22 +03:00
|
|
|
arch_timer_set_hardware_timer((bigtime_t)sEvents[cpu]->entry.key - system_time());
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
release_spinlock(&sTimerSpinlock[cpu]);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
return current == event ? B_OK : B_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
bool
|
|
|
|
cancel_timer(timer *event)
|
|
|
|
{
|
|
|
|
int currentCPU = smp_get_current_cpu();
|
|
|
|
cpu_status state;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-07-09 16:24:59 +04: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.
|
2004-03-14 21:02:22 +03:00
|
|
|
|
|
|
|
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 16:24:59 +04:00
|
|
|
// we found it
|
2004-03-14 21:02:22 +03:00
|
|
|
if (current == sEvents[cpu])
|
|
|
|
sEvents[cpu] = (timer *)current->entry.next;
|
2002-07-21 02:52:04 +04:00
|
|
|
else
|
2004-03-14 21:02:22 +03:00
|
|
|
(timer *)last->entry.next = (timer *)current->entry.next;
|
|
|
|
current->entry.next = NULL;
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// break out of the whole thing
|
2004-03-14 21:02:22 +03:00
|
|
|
|
|
|
|
release_spinlock(&sTimerSpinlock[cpu]);
|
|
|
|
restore_interrupts(state);
|
|
|
|
return (bigtime_t)event->entry.key < system_time();
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-03-14 21:02:22 +03:00
|
|
|
last = current;
|
|
|
|
current = (timer *)current->entry.next;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-03-14 21:02:22 +03:00
|
|
|
release_spinlock(&sTimerSpinlock[cpu]);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-21 02:52:04 +04:00
|
|
|
return false;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2004-03-14 21:02:22 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
spin(bigtime_t microseconds)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
bigtime_t time = system_time();
|
|
|
|
|
|
|
|
while((system_time() - time) < microseconds)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|