mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-11-01 23:18:21 +00:00
timer: repeat support and running status
This commit is contained in:
@@ -4,10 +4,12 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <arch/context.h>
|
#include <arch/context.h>
|
||||||
#include <spinlock.h>
|
#include <spinlock.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct timerentry_t {
|
typedef struct timerentry_t {
|
||||||
struct timerentry_t *next;
|
struct timerentry_t *next;
|
||||||
time_t absolutetick;
|
time_t absolutetick;
|
||||||
|
time_t repeatus;
|
||||||
void *private;
|
void *private;
|
||||||
void (*func)(void *private, context_t* context);
|
void (*func)(void *private, context_t* context);
|
||||||
} timerentry_t;
|
} timerentry_t;
|
||||||
@@ -16,6 +18,7 @@ typedef struct {
|
|||||||
spinlock_t lock;
|
spinlock_t lock;
|
||||||
time_t ticksperus;
|
time_t ticksperus;
|
||||||
time_t tickcurrent;
|
time_t tickcurrent;
|
||||||
|
bool running;
|
||||||
void (*arm)(time_t);
|
void (*arm)(time_t);
|
||||||
time_t (*stop)();
|
time_t (*stop)();
|
||||||
timerentry_t *queue;
|
timerentry_t *queue;
|
||||||
|
|||||||
+68
-49
@@ -3,55 +3,8 @@
|
|||||||
#include <logging.h>
|
#include <logging.h>
|
||||||
#include <kernel/interrupt.h>
|
#include <kernel/interrupt.h>
|
||||||
|
|
||||||
void timer_isr(timer_t *timer, context_t *context) {
|
static void insert(timer_t *timer, timerentry_t *entry, time_t us) {
|
||||||
spinlock_acquire(&timer->lock);
|
entry->absolutetick = timer->tickcurrent + us * timer->ticksperus;
|
||||||
__assert(timer->queue);
|
|
||||||
timer->tickcurrent = timer->queue->absolutetick;
|
|
||||||
timerentry_t *oldentry = timer->queue;
|
|
||||||
timer->queue = timer->queue->next;
|
|
||||||
|
|
||||||
if (timer->queue) {
|
|
||||||
// TODO handle this
|
|
||||||
__assert(timer->queue->absolutetick > timer->tickcurrent);
|
|
||||||
timer->arm(timer->queue->absolutetick - timer->tickcurrent);
|
|
||||||
}
|
|
||||||
|
|
||||||
spinlock_release(&timer->lock);
|
|
||||||
oldentry->func(oldentry->private, context);
|
|
||||||
}
|
|
||||||
|
|
||||||
void timer_resume(timer_t *timer) {
|
|
||||||
spinlock_acquire(&timer->lock);
|
|
||||||
|
|
||||||
if (timer->queue == NULL)
|
|
||||||
goto leave;
|
|
||||||
|
|
||||||
// TODO handle difference being 0
|
|
||||||
__assert(timer->queue->absolutetick > timer->tickcurrent);
|
|
||||||
timer->arm(timer->queue->absolutetick - timer->tickcurrent);
|
|
||||||
|
|
||||||
leave:
|
|
||||||
spinlock_release(&timer->lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
void timer_stop(timer_t *timer) {
|
|
||||||
long oldipl = interrupt_setipl(IPL_TIMER);
|
|
||||||
spinlock_acquire(&timer->lock);
|
|
||||||
|
|
||||||
timer->tickcurrent += timer->stop(timer);
|
|
||||||
|
|
||||||
spinlock_release(&timer->lock);
|
|
||||||
interrupt_setipl(oldipl);
|
|
||||||
}
|
|
||||||
|
|
||||||
void timer_insert(timer_t *timer, timerentry_t* entry, time_t us) {
|
|
||||||
long oldipl = interrupt_setipl(IPL_TIMER);
|
|
||||||
spinlock_acquire(&timer->lock);
|
|
||||||
|
|
||||||
timer->tickcurrent += timer->stop(timer);
|
|
||||||
|
|
||||||
time_t tickdiff = us * timer->ticksperus;
|
|
||||||
entry->absolutetick = timer->tickcurrent + tickdiff;
|
|
||||||
|
|
||||||
if (timer->queue == NULL) { // if no other entries in the queue
|
if (timer->queue == NULL) { // if no other entries in the queue
|
||||||
entry->next = NULL;
|
entry->next = NULL;
|
||||||
@@ -75,6 +28,71 @@ void timer_insert(timer_t *timer, timerentry_t* entry, time_t us) {
|
|||||||
search = search->next;
|
search = search->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void timer_isr(timer_t *timer, context_t *context) {
|
||||||
|
spinlock_acquire(&timer->lock);
|
||||||
|
|
||||||
|
__assert(timer->queue);
|
||||||
|
timer->tickcurrent = timer->queue->absolutetick;
|
||||||
|
timerentry_t *oldentry = timer->queue;
|
||||||
|
timer->queue = timer->queue->next;
|
||||||
|
|
||||||
|
oldentry->func(oldentry->private, context);
|
||||||
|
|
||||||
|
if (oldentry->repeatus)
|
||||||
|
insert(timer, oldentry, oldentry->repeatus);
|
||||||
|
|
||||||
|
if (timer->queue) {
|
||||||
|
// TODO handle this
|
||||||
|
__assert(timer->queue->absolutetick > timer->tickcurrent);
|
||||||
|
timer->arm(timer->queue->absolutetick - timer->tickcurrent);
|
||||||
|
}
|
||||||
|
|
||||||
|
spinlock_release(&timer->lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void timer_resume(timer_t *timer) {
|
||||||
|
spinlock_acquire(&timer->lock);
|
||||||
|
|
||||||
|
timer->running = true;
|
||||||
|
|
||||||
|
if (timer->queue == NULL)
|
||||||
|
goto leave;
|
||||||
|
|
||||||
|
// TODO handle difference being 0
|
||||||
|
__assert(timer->queue->absolutetick > timer->tickcurrent);
|
||||||
|
timer->arm(timer->queue->absolutetick - timer->tickcurrent);
|
||||||
|
|
||||||
|
leave:
|
||||||
|
spinlock_release(&timer->lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void timer_stop(timer_t *timer) {
|
||||||
|
long oldipl = interrupt_setipl(IPL_TIMER);
|
||||||
|
spinlock_acquire(&timer->lock);
|
||||||
|
|
||||||
|
timer->tickcurrent += timer->stop(timer);
|
||||||
|
timer->running = false;
|
||||||
|
|
||||||
|
spinlock_release(&timer->lock);
|
||||||
|
interrupt_setipl(oldipl);
|
||||||
|
}
|
||||||
|
|
||||||
|
void timer_insert(timer_t *timer, timerentry_t* entry, time_t us) {
|
||||||
|
long oldipl = interrupt_setipl(IPL_TIMER);
|
||||||
|
spinlock_acquire(&timer->lock);
|
||||||
|
|
||||||
|
if (timer->running)
|
||||||
|
timer->tickcurrent += timer->stop(timer);
|
||||||
|
|
||||||
|
insert(timer, entry, us);
|
||||||
|
|
||||||
|
if (timer->running) {
|
||||||
|
// TODO handle difference being 0
|
||||||
|
__assert(timer->queue->absolutetick > timer->tickcurrent);
|
||||||
|
timer->arm(timer->queue->absolutetick - timer->tickcurrent);
|
||||||
|
}
|
||||||
|
|
||||||
spinlock_release(&timer->lock);
|
spinlock_release(&timer->lock);
|
||||||
interrupt_setipl(oldipl);
|
interrupt_setipl(oldipl);
|
||||||
@@ -86,6 +104,7 @@ timer_t *timer_new(time_t ticksperus, void (*arm)(time_t), time_t (*stop)()) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
timer->ticksperus = ticksperus;
|
timer->ticksperus = ticksperus;
|
||||||
|
timer->running = false;
|
||||||
timer->arm = arm;
|
timer->arm = arm;
|
||||||
timer->stop = stop;
|
timer->stop = stop;
|
||||||
SPINLOCK_INIT(timer->lock);
|
SPINLOCK_INIT(timer->lock);
|
||||||
|
|||||||
Reference in New Issue
Block a user