beos compatible timer routines and style cleanups in timer and sem code
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@353 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
4918394f73
commit
01fb96c4c3
@ -36,7 +36,6 @@ struct sem_entry {
|
||||
static struct sem_entry *sems = NULL;
|
||||
static region_id sem_region = 0;
|
||||
static bool sems_active = false;
|
||||
|
||||
static sem_id next_sem = 0;
|
||||
|
||||
static int sem_spinlock = 0;
|
||||
@ -61,10 +60,9 @@ static int dump_sem_list(int argc, char **argv)
|
||||
int i;
|
||||
|
||||
for (i=0; i<MAX_SEMS; i++) {
|
||||
if(sems[i].id >= 0) {
|
||||
if (sems[i].id >= 0)
|
||||
dprintf("%p\tid: 0x%x\t\tname: '%s'\n", &sems[i], sems[i].id, sems[i].name);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -94,7 +92,8 @@ static int dump_sem_info(int argc, char **argv)
|
||||
// XXX semi-hack
|
||||
_dump_sem_info((struct sem_entry *)num);
|
||||
return 0;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
unsigned slot = num % MAX_SEMS;
|
||||
if (sems[slot].id != (int)num) {
|
||||
dprintf("sem 0x%lx doesn't exist!\n", num);
|
||||
@ -161,7 +160,8 @@ sem_id create_sem_etc(int count, const char *name, proc_id owner)
|
||||
return ENOMEM;
|
||||
strncpy(temp_name, name, SYS_MAX_OS_NAME_LEN-1);
|
||||
temp_name[SYS_MAX_OS_NAME_LEN-1] = 0;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
temp_name = (char *)kmalloc(sizeof("default_sem_name")+1);
|
||||
if (temp_name == NULL)
|
||||
return ENOMEM;
|
||||
@ -175,11 +175,10 @@ sem_id create_sem_etc(int count, const char *name, proc_id owner)
|
||||
for (i=0; i<MAX_SEMS; i++) {
|
||||
if (sems[i].id == -1) {
|
||||
// make the sem id be a multiple of the slot it's in
|
||||
if(i >= next_sem % MAX_SEMS) {
|
||||
if (i >= next_sem % MAX_SEMS)
|
||||
next_sem += i - next_sem % MAX_SEMS;
|
||||
} else {
|
||||
else
|
||||
next_sem += MAX_SEMS - (next_sem % MAX_SEMS - i);
|
||||
}
|
||||
sems[i].id = next_sem++;
|
||||
|
||||
sems[i].lock = 0;
|
||||
@ -198,7 +197,6 @@ sem_id create_sem_etc(int count, const char *name, proc_id owner)
|
||||
}
|
||||
}
|
||||
|
||||
//err:
|
||||
RELEASE_SEM_LIST_LOCK();
|
||||
kfree(temp_name);
|
||||
|
||||
@ -281,9 +279,9 @@ int delete_sem_etc(sem_id id, int return_code)
|
||||
}
|
||||
|
||||
// Called from a timer handler. Wakes up a semaphore
|
||||
static int sem_timeout(void *data)
|
||||
static int sem_timeout(timer *data)
|
||||
{
|
||||
struct sem_timeout_args *args = (struct sem_timeout_args *)data;
|
||||
struct sem_timeout_args *args = (struct sem_timeout_args *)data->entry.prev;
|
||||
struct thread *t;
|
||||
int slot;
|
||||
int state;
|
||||
@ -336,12 +334,10 @@ int acquire_sem_etc(sem_id id, int count, int flags, bigtime_t timeout)
|
||||
|
||||
if (sems_active == false)
|
||||
return B_NO_MORE_SEMS;
|
||||
|
||||
if (id < 0) {
|
||||
dprintf("acquire_sem_etc: invalid sem handle %d\n", id);
|
||||
return B_BAD_SEM_ID;
|
||||
}
|
||||
|
||||
if (count <= 0)
|
||||
return EINVAL;
|
||||
|
||||
@ -363,7 +359,7 @@ int acquire_sem_etc(sem_id id, int count, int flags, bigtime_t timeout)
|
||||
if ((sems[slot].count -= count) < 0) {
|
||||
// we need to block
|
||||
struct thread *t = thread_get_current_thread();
|
||||
struct timer_event timer; // stick it on the stack, since we may be blocking here
|
||||
timer timeout_timer; // stick it on the stack, since we may be blocking here
|
||||
struct sem_timeout_args args;
|
||||
|
||||
// do a quick check to see if the thread has any pending kill signals
|
||||
@ -384,19 +380,18 @@ int acquire_sem_etc(sem_id id, int count, int flags, bigtime_t timeout)
|
||||
thread_enqueue(t, &sems[slot].q);
|
||||
|
||||
if ((flags & (B_TIMEOUT | B_ABSOLUTE_TIMEOUT)) != 0) {
|
||||
int the_timeout = timeout;
|
||||
// dprintf("sem_acquire_etc: setting timeout sem for %d %d usecs, semid %d, tid %d\n",
|
||||
// timeout, sem_id, t->id);
|
||||
// set up an event to go off with the thread struct as the data
|
||||
if (flags & B_ABSOLUTE_TIMEOUT)
|
||||
the_timeout -= system_time();
|
||||
|
||||
args.blocked_sem_id = id;
|
||||
args.blocked_thread = t->id;
|
||||
args.sem_count = count;
|
||||
|
||||
timer_setup_timer(&sem_timeout, &args, &timer);
|
||||
timer_set_event(the_timeout, TIMER_MODE_ONESHOT, &timer);
|
||||
// another evil hack: pass the args into timer->entry.prev
|
||||
timeout_timer.entry.prev = (qent *)&args;
|
||||
add_timer(&timeout_timer, &sem_timeout, timeout,
|
||||
flags & B_RELATIVE_TIMEOUT ?
|
||||
B_ONE_SHOT_RELATIVE_TIMER : B_ONE_SHOT_ABSOLUTE_TIMER);
|
||||
}
|
||||
|
||||
RELEASE_SEM_LOCK(sems[slot]);
|
||||
@ -423,11 +418,11 @@ int acquire_sem_etc(sem_id id, int count, int flags, bigtime_t timeout)
|
||||
thread_resched();
|
||||
RELEASE_THREAD_LOCK();
|
||||
|
||||
if((flags & B_TIMEOUT) != 0) {
|
||||
if ((flags & (B_TIMEOUT | B_ABSOLUTE_TIMEOUT)) != 0) {
|
||||
if (t->sem_errcode != B_TIMED_OUT) {
|
||||
// cancel the timer event, the sem may have been deleted or interrupted
|
||||
// with the timer still active
|
||||
timer_cancel_event(&timer);
|
||||
cancel_timer(&timeout_timer);
|
||||
}
|
||||
}
|
||||
|
||||
@ -459,10 +454,8 @@ int release_sem_etc(sem_id id, int count, int flags)
|
||||
|
||||
if (sems_active == false)
|
||||
return B_NO_MORE_SEMS;
|
||||
|
||||
if (id < 0)
|
||||
return B_BAD_SEM_ID;
|
||||
|
||||
if (count <= 0)
|
||||
return EINVAL;
|
||||
|
||||
@ -530,7 +523,6 @@ int get_sem_count(sem_id id, int32* thread_count)
|
||||
{
|
||||
int slot;
|
||||
int state;
|
||||
// int count;
|
||||
|
||||
if (sems_active == false)
|
||||
return B_NO_MORE_SEMS;
|
||||
@ -602,7 +594,6 @@ int _get_next_sem_info(proc_id proc, uint32 *cookie, struct sem_info *info, size
|
||||
|
||||
if (sems_active == false)
|
||||
return B_NO_MORE_SEMS;
|
||||
|
||||
if (cookie == NULL)
|
||||
return EINVAL;
|
||||
/* prevents sems[].owner == -1 >= means owned by a port */
|
||||
@ -612,7 +603,8 @@ int _get_next_sem_info(proc_id proc, uint32 *cookie, struct sem_info *info, size
|
||||
if (*cookie == NULL) {
|
||||
// return first found
|
||||
slot = 0;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
// start at index cookie, but check cookie against MAX_PORTS
|
||||
slot = *cookie;
|
||||
if (slot >= MAX_SEMS)
|
||||
@ -658,7 +650,7 @@ int set_sem_owner(sem_id id, proc_id proc)
|
||||
return B_NO_MORE_SEMS;
|
||||
if (id < 0)
|
||||
return B_BAD_SEM_ID;
|
||||
if (proc < NULL)
|
||||
if (proc < 0)
|
||||
return EINVAL;
|
||||
|
||||
// XXX: todo check if proc exists
|
||||
@ -801,7 +793,8 @@ sem_id user_create_sem(int count, const char *uname)
|
||||
name[SYS_MAX_OS_NAME_LEN-1] = 0;
|
||||
|
||||
return create_sem_etc(count, name, proc_get_current_proc_id());
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return create_sem_etc(count, NULL, proc_get_current_proc_id());
|
||||
}
|
||||
}
|
||||
|
@ -1475,7 +1475,7 @@ static int _rand(void)
|
||||
return((next >> 16) & 0x7FFF);
|
||||
}
|
||||
|
||||
static int reschedule_event(void *unused)
|
||||
static int reschedule_event(timer *unused)
|
||||
{
|
||||
// this function is called as a result of the timer event set by the scheduler
|
||||
// returning this causes a reschedule on the timer event
|
||||
@ -1491,7 +1491,7 @@ void thread_resched(void)
|
||||
struct thread *old_thread = thread_get_current_thread();
|
||||
int i;
|
||||
bigtime_t quantum;
|
||||
struct timer_event *quantum_timer;
|
||||
timer *quantum_timer;
|
||||
|
||||
// dprintf("top of thread_resched: cpu %d, cur_thread = 0x%x\n", smp_get_current_cpu(), thread_get_current_thread());
|
||||
|
||||
@ -1561,8 +1561,7 @@ found_thread:
|
||||
_local_timer_cancel_event(old_thread->cpu->info.cpu_num, quantum_timer);
|
||||
}
|
||||
old_thread->cpu->info.preempted = 0;
|
||||
timer_setup_timer(&reschedule_event, NULL, quantum_timer);
|
||||
timer_set_event(quantum, TIMER_MODE_ONESHOT, quantum_timer);
|
||||
add_timer(quantum_timer, &reschedule_event, quantum, B_ONE_SHOT_RELATIVE_TIMER);
|
||||
|
||||
if(next_thread != old_thread) {
|
||||
// dprintf("thread_resched: cpu %d switching from thread %d to %d\n",
|
||||
|
@ -14,12 +14,13 @@
|
||||
#include <timer.h>
|
||||
#include <Errors.h>
|
||||
#include <stage2.h>
|
||||
#include <OS.h>
|
||||
|
||||
#include <arch/cpu.h>
|
||||
#include <arch/timer.h>
|
||||
#include <arch/smp.h>
|
||||
|
||||
static struct timer_event * volatile events[SMP_MAX_CPUS] = { NULL, };
|
||||
static timer * volatile events[SMP_MAX_CPUS] = { NULL, };
|
||||
static spinlock_t timer_spinlock[SMP_MAX_CPUS] = { 0, };
|
||||
|
||||
int timer_init(kernel_args *ka)
|
||||
@ -30,31 +31,31 @@ int timer_init(kernel_args *ka)
|
||||
}
|
||||
|
||||
// NOTE: expects interrupts to be off
|
||||
static void add_event_to_list(struct timer_event *event, struct timer_event * volatile *list)
|
||||
static void add_event_to_list(timer *event, timer * volatile *list)
|
||||
{
|
||||
struct timer_event *next;
|
||||
struct timer_event *last = NULL;
|
||||
timer *next;
|
||||
timer *last = NULL;
|
||||
|
||||
// stick it in the event list
|
||||
next = *list;
|
||||
while(next != NULL && next->sched_time < event->sched_time) {
|
||||
last = next;
|
||||
next = next->next;
|
||||
for (next = *list; next; last = next, next = (timer *)next->entry.next) {
|
||||
if ((bigtime_t)next->entry.key >= (bigtime_t)event->entry.key)
|
||||
break;
|
||||
}
|
||||
|
||||
if (last != NULL) {
|
||||
event->next = last->next;
|
||||
last->next = event;
|
||||
} else {
|
||||
event->next = next;
|
||||
(timer *)event->entry.next = (timer *)last->entry.next;
|
||||
(timer *)last->entry.next = event;
|
||||
}
|
||||
else {
|
||||
(timer *)event->entry.next = next;
|
||||
*list = event;
|
||||
}
|
||||
}
|
||||
|
||||
int timer_interrupt()
|
||||
{
|
||||
bigtime_t curr_time = system_time();
|
||||
struct timer_event *event;
|
||||
bigtime_t sched_time;
|
||||
timer *event;
|
||||
spinlock_t *spinlock;
|
||||
int curr_cpu = smp_get_current_cpu();
|
||||
int rc = B_HANDLED_INTERRUPT;
|
||||
@ -67,33 +68,34 @@ int timer_interrupt()
|
||||
|
||||
restart_scan:
|
||||
event = events[curr_cpu];
|
||||
if(event != NULL && event->sched_time < curr_time) {
|
||||
if ((event) && ((bigtime_t)event->entry.key < system_time())) {
|
||||
// this event needs to happen
|
||||
int mode = event->mode;
|
||||
int mode = event->flags;
|
||||
|
||||
events[curr_cpu] = event->next;
|
||||
event->sched_time = 0;
|
||||
events[curr_cpu] = (timer *)event->entry.next;
|
||||
event->entry.key = 0;
|
||||
|
||||
release_spinlock(spinlock);
|
||||
|
||||
// call the callback
|
||||
// note: if the event is not periodic, it is ok
|
||||
// to delete the event structure inside the callback
|
||||
if(event->func != NULL) {
|
||||
rc = event->func(event->data);
|
||||
if (event->hook) {
|
||||
rc = event->hook(event);
|
||||
// if (event->func(event->data) == INT_RESCHEDULE)
|
||||
// rc = INT_RESCHEDULE;
|
||||
}
|
||||
|
||||
acquire_spinlock(spinlock);
|
||||
|
||||
if(mode == TIMER_MODE_PERIODIC) {
|
||||
if (mode == B_PERIODIC_TIMER) {
|
||||
// we need to adjust it and add it back to the list
|
||||
event->sched_time = system_time() + event->periodic_time;
|
||||
if(event->sched_time == 0)
|
||||
event->sched_time = 1; // if we wrapped around and happen
|
||||
sched_time = system_time() + event->period;
|
||||
if (sched_time == 0)
|
||||
sched_time = 1; // if we wrapped around and happen
|
||||
// to hit zero, set it to one, since
|
||||
// zero represents not scheduled
|
||||
event->entry.key = (int64)sched_time;
|
||||
add_event_to_list(event, &events[curr_cpu]);
|
||||
}
|
||||
|
||||
@ -102,121 +104,102 @@ restart_scan:
|
||||
|
||||
// setup the next hardware timer
|
||||
if (events[curr_cpu] != NULL)
|
||||
arch_timer_set_hardware_timer(events[curr_cpu]->sched_time - system_time());
|
||||
arch_timer_set_hardware_timer((bigtime_t)events[curr_cpu]->entry.key - system_time());
|
||||
|
||||
release_spinlock(spinlock);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void timer_setup_timer(timer_callback func, void *data, struct timer_event *event)
|
||||
{
|
||||
event->func = func;
|
||||
event->data = data;
|
||||
event->sched_time = 0;
|
||||
}
|
||||
|
||||
int timer_set_event(bigtime_t relative_time, timer_mode mode, struct timer_event *event)
|
||||
status_t add_timer(timer *t, timer_hook hook, bigtime_t period, int32 flags)
|
||||
{
|
||||
bigtime_t sched_time;
|
||||
bigtime_t curr_time = system_time();
|
||||
int state;
|
||||
int curr_cpu;
|
||||
|
||||
if(event == NULL)
|
||||
return EINVAL;
|
||||
if ((!t) || (!hook) || (period < 0))
|
||||
return B_BAD_VALUE;
|
||||
|
||||
if(relative_time < 0)
|
||||
relative_time = 0;
|
||||
sched_time = period;
|
||||
if (flags != B_ONE_SHOT_ABSOLUTE_TIMER)
|
||||
sched_time += curr_time;
|
||||
if (sched_time == 0)
|
||||
sched_time = 1;
|
||||
|
||||
if(event->sched_time != 0)
|
||||
panic("timer_set_event: event %p in list already!\n", event);
|
||||
|
||||
event->sched_time = system_time() + relative_time;
|
||||
if(event->sched_time == 0)
|
||||
event->sched_time = 1; // if we wrapped around and happen
|
||||
// to hit zero, set it to one, since
|
||||
// zero represents not scheduled
|
||||
event->mode = mode;
|
||||
if(event->mode == TIMER_MODE_PERIODIC)
|
||||
event->periodic_time = relative_time;
|
||||
t->entry.key = (int64)sched_time;
|
||||
t->period = period;
|
||||
t->hook = hook;
|
||||
t->flags = flags;
|
||||
|
||||
state = int_disable_interrupts();
|
||||
|
||||
curr_cpu = smp_get_current_cpu();
|
||||
|
||||
acquire_spinlock(&timer_spinlock[curr_cpu]);
|
||||
|
||||
add_event_to_list(event, &events[curr_cpu]);
|
||||
add_event_to_list(t, &events[curr_cpu]);
|
||||
t->cpu = curr_cpu;
|
||||
|
||||
// if we were stuck at the head of the list, set the hardware timer
|
||||
if(event == events[curr_cpu]) {
|
||||
arch_timer_set_hardware_timer(relative_time);
|
||||
}
|
||||
if (t == events[curr_cpu])
|
||||
arch_timer_set_hardware_timer(sched_time - curr_time);
|
||||
|
||||
release_spinlock(&timer_spinlock[curr_cpu]);
|
||||
int_restore_interrupts(state);
|
||||
|
||||
return 0;
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
/* this is a fast path to be called from reschedule and from timer_cancel_event */
|
||||
/* must always be invoked with interrupts disabled */
|
||||
int _local_timer_cancel_event(int curr_cpu, struct timer_event *event)
|
||||
int _local_timer_cancel_event(int curr_cpu, timer *event)
|
||||
{
|
||||
struct timer_event *last = NULL;
|
||||
struct timer_event *e;
|
||||
bool foundit = false;
|
||||
timer *last = NULL;
|
||||
timer *e;
|
||||
|
||||
acquire_spinlock(&timer_spinlock[curr_cpu]);
|
||||
e = events[curr_cpu];
|
||||
while (e != NULL) {
|
||||
if (e == event) {
|
||||
// we found it
|
||||
foundit = true;
|
||||
if(e == events[curr_cpu]) {
|
||||
events[curr_cpu] = e->next;
|
||||
} else {
|
||||
last->next = e->next;
|
||||
}
|
||||
e->next = NULL;
|
||||
if (e == events[curr_cpu])
|
||||
events[curr_cpu] = (timer *)e->entry.next;
|
||||
else
|
||||
(timer *)last->entry.next = (timer *)e->entry.next;
|
||||
e->entry.next = NULL;
|
||||
// break out of the whole thing
|
||||
goto done;
|
||||
break;
|
||||
}
|
||||
last = e;
|
||||
e = e->next;
|
||||
e = (timer *)e->entry.next;
|
||||
}
|
||||
release_spinlock(&timer_spinlock[curr_cpu]);
|
||||
done:
|
||||
|
||||
if(events[curr_cpu] == NULL) {
|
||||
if (events[curr_cpu] == NULL)
|
||||
arch_timer_clear_hardware_timer();
|
||||
} else {
|
||||
arch_timer_set_hardware_timer(events[curr_cpu]->sched_time - system_time());
|
||||
}
|
||||
else
|
||||
arch_timer_set_hardware_timer((bigtime_t)events[curr_cpu]->entry.key - system_time());
|
||||
|
||||
if(foundit) {
|
||||
release_spinlock(&timer_spinlock[curr_cpu]);
|
||||
|
||||
return (e == event ? 0 : B_ERROR);
|
||||
}
|
||||
|
||||
return (foundit ? 0 : B_ERROR);
|
||||
}
|
||||
|
||||
int local_timer_cancel_event(struct timer_event *event)
|
||||
int local_timer_cancel_event(timer *event)
|
||||
{
|
||||
return _local_timer_cancel_event(smp_get_current_cpu(), event);
|
||||
}
|
||||
|
||||
int timer_cancel_event(struct timer_event *event)
|
||||
bool cancel_timer(timer *event)
|
||||
{
|
||||
int state;
|
||||
struct timer_event *last = NULL;
|
||||
struct timer_event *e;
|
||||
timer *last = NULL;
|
||||
timer *e;
|
||||
bool foundit = false;
|
||||
int num_cpus = smp_get_num_cpus();
|
||||
int cpu= 0;
|
||||
int curr_cpu;
|
||||
|
||||
if(event->sched_time == 0)
|
||||
return 0; // it's not scheduled
|
||||
// if (event->sched_time == 0)
|
||||
// return 0; // it's not scheduled
|
||||
|
||||
state = int_disable_interrupts();
|
||||
curr_cpu = smp_get_current_cpu();
|
||||
@ -236,29 +219,29 @@ int timer_cancel_event(struct timer_event *event)
|
||||
if (e == event) {
|
||||
// we found it
|
||||
foundit = true;
|
||||
if(e == events[cpu]) {
|
||||
events[cpu] = e->next;
|
||||
} else {
|
||||
last->next = e->next;
|
||||
}
|
||||
e->next = NULL;
|
||||
if(e == events[cpu])
|
||||
events[cpu] = (timer *)e->entry.next;
|
||||
else
|
||||
(timer *)last->entry.next = (timer *)e->entry.next;
|
||||
e->entry.next = NULL;
|
||||
// break out of the whole thing
|
||||
goto done;
|
||||
}
|
||||
last = e;
|
||||
e = e->next;
|
||||
e = (timer *)e->entry.next;
|
||||
}
|
||||
release_spinlock(&timer_spinlock[cpu]);
|
||||
}
|
||||
}
|
||||
done:
|
||||
|
||||
if(foundit) {
|
||||
if (foundit)
|
||||
release_spinlock(&timer_spinlock[cpu]);
|
||||
}
|
||||
int_restore_interrupts(state);
|
||||
|
||||
return (foundit ? 0 : B_ERROR);
|
||||
if (foundit && ((bigtime_t)event->entry.key < system_time()))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void spin(bigtime_t microseconds)
|
||||
|
Loading…
Reference in New Issue
Block a user