Snippets
Created by
Renato Cunha
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | /*
* Copyright (C) 1990 Don Libes
* Copyright (C) 2015 Renato L. F. Cunha
*
* An implementation of an arbitrary set of timers that use a single "real"
* timer (with the alarm function, in this case).
*
* This implementation most completely based on Don Libes' notes found at
* http://www.kohala.com/start/libes.timers.txt - The only things I changed
* were that I actually used a timer, and actually implemented signal
* blocking/unblocking.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <limits.h>
#include "timers.h"
static sigset_t oldset; /* < backup storage for the old signal mask */
static TIME time_timer_set, time_now; /* < internal timers */
static struct timer *timer_next = NULL; /* < the next timer that might be fired */
static void sig_alarm();
static void timers_update(TIME);
/**
* Disables interrupts by blocking all signals with a call to sigprocmask.
*/
static void disable_interrupts() {
sigset_t set;
sigfillset(&set);
sigprocmask(SIG_BLOCK, &set, &oldset);
}
/**
* Re-enables interrupts by restoring the original signal mask.
*/
static void enable_interrupts() {
sigprocmask(SIG_SETMASK, &oldset, NULL);
}
/**
* The original document suggests the current time can be a volatile variable
* that's updated by some other thread, or by the OS, whatever. We don't have
* it here, so, every time we have to read the current time, we must call this
* function to update our internal clock.
*/
static inline void update_time_now() {
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
time_now = tp.tv_sec;
}
/**
* Starts the "physical" timer by calling alarm(time).
*/
static void start_physical_timer(TIME time) {
if (signal(SIGALRM, sig_alarm) == SIG_ERR) {
fprintf(stderr, "Can't catch SIGALRM\n");
abort();
}
alarm(time);
}
/**
* The original timer interrupt handler. Updates the current time, updates all
* timers and, if there is a timer to expire in the future, re-starts the
* "physical" timer to the closest possible.
*/
static inline void timer_interrupt_handler() {
update_time_now();
timers_update(time_now - time_timer_set);
/* start physical timer for next shortest time if one exists */
if (timer_next) {
time_timer_set = time_now;
start_physical_timer(timer_next->time);
}
}
/**
* Just a handler to SIGALRM.
*/
static void sig_alarm(int signo) {
timer_interrupt_handler();
}
/**
* "Constructor". Initializes the set of timers.
*/
void timers_init() {
struct timer *t;
for (t = timers; t < &timers[MAX_TIMERS]; t++) {
t->inuse = false;
}
}
/**
* Updates all timers.
*/
static void timers_update(TIME time) {
static struct timer timer_last = { /* This is just a placeholder */
false, /* in use */
VERY_LONG_TIME, /* time */
NULL /* event pointer */
};
struct timer *t;
timer_next = &timer_last;
for (t=timers; t < &timers[MAX_TIMERS]; t++) {
if (t->inuse) {
if (time < t->time) { /* haven't expired yet */
t->time -= time;
if (t->time < timer_next->time) {
timer_next = t;
}
} else { /* expired */
/*
* FIXME: Add a callback functionality to invoke
* "scheduler"
*/
*t->event = true;
t->inuse = 0; /* remove timer, since it is done */
}
}
}
if (!timer_next->inuse) {
timer_next = 0;
}
}
/**
* Declares a new timer.
* time -> In this many seconds the timer must go off
* event -> Will be set to true when this timer does expire
*
* Returns a pointer to the newly created timer.
*/
struct timer *timer_declare(unsigned int time, char *event) {
struct timer *t;
disable_interrupts();
for (t=timers; t < &timers[MAX_TIMERS]; t++) {
if (!t->inuse) {
break;
}
}
/* out of timers? */
if (t == &timers[MAX_TIMERS]) {
enable_interrupts();
return NULL;
}
/* install new timer */
t->event = event;
t->time = time;
update_time_now();
if (!timer_next) {
/* no timers, this is the shortest */
time_timer_set = time_now;
start_physical_timer((timer_next = t)->time);
} else if ((time + time_now) < (timer_next->time + time_timer_set)) {
/* new timer is shorter than current one, so update it */
timers_update(time_now - time_timer_set);
time_timer_set = time_now;
start_physical_timer((timer_next = t)->time);
} else {
/* nothing to do */
}
t->inuse = true;
enable_interrupts();
return t;
}
/**
* Deallocates a timer returned by timer_declare.
*/
void timer_undeclare(struct timer *t) {
disable_interrupts();
if (!t->inuse) {
enable_interrupts();
return;
}
t->inuse = false;
/* check if we were waiting on this one */
if (t == timer_next) {
update_time_now();
timers_update(time_now - time_timer_set);
if (timer_next) {
start_physical_timer(timer_next->time);
time_timer_set = time_now;
}
}
enable_interrupts();
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.