9ddf26a374e28da4d766e7c4834521507226e4da
[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 Sint64  now(void);
21 void    Timer_CallTimers(void);
22
23 // === GLOBALS ===
24 Uint64  giTicks = 0;
25 Sint64  giTimestamp = 0;
26 Uint64  giPartMiliseconds = 0;
27 tTimer  gTimers[NUM_TIMERS];    // TODO: Replace by a ring-list timer
28
29 // === CODE ===
30 /**
31  * \fn void Timer_CallTimers()
32  */
33 void Timer_CallTimers()
34 {
35          int    i;
36         void    (*callback)(void *);
37         void    *arg;
38         
39         for(i = 0; i < NUM_TIMERS; i ++)
40         {
41                 if(gTimers[i].Callback == NULL) continue;
42                 if(giTimestamp < gTimers[i].FiresAfter) continue;
43                 callback = gTimers[i].Callback; arg = gTimers[i].Argument;
44                 gTimers[i].Callback = NULL;
45                 callback(arg);
46         }
47 }
48
49 /**
50  * \fn int Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument)
51  */
52 int Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument)
53 {
54          int    ret;
55         
56         if(Callback == NULL)    return -1;
57         
58         for(ret = 0;
59                 ret < NUM_TIMERS;
60                 ret++)
61         {
62                 if(gTimers[ret].Callback != NULL)       continue;
63                 gTimers[ret].Callback = Callback;
64                 gTimers[ret].FiresAfter = giTimestamp + Delta;
65                 gTimers[ret].Argument = Argument;
66                 //Log("Callback = %p", Callback);
67                 //Log("Timer %i fires at %lli", ret, gTimers[ret].FiresAfter);
68                 return ret;
69         }
70         return -1;
71 }
72
73 /**
74  * \fn void Time_RemoveTimer(int ID)
75  */
76 void Time_RemoveTimer(int ID)
77 {
78         if(ID < 0 || ID >= NUM_TIMERS)  return;
79         gTimers[ID].Callback = NULL;
80 }
81
82 /**
83  * \fn void Time_Delay(int Delay)
84  * \brief Delay for a small ammount of time
85  */
86 void Time_Delay(int Delay)
87 {
88         Sint64  dest = giTimestamp + Delay;
89         while(dest > giTimestamp)       Threads_Yield();
90 }
91
92 // === EXPORTS ===
93 EXPORT(now);
94 EXPORT(Time_CreateTimer);
95 EXPORT(Time_RemoveTimer);
96 EXPORT(Time_Delay);

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