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:
lillo
2002-07-20 22:52:04 +00:00
parent 4918394f73
commit 01fb96c4c3
3 changed files with 199 additions and 224 deletions
+22 -29
View File
@@ -36,7 +36,6 @@ struct sem_entry {
static struct sem_entry *sems = NULL; static struct sem_entry *sems = NULL;
static region_id sem_region = 0; static region_id sem_region = 0;
static bool sems_active = false; static bool sems_active = false;
static sem_id next_sem = 0; static sem_id next_sem = 0;
static int sem_spinlock = 0; static int sem_spinlock = 0;
@@ -61,10 +60,9 @@ static int dump_sem_list(int argc, char **argv)
int i; int i;
for (i=0; i<MAX_SEMS; 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); dprintf("%p\tid: 0x%x\t\tname: '%s'\n", &sems[i], sems[i].id, sems[i].name);
} }
}
return 0; return 0;
} }
@@ -94,7 +92,8 @@ static int dump_sem_info(int argc, char **argv)
// XXX semi-hack // XXX semi-hack
_dump_sem_info((struct sem_entry *)num); _dump_sem_info((struct sem_entry *)num);
return 0; return 0;
} else { }
else {
unsigned slot = num % MAX_SEMS; unsigned slot = num % MAX_SEMS;
if (sems[slot].id != (int)num) { if (sems[slot].id != (int)num) {
dprintf("sem 0x%lx doesn't exist!\n", 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; return ENOMEM;
strncpy(temp_name, name, SYS_MAX_OS_NAME_LEN-1); strncpy(temp_name, name, SYS_MAX_OS_NAME_LEN-1);
temp_name[SYS_MAX_OS_NAME_LEN-1] = 0; temp_name[SYS_MAX_OS_NAME_LEN-1] = 0;
} else { }
else {
temp_name = (char *)kmalloc(sizeof("default_sem_name")+1); temp_name = (char *)kmalloc(sizeof("default_sem_name")+1);
if (temp_name == NULL) if (temp_name == NULL)
return ENOMEM; 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++) { for (i=0; i<MAX_SEMS; i++) {
if (sems[i].id == -1) { if (sems[i].id == -1) {
// make the sem id be a multiple of the slot it's in // 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; next_sem += i - next_sem % MAX_SEMS;
} else { else
next_sem += MAX_SEMS - (next_sem % MAX_SEMS - i); next_sem += MAX_SEMS - (next_sem % MAX_SEMS - i);
}
sems[i].id = next_sem++; sems[i].id = next_sem++;
sems[i].lock = 0; 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(); RELEASE_SEM_LIST_LOCK();
kfree(temp_name); 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 // 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; struct thread *t;
int slot; int slot;
int state; int state;
@@ -336,12 +334,10 @@ int acquire_sem_etc(sem_id id, int count, int flags, bigtime_t timeout)
if (sems_active == false) if (sems_active == false)
return B_NO_MORE_SEMS; return B_NO_MORE_SEMS;
if (id < 0) { if (id < 0) {
dprintf("acquire_sem_etc: invalid sem handle %d\n", id); dprintf("acquire_sem_etc: invalid sem handle %d\n", id);
return B_BAD_SEM_ID; return B_BAD_SEM_ID;
} }
if (count <= 0) if (count <= 0)
return EINVAL; 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) { if ((sems[slot].count -= count) < 0) {
// we need to block // we need to block
struct thread *t = thread_get_current_thread(); 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; struct sem_timeout_args args;
// do a quick check to see if the thread has any pending kill signals // 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); thread_enqueue(t, &sems[slot].q);
if ((flags & (B_TIMEOUT | B_ABSOLUTE_TIMEOUT)) != 0) { 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", // dprintf("sem_acquire_etc: setting timeout sem for %d %d usecs, semid %d, tid %d\n",
// timeout, sem_id, t->id); // timeout, sem_id, t->id);
// set up an event to go off with the thread struct as the data // 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_sem_id = id;
args.blocked_thread = t->id; args.blocked_thread = t->id;
args.sem_count = count; args.sem_count = count;
timer_setup_timer(&sem_timeout, &args, &timer); // another evil hack: pass the args into timer->entry.prev
timer_set_event(the_timeout, TIMER_MODE_ONESHOT, &timer); 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]); 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(); thread_resched();
RELEASE_THREAD_LOCK(); RELEASE_THREAD_LOCK();
if((flags & B_TIMEOUT) != 0) { if ((flags & (B_TIMEOUT | B_ABSOLUTE_TIMEOUT)) != 0) {
if (t->sem_errcode != B_TIMED_OUT) { if (t->sem_errcode != B_TIMED_OUT) {
// cancel the timer event, the sem may have been deleted or interrupted // cancel the timer event, the sem may have been deleted or interrupted
// with the timer still active // 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) if (sems_active == false)
return B_NO_MORE_SEMS; return B_NO_MORE_SEMS;
if (id < 0) if (id < 0)
return B_BAD_SEM_ID; return B_BAD_SEM_ID;
if (count <= 0) if (count <= 0)
return EINVAL; return EINVAL;
@@ -530,7 +523,6 @@ int get_sem_count(sem_id id, int32* thread_count)
{ {
int slot; int slot;
int state; int state;
// int count;
if (sems_active == false) if (sems_active == false)
return B_NO_MORE_SEMS; 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) if (sems_active == false)
return B_NO_MORE_SEMS; return B_NO_MORE_SEMS;
if (cookie == NULL) if (cookie == NULL)
return EINVAL; return EINVAL;
/* prevents sems[].owner == -1 >= means owned by a port */ /* 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) { if (*cookie == NULL) {
// return first found // return first found
slot = 0; slot = 0;
} else { }
else {
// start at index cookie, but check cookie against MAX_PORTS // start at index cookie, but check cookie against MAX_PORTS
slot = *cookie; slot = *cookie;
if (slot >= MAX_SEMS) if (slot >= MAX_SEMS)
@@ -658,7 +650,7 @@ int set_sem_owner(sem_id id, proc_id proc)
return B_NO_MORE_SEMS; return B_NO_MORE_SEMS;
if (id < 0) if (id < 0)
return B_BAD_SEM_ID; return B_BAD_SEM_ID;
if (proc < NULL) if (proc < 0)
return EINVAL; return EINVAL;
// XXX: todo check if proc exists // 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; name[SYS_MAX_OS_NAME_LEN-1] = 0;
return create_sem_etc(count, name, proc_get_current_proc_id()); return create_sem_etc(count, name, proc_get_current_proc_id());
} else { }
else {
return create_sem_etc(count, NULL, proc_get_current_proc_id()); return create_sem_etc(count, NULL, proc_get_current_proc_id());
} }
} }
+3 -4
View File
@@ -1475,7 +1475,7 @@ static int _rand(void)
return((next >> 16) & 0x7FFF); 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 // this function is called as a result of the timer event set by the scheduler
// returning this causes a reschedule on the timer event // 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(); struct thread *old_thread = thread_get_current_thread();
int i; int i;
bigtime_t quantum; 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()); // 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); _local_timer_cancel_event(old_thread->cpu->info.cpu_num, quantum_timer);
} }
old_thread->cpu->info.preempted = 0; old_thread->cpu->info.preempted = 0;
timer_setup_timer(&reschedule_event, NULL, quantum_timer); add_timer(quantum_timer, &reschedule_event, quantum, B_ONE_SHOT_RELATIVE_TIMER);
timer_set_event(quantum, TIMER_MODE_ONESHOT, quantum_timer);
if(next_thread != old_thread) { if(next_thread != old_thread) {
// dprintf("thread_resched: cpu %d switching from thread %d to %d\n", // dprintf("thread_resched: cpu %d switching from thread %d to %d\n",
+77 -94
View File
@@ -14,12 +14,13 @@
#include <timer.h> #include <timer.h>
#include <Errors.h> #include <Errors.h>
#include <stage2.h> #include <stage2.h>
#include <OS.h>
#include <arch/cpu.h> #include <arch/cpu.h>
#include <arch/timer.h> #include <arch/timer.h>
#include <arch/smp.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, }; static spinlock_t timer_spinlock[SMP_MAX_CPUS] = { 0, };
int timer_init(kernel_args *ka) int timer_init(kernel_args *ka)
@@ -30,31 +31,31 @@ int timer_init(kernel_args *ka)
} }
// NOTE: expects interrupts to be off // 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; timer *next;
struct timer_event *last = NULL; timer *last = NULL;
// stick it in the event list // stick it in the event list
next = *list; for (next = *list; next; last = next, next = (timer *)next->entry.next) {
while(next != NULL && next->sched_time < event->sched_time) { if ((bigtime_t)next->entry.key >= (bigtime_t)event->entry.key)
last = next; break;
next = next->next;
} }
if (last != NULL) { if (last != NULL) {
event->next = last->next; (timer *)event->entry.next = (timer *)last->entry.next;
last->next = event; (timer *)last->entry.next = event;
} else { }
event->next = next; else {
(timer *)event->entry.next = next;
*list = event; *list = event;
} }
} }
int timer_interrupt() int timer_interrupt()
{ {
bigtime_t curr_time = system_time(); bigtime_t sched_time;
struct timer_event *event; timer *event;
spinlock_t *spinlock; spinlock_t *spinlock;
int curr_cpu = smp_get_current_cpu(); int curr_cpu = smp_get_current_cpu();
int rc = B_HANDLED_INTERRUPT; int rc = B_HANDLED_INTERRUPT;
@@ -67,33 +68,34 @@ int timer_interrupt()
restart_scan: restart_scan:
event = events[curr_cpu]; 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 // this event needs to happen
int mode = event->mode; int mode = event->flags;
events[curr_cpu] = event->next; events[curr_cpu] = (timer *)event->entry.next;
event->sched_time = 0; event->entry.key = 0;
release_spinlock(spinlock); release_spinlock(spinlock);
// call the callback // call the callback
// note: if the event is not periodic, it is ok // note: if the event is not periodic, it is ok
// to delete the event structure inside the callback // to delete the event structure inside the callback
if(event->func != NULL) { if (event->hook) {
rc = event->func(event->data); rc = event->hook(event);
// if (event->func(event->data) == INT_RESCHEDULE) // if (event->func(event->data) == INT_RESCHEDULE)
// rc = INT_RESCHEDULE; // rc = INT_RESCHEDULE;
} }
acquire_spinlock(spinlock); 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 // we need to adjust it and add it back to the list
event->sched_time = system_time() + event->periodic_time; sched_time = system_time() + event->period;
if(event->sched_time == 0) if (sched_time == 0)
event->sched_time = 1; // if we wrapped around and happen sched_time = 1; // if we wrapped around and happen
// to hit zero, set it to one, since // to hit zero, set it to one, since
// zero represents not scheduled // zero represents not scheduled
event->entry.key = (int64)sched_time;
add_event_to_list(event, &events[curr_cpu]); add_event_to_list(event, &events[curr_cpu]);
} }
@@ -102,121 +104,102 @@ restart_scan:
// setup the next hardware timer // setup the next hardware timer
if (events[curr_cpu] != NULL) 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); release_spinlock(spinlock);
return rc; return rc;
} }
void timer_setup_timer(timer_callback func, void *data, struct timer_event *event) status_t add_timer(timer *t, timer_hook hook, bigtime_t period, int32 flags)
{
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)
{ {
bigtime_t sched_time;
bigtime_t curr_time = system_time();
int state; int state;
int curr_cpu; int curr_cpu;
if(event == NULL) if ((!t) || (!hook) || (period < 0))
return EINVAL; return B_BAD_VALUE;
if(relative_time < 0) sched_time = period;
relative_time = 0; if (flags != B_ONE_SHOT_ABSOLUTE_TIMER)
sched_time += curr_time;
if (sched_time == 0)
sched_time = 1;
if(event->sched_time != 0) t->entry.key = (int64)sched_time;
panic("timer_set_event: event %p in list already!\n", event); t->period = period;
t->hook = hook;
event->sched_time = system_time() + relative_time; t->flags = flags;
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;
state = int_disable_interrupts(); state = int_disable_interrupts();
curr_cpu = smp_get_current_cpu(); curr_cpu = smp_get_current_cpu();
acquire_spinlock(&timer_spinlock[curr_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 we were stuck at the head of the list, set the hardware timer
if(event == events[curr_cpu]) { if (t == events[curr_cpu])
arch_timer_set_hardware_timer(relative_time); arch_timer_set_hardware_timer(sched_time - curr_time);
}
release_spinlock(&timer_spinlock[curr_cpu]); release_spinlock(&timer_spinlock[curr_cpu]);
int_restore_interrupts(state); int_restore_interrupts(state);
return 0; return B_OK;
} }
/* this is a fast path to be called from reschedule and from timer_cancel_event */ /* this is a fast path to be called from reschedule and from timer_cancel_event */
/* must always be invoked with interrupts disabled */ /* 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; timer *last = NULL;
struct timer_event *e; timer *e;
bool foundit = false;
acquire_spinlock(&timer_spinlock[curr_cpu]); acquire_spinlock(&timer_spinlock[curr_cpu]);
e = events[curr_cpu]; e = events[curr_cpu];
while (e != NULL) { while (e != NULL) {
if (e == event) { if (e == event) {
// we found it // we found it
foundit = true; if (e == events[curr_cpu])
if(e == events[curr_cpu]) { events[curr_cpu] = (timer *)e->entry.next;
events[curr_cpu] = e->next; else
} else { (timer *)last->entry.next = (timer *)e->entry.next;
last->next = e->next; e->entry.next = NULL;
}
e->next = NULL;
// break out of the whole thing // break out of the whole thing
goto done; break;
} }
last = e; 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(); arch_timer_clear_hardware_timer();
} else { else
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());
}
if(foundit) {
release_spinlock(&timer_spinlock[curr_cpu]); release_spinlock(&timer_spinlock[curr_cpu]);
return (e == event ? 0 : B_ERROR);
} }
return (foundit ? 0 : B_ERROR); int local_timer_cancel_event(timer *event)
}
int local_timer_cancel_event(struct timer_event *event)
{ {
return _local_timer_cancel_event(smp_get_current_cpu(), 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; int state;
struct timer_event *last = NULL; timer *last = NULL;
struct timer_event *e; timer *e;
bool foundit = false; bool foundit = false;
int num_cpus = smp_get_num_cpus(); int num_cpus = smp_get_num_cpus();
int cpu= 0; int cpu= 0;
int curr_cpu; int curr_cpu;
if(event->sched_time == 0) // if (event->sched_time == 0)
return 0; // it's not scheduled // return 0; // it's not scheduled
state = int_disable_interrupts(); state = int_disable_interrupts();
curr_cpu = smp_get_current_cpu(); curr_cpu = smp_get_current_cpu();
@@ -236,29 +219,29 @@ int timer_cancel_event(struct timer_event *event)
if (e == event) { if (e == event) {
// we found it // we found it
foundit = true; foundit = true;
if(e == events[cpu]) { if(e == events[cpu])
events[cpu] = e->next; events[cpu] = (timer *)e->entry.next;
} else { else
last->next = e->next; (timer *)last->entry.next = (timer *)e->entry.next;
} e->entry.next = NULL;
e->next = NULL;
// break out of the whole thing // break out of the whole thing
goto done; goto done;
} }
last = e; last = e;
e = e->next; e = (timer *)e->entry.next;
} }
release_spinlock(&timer_spinlock[cpu]); release_spinlock(&timer_spinlock[cpu]);
} }
} }
done: done:
if(foundit) { if (foundit)
release_spinlock(&timer_spinlock[cpu]); release_spinlock(&timer_spinlock[cpu]);
}
int_restore_interrupts(state); 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) void spin(bigtime_t microseconds)