Kernel - Cleaning up a little
[tpg/acess2.git] / Kernel / time.c
1 /*
2  * Acess 2
3  * - By John Hodge (thePowersGang) 
4  *
5  * Timer Code
6  */
7 #include <acess.h>
8
9 // === CONSTANTS ===
10 #define NUM_TIMERS      8
11
12 // === TYPEDEFS ===
13 typedef struct sTimer {
14          int    FiresAfter;
15         void    (*Callback)(void*);
16         void    *Argument;
17 } tTimer;
18
19 // === PROTOTYPES ===
20 void    Timer_CallTimers(void);
21
22 // === GLOBALS ===
23 volatile Uint64 giTicks = 0;
24 volatile Sint64 giTimestamp = 0;
25 volatile Uint64 giPartMiliseconds = 0;
26 tTimer  gTimers[NUM_TIMERS];    // TODO: Replace by a ring-list timer
27
28 // === CODE ===
29 /**
30  * \fn void Timer_CallTimers()
31  */
32 void Timer_CallTimers()
33 {
34          int    i;
35         void    (*callback)(void *);
36         void    *arg;
37         
38         for(i = 0; i < NUM_TIMERS; i ++)
39         {
40                 if(gTimers[i].Callback == NULL) continue;
41                 if(giTimestamp < gTimers[i].FiresAfter) continue;
42                 callback = gTimers[i].Callback; arg = gTimers[i].Argument;
43                 gTimers[i].Callback = NULL;
44                 callback(arg);
45         }
46 }
47
48 /**
49  * \fn int Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument)
50  */
51 int Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument)
52 {
53          int    ret;
54         
55         if(Callback == NULL)    return -1;
56         
57         for(ret = 0;
58                 ret < NUM_TIMERS;
59                 ret++)
60         {
61                 if(gTimers[ret].Callback != NULL)       continue;
62                 gTimers[ret].Callback = Callback;
63                 gTimers[ret].FiresAfter = giTimestamp + Delta;
64                 gTimers[ret].Argument = Argument;
65                 //Log("Callback = %p", Callback);
66                 //Log("Timer %i fires at %lli", ret, gTimers[ret].FiresAfter);
67                 return ret;
68         }
69         return -1;
70 }
71
72 /**
73  * \fn void Time_RemoveTimer(int ID)
74  */
75 void Time_RemoveTimer(int ID)
76 {
77         if(ID < 0 || ID >= NUM_TIMERS)  return;
78         gTimers[ID].Callback = NULL;
79 }
80
81 /**
82  * \fn void Time_Delay(int Delay)
83  * \brief Delay for a small ammount of time
84  */
85 void Time_Delay(int Delay)
86 {
87         tTime   dest = now() + Delay;
88         while(dest > now())     Threads_Yield();
89 }
90
91 // === EXPORTS ===
92 EXPORT(Time_CreateTimer);
93 EXPORT(Time_RemoveTimer);
94 EXPORT(Time_Delay);

UCC git Repository :: git.ucc.asn.au