os: Use xorg_list for struct _OsTimerRec

No sense having an open-coded linked list here, plus the doubly linked
list is more efficient

Signed-off-by: Keith Packard <keithp@keithp.com>
Reviewed-by: Adam Jackson <ajax@redhat.com>
This commit is contained in:
Keith Packard 2016-05-29 19:48:25 -07:00 committed by Adam Jackson
parent 50779c494d
commit 2ab8b1dcd3

View File

@ -113,16 +113,25 @@ mffs(fd_mask mask)
#endif #endif
struct _OsTimerRec { struct _OsTimerRec {
OsTimerPtr next; struct xorg_list list;
CARD32 expires; CARD32 expires;
CARD32 delta; CARD32 delta;
OsTimerCallback callback; OsTimerCallback callback;
void *arg; void *arg;
}; };
static void DoTimer(OsTimerPtr timer, CARD32 now, volatile OsTimerPtr *prev); static void DoTimer(OsTimerPtr timer, CARD32 now);
static void CheckAllTimers(void); static void CheckAllTimers(void);
static volatile OsTimerPtr timers = NULL; static volatile struct xorg_list timers;
static inline OsTimerPtr
first_timer(void)
{
/* inline xorg_list_is_empty which can't handle volatile */
if (timers.next == &timers)
return NULL;
return xorg_list_first_entry(&timers, struct _OsTimerRec, list);
}
/***************** /*****************
* WaitForSomething: * WaitForSomething:
@ -150,6 +159,7 @@ WaitForSomething(Bool are_ready)
static Bool were_ready; static Bool were_ready;
Bool timer_is_running; Bool timer_is_running;
CARD32 now = 0; CARD32 now = 0;
OsTimerPtr timer;
timer_is_running = were_ready; timer_is_running = were_ready;
@ -176,16 +186,17 @@ WaitForSomething(Bool are_ready)
} }
else { else {
timeout = -1; timeout = -1;
if (timers) { if ((timer = first_timer()) != NULL) {
now = GetTimeInMillis(); now = GetTimeInMillis();
timeout = timers->expires - now; timeout = timer->expires - now;
if (timeout > 0 && timeout > timers->delta + 250) { if (timeout > 0 && timeout > timer->delta + 250) {
/* time has rewound. reset the timers. */ /* time has rewound. reset the timers. */
CheckAllTimers(); CheckAllTimers();
timer = first_timer();
} }
if (timers) { if (timer) {
timeout = timers->expires - now; timeout = timer->expires - now;
if (timeout < 0) if (timeout < 0)
timeout = 0; timeout = 0;
} }
@ -224,17 +235,17 @@ WaitForSomething(Bool are_ready)
if (*checkForInput[0] != *checkForInput[1]) if (*checkForInput[0] != *checkForInput[1])
return FALSE; return FALSE;
if (timers) { if ((timer = first_timer()) != NULL) {
int expired = 0; int expired = 0;
now = GetTimeInMillis(); now = GetTimeInMillis();
if ((int) (timers->expires - now) <= 0) if ((int) (timer->expires - now) <= 0)
expired = 1; expired = 1;
if (expired) { if (expired) {
OsBlockSignals(); OsBlockSignals();
while (timers && (int) (timers->expires - now) <= 0) while ((timer = first_timer()) != NULL && (int) (timer->expires - now) <= 0)
DoTimer(timers, now, &timers); DoTimer(timer, now);
OsReleaseSignals(); OsReleaseSignals();
return FALSE; return FALSE;
@ -246,17 +257,17 @@ WaitForSomething(Bool are_ready)
if (*checkForInput[0] != *checkForInput[1]) if (*checkForInput[0] != *checkForInput[1])
return FALSE; return FALSE;
if (timers) { if ((timer = first_timer()) != NULL) {
int expired = 0; int expired = 0;
now = GetTimeInMillis(); now = GetTimeInMillis();
if ((int) (timers->expires - now) <= 0) if ((int) (timer->expires - now) <= 0)
expired = 1; expired = 1;
if (expired) { if (expired) {
OsBlockSignals(); OsBlockSignals();
while (timers && (int) (timers->expires - now) <= 0) while ((timer = first_timer()) != NULL && (int) (timer->expires - now) <= 0)
DoTimer(timers, now, &timers); DoTimer(timer, now);
OsReleaseSignals(); OsReleaseSignals();
return FALSE; return FALSE;
@ -288,6 +299,10 @@ AdjustWaitForDelay(void *waitTime, int newdelay)
*timeoutp = newdelay; *timeoutp = newdelay;
} }
static inline Bool timer_pending(OsTimerPtr timer) {
return !xorg_list_is_empty(&timer->list);
}
/* If time has rewound, re-run every affected timer. /* If time has rewound, re-run every affected timer.
* Timers might drop out of the list, so we have to restart every time. */ * Timers might drop out of the list, so we have to restart every time. */
static void static void
@ -300,9 +315,9 @@ CheckAllTimers(void)
start: start:
now = GetTimeInMillis(); now = GetTimeInMillis();
for (timer = timers; timer; timer = timer->next) { xorg_list_for_each_entry(timer, &timers, list) {
if (timer->expires - now > timer->delta + 250) { if (timer->expires - now > timer->delta + 250) {
TimerForce(timer); DoTimer(timer, now);
goto start; goto start;
} }
} }
@ -310,41 +325,49 @@ CheckAllTimers(void)
} }
static void static void
DoTimer(OsTimerPtr timer, CARD32 now, volatile OsTimerPtr *prev) DoTimer(OsTimerPtr timer, CARD32 now)
{ {
CARD32 newTime; CARD32 newTime;
input_lock(); xorg_list_del(&timer->list);
*prev = timer->next;
timer->next = NULL;
input_unlock();
newTime = (*timer->callback) (timer, now, timer->arg); newTime = (*timer->callback) (timer, now, timer->arg);
if (newTime) if (newTime)
TimerSet(timer, 0, newTime, timer->callback, timer->arg); TimerSet(timer, 0, newTime, timer->callback, timer->arg);
} }
static void
DoTimers(CARD32 now)
{
OsTimerPtr timer;
input_lock();
while ((timer = first_timer())) {
if ((int) (timer->expires - now) > 0)
break;
DoTimer(timer, now);
}
input_unlock();
}
OsTimerPtr OsTimerPtr
TimerSet(OsTimerPtr timer, int flags, CARD32 millis, TimerSet(OsTimerPtr timer, int flags, CARD32 millis,
OsTimerCallback func, void *arg) OsTimerCallback func, void *arg)
{ {
volatile OsTimerPtr *prev; OsTimerPtr existing, tmp;
CARD32 now = GetTimeInMillis(); CARD32 now = GetTimeInMillis();
if (!timer) { if (!timer) {
timer = malloc(sizeof(struct _OsTimerRec)); timer = calloc(1, sizeof(struct _OsTimerRec));
if (!timer) if (!timer)
return NULL; return NULL;
xorg_list_init(&timer->list);
} }
else { else {
input_lock(); input_lock();
for (prev = &timers; *prev; prev = &(*prev)->next) { if (timer_pending(timer)) {
if (*prev == timer) { xorg_list_del(&timer->list);
*prev = timer->next; if (flags & TimerForceOld)
if (flags & TimerForceOld) (void) (*timer->callback) (timer, now, timer->arg);
(void) (*timer->callback) (timer, now, timer->arg);
break;
}
} }
input_unlock(); input_unlock();
} }
@ -360,18 +383,19 @@ TimerSet(OsTimerPtr timer, int flags, CARD32 millis,
timer->expires = millis; timer->expires = millis;
timer->callback = func; timer->callback = func;
timer->arg = arg; timer->arg = arg;
if ((int) (millis - now) <= 0) {
timer->next = NULL;
millis = (*timer->callback) (timer, now, timer->arg);
if (!millis)
return timer;
}
input_lock(); input_lock();
for (prev = &timers;
*prev && (int) ((*prev)->expires - millis) <= 0; /* Sort into list */
prev = &(*prev)->next); xorg_list_for_each_entry_safe(existing, tmp, &timers, list)
timer->next = *prev; if ((int) (existing->expires - millis) > 0)
*prev = timer; break;
/* This even works at the end of the list -- existing->list will be timers */
xorg_list_add(&timer->list, existing->list.prev);
/* Check to see if the timer is ready to run now */
if ((int) (millis - now) <= 0)
DoTimer(timer, now);
input_unlock(); input_unlock();
return timer; return timer;
} }
@ -379,35 +403,23 @@ TimerSet(OsTimerPtr timer, int flags, CARD32 millis,
Bool Bool
TimerForce(OsTimerPtr timer) TimerForce(OsTimerPtr timer)
{ {
int rc = FALSE; int pending;
volatile OsTimerPtr *prev;
input_lock(); input_lock();
for (prev = &timers; *prev; prev = &(*prev)->next) { pending = timer_pending(timer);
if (*prev == timer) { if (pending)
DoTimer(timer, GetTimeInMillis(), prev); DoTimer(timer, GetTimeInMillis());
rc = TRUE;
break;
}
}
input_unlock(); input_unlock();
return rc; return pending;
} }
void void
TimerCancel(OsTimerPtr timer) TimerCancel(OsTimerPtr timer)
{ {
volatile OsTimerPtr *prev;
if (!timer) if (!timer)
return; return;
input_lock(); input_lock();
for (prev = &timers; *prev; prev = &(*prev)->next) { xorg_list_del(&timer->list);
if (*prev == timer) {
*prev = timer->next;
break;
}
}
input_unlock(); input_unlock();
} }
@ -423,23 +435,22 @@ TimerFree(OsTimerPtr timer)
void void
TimerCheck(void) TimerCheck(void)
{ {
CARD32 now = GetTimeInMillis(); DoTimers(GetTimeInMillis());
if (timers && (int) (timers->expires - now) <= 0) {
input_lock();
while (timers && (int) (timers->expires - now) <= 0)
DoTimer(timers, now, &timers);
input_unlock();
}
} }
void void
TimerInit(void) TimerInit(void)
{ {
OsTimerPtr timer; static Bool been_here;
OsTimerPtr timer, tmp;
while ((timer = timers)) { if (!been_here) {
timers = timer->next; been_here = TRUE;
xorg_list_init((struct xorg_list*) &timers);
}
xorg_list_for_each_entry_safe(timer, tmp, &timers, list) {
xorg_list_del(&timer->list);
free(timer); free(timer);
} }
} }