3 * - By John Hodge (thePowersGang)
7 #define SANITY 1 // Enable ASSERTs
11 #include <timers_int.h>
13 #include <hal_proc.h> // Proc_GetCurThread
14 #include <workqueue.h>
15 #include <threads_int.h> // Used to get thread timer
18 void Timer_CallbackThread(void *Unused);
19 void Timer_CallTimers(void);
21 tTimer *Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument);
22 void Time_ScheduleTimer(tTimer *Timer, int Delta);
23 void Time_RemoveTimer(tTimer *Timer);
24 tTimer *Time_AllocateTimer(tTimerCallback *Callback, void *Argument);
26 void Time_InitTimer(tTimer *Timer, tTimerCallback *Callback, void *Argument);
28 void Time_FreeTimer(tTimer *Timer);
29 void Time_Delay(int Time);
33 volatile Uint64 giTicks = 0;
34 volatile Sint64 giTimestamp = 0;
35 volatile Uint64 giPartMiliseconds = 0;
37 tWorkqueue gTimers_CallbackQueue;
38 tShortSpinlock gTimers_ListLock;
41 void Timer_CallbackThread(void *Unused)
43 Threads_SetName("Timer Callback Thread");
44 Workqueue_Init(&gTimers_CallbackQueue, "Timer Callbacks", offsetof(tTimer, Next));
48 tTimer *timer = Workqueue_GetWork(&gTimers_CallbackQueue);
50 if( !timer->Callback ) {
51 LOG("Timer %p doesn't have a callback", timer);
52 ASSERT( timer->Callback );
55 // Save callback and argument (because once the mutex is released
56 // the timer may no longer be valid)
57 tTimerCallback *cb = timer->Callback;
58 void *arg = timer->Argument;
60 LOG("Callback fire %p", timer);
62 // Allow Time_RemoveTimer to be called safely
68 // Mark timer as no longer needed
73 * \fn void Timer_CallTimers()
75 void Timer_CallTimers()
77 SHORTLOCK(&gTimers_ListLock);
78 while( gTimers && gTimers->FiresAfter < now() )
80 // Get timer from list
81 tTimer *timer = gTimers;
83 ASSERT( gTimers != gTimers->Next );
84 gTimers = gTimers->Next;
87 if( timer->Callback ) {
88 LOG("Callback schedule %p", timer);
89 // PROBLEM! Possibly causes rescheudle during interrupt
90 // Mutex_Acquire( &timer->Lock ); // Released once the callback fires
91 Workqueue_AddWork(&gTimers_CallbackQueue, timer);
94 LOG("Event fire %p", timer);
95 ASSERT( timer->Argument );
96 Threads_PostEvent(timer->Argument, THREAD_EVENT_TIMER);
100 SHORTREL(&gTimers_ListLock);
104 * \brief Schedule an action (Legacy)
106 tTimer *Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument)
108 tTimer *ret = malloc(sizeof(tTimer));
109 if( !ret ) return NULL;
110 Time_InitTimer(ret, Callback, Argument);
111 Time_ScheduleTimer(ret, Delta);
116 * \brief Schedule a timer to fire
118 void Time_ScheduleTimer(tTimer *Timer, int Delta)
124 if( !Timer ) return ;
126 if( Timer->bActive ) return;
129 Timer->FiresAfter = now() + Delta;
132 LOG("%p added timer %p - %i ms (ts=%lli)",
133 __builtin_return_address(0), Timer, Delta, Timer->FiresAfter);
135 // Add into list (sorted)
136 SHORTLOCK(&gTimers_ListLock);
137 // Mutex_Release( &Timer->Lock ); // Prevent deadlocks
138 for( prev_next = &gTimers, t = gTimers; t; prev_next = &t->Next, t = t->Next )
140 ASSERT( prev_next != &t->Next );
141 ASSERT( CheckMem(t, sizeof(tTimer)) );
144 LOG("Double schedule - increasing delta");
145 SHORTREL(&gTimers_ListLock);
148 LOG(" t = %p ts:%lli", t, t->FiresAfter);
149 if( t->FiresAfter > Timer->FiresAfter ) break;
154 LOG(" %p %p %p", prev_next, Timer, t);
155 SHORTREL(&gTimers_ListLock);
159 * \brief Delete a timer from the running list
161 void Time_RemoveTimer(tTimer *Timer)
166 if( !Timer ) return ;
168 SHORTLOCK(&gTimers_ListLock);
169 for( prev_next = &gTimers, t = gTimers; t; prev_next = &t->Next, t = t->Next )
171 ASSERT( prev_next != &t->Next );
172 ASSERT( CheckMem(t, sizeof(tTimer)) );
175 *prev_next = t->Next;
179 SHORTREL(&gTimers_ListLock);
183 LOG("%p removed %p", __builtin_return_address(0), Timer);
186 LOG("%p tried to remove %p (already complete)", __builtin_return_address(0), Timer);
190 * \brief Allocate a timer object, but don't schedule it
192 tTimer *Time_AllocateTimer(tTimerCallback *Callback, void *Argument)
194 tTimer *ret = malloc(sizeof(tTimer));
195 if( !ret ) return NULL;
196 Time_InitTimer(ret, Callback, Argument);
201 * \brief Initialise a timer
202 * \note Mostly an internal function
204 void Time_InitTimer(tTimer *Timer, tTimerCallback *Callback, void *Argument)
207 Argument = Proc_GetCurThread();
208 Timer->FiresAfter = 0;
209 Timer->Callback = Callback;
210 Timer->Argument = Argument;
211 // memset( &Timer->Lock, 0, sizeof(Timer->Lock) );
216 * \brief Free an allocated timer
218 void Time_FreeTimer(tTimer *Timer)
220 if( !Timer ) return ;
221 Time_RemoveTimer( Timer ); // Just in case
223 // Ensures that we don't free until the timer callback has started
224 while( Timer->bActive ) Threads_Yield();
225 // Release won't be needed, as nothing should be waiting on it
226 // Mutex_Acquire( &Timer->Lock );
230 LOG("%p deallocated %p", __builtin_return_address(0), Timer);
234 * \fn void Time_Delay(int Delay)
235 * \brief Delay for a small ammount of time
237 void Time_Delay(int Delay)
239 tTimer *t = &Proc_GetCurThread()->ThreadTimer;
240 Time_InitTimer(t, NULL, NULL);
241 Time_ScheduleTimer(t, Delay);
242 Threads_WaitEvents(THREAD_EVENT_TIMER);
246 //EXPORT(Time_CreateTimer);
247 EXPORT(Time_ScheduleTimer);
248 EXPORT(Time_RemoveTimer);
249 EXPORT(Time_AllocateTimer);
250 //EXPORT(Time_InitTimer);
251 EXPORT(Time_FreeTimer);