10 #if GFX_USE_GTIMER || defined(__DOXYGEN__)
12 #define GTIMER_FLG_PERIODIC 0x0001
13 #define GTIMER_FLG_INFINITE 0x0002
14 #define GTIMER_FLG_JABBED 0x0004
15 #define GTIMER_FLG_SCHEDULED 0x0008
18 #define TimeIsWithin(x, start, end) ((end >= start && x >= start && x <= end) || (end < start && (x >= start || x <= end)))
23 static GTimer *pTimerHead = 0;
25 static gTicks ticks2ms;
41 nxtTimeout = gDelayForever;
52 nxtTimeout = gDelayForever;
61 if ((pt->flags & GTIMER_FLG_JABBED) || (!(pt->flags & GTIMER_FLG_INFINITE) && TimeIsWithin(pt->when, lastTime, tm))) {
64 if ((pt->flags & GTIMER_FLG_PERIODIC) && pt->period != gDelayNone) {
66 if (!(pt->flags & GTIMER_FLG_INFINITE)) {
71 pt->when += ((tm + pt->period - pt->when) / pt->period) * pt->period;
75 pt->flags &= ~GTIMER_FLG_JABBED;
82 pt->next->prev = pt->prev;
83 pt->prev->next = pt->next;
85 pTimerHead = pt->next;
98 goto restartTimerChecks;
102 if (!(pt->flags & GTIMER_FLG_INFINITE) && pt->when - tm < nxtTimeout)
103 nxtTimeout = (pt->when - tm)/ticks2ms;
105 }
while(pt != pTimerHead);
115 void _gtimerInit(
void)
122 void _gtimerDeinit(
void)
149 if (pt->flags & GTIMER_FLG_SCHEDULED) {
154 pt->next->prev = pt->prev;
155 pt->prev->next = pt->next;
156 if (pTimerHead == pt)
157 pTimerHead = pt->next;
164 pt->flags = GTIMER_FLG_SCHEDULED;
166 pt->flags |= GTIMER_FLG_PERIODIC;
167 if (millisec == gDelayForever) {
168 pt->flags |= GTIMER_FLG_INFINITE;
169 pt->period = gDelayForever;
177 pt->next = pTimerHead;
178 pt->prev = pTimerHead->prev;
182 pt->next = pt->prev = pTimerHead = pt;
185 if (!(pt->flags & GTIMER_FLG_INFINITE))
192 if (pt->flags & GTIMER_FLG_SCHEDULED) {
197 pt->next->prev = pt->prev;
198 pt->prev->next = pt->next;
199 if (pTimerHead == pt)
200 pTimerHead = pt->next;
209 return (pt->flags & GTIMER_FLG_SCHEDULED) ? gTrue : gFalse;
216 pt->flags |= GTIMER_FLG_JABBED;
225 pt->flags |= GTIMER_FLG_JABBED;
#define GFX_THREAD_STACK(name, sz)
Declare a thread stack.
void gfxSemSignal(gSem *psem)
Signal a semaphore.
void * gThread
A thread handle.
gTicks gfxSystemTicks(void)
Get the current operating system tick time.
void gfxYield(void)
Yield the current thread.
#define GFX_THREAD_FUNCTION(fnName, param)
Declare a thread function.
void gfxMutexExit(gMutex *pmutex)
Exit the critical code region protected by the mutex.
gTicks gfxMillisecondsToTicks(gDelay ms)
Convert a given number of millseconds to a number of operating system ticks.
gBool gfxSemWait(gSem *psem, gDelay ms)
Wait on a semaphore.
void gfxMutexEnter(gMutex *pmutex)
Enter the critical code region protected by the mutex.
void gfxMutexInit(gMutex *pmutex)
Initialise a mutex to protect a region of code from other threads.
gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn), p), void *param)
Start a new thread.
void gfxSemDestroy(gSem *psem)
Destroy a Counted Semaphore.
void gfxSemInit(gSem *psem, gSemcount val, gSemcount limit)
Initialise a Counted Semaphore.
void gfxMutexDestroy(gMutex *pmutex)
Destroy a Mutex.
void gfxSemSignalI(gSem *psem)
Signal a semaphore.
void gfxThreadClose(gThread thread)
Close the thread handle.
#define GTIMER_THREAD_WORKAREA_SIZE
Defines the size of the timer threads work area (stack+structures).
void gtimerJabI(GTimer *pt)
Jab a timer causing the current period to immediate expire.
void gtimerStart(GTimer *pt, GTimerFunction fn, void *param, gBool periodic, gDelay millisec)
Set a timer going or alter its properties if it is already going.
void gtimerInit(GTimer *pt)
Initialise a timer.
void gtimerDeinit(GTimer *pt)
Deinitialise a timer.
gBool gtimerIsActive(GTimer *pt)
Test if a timer is currently active.
void gtimerStop(GTimer *pt)
Stop a timer (periodic or otherwise)
#define GTIMER_THREAD_PRIORITY
Defines the GTIMER thread priority.
void gtimerJab(GTimer *pt)
Jab a timer causing the current period to immediate expire.