Attempt to fix multiboot module support
[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];
28
29 // === CODE ===
30 /**
31  * \fn Sint64 now()
32  * \brief Return the current timestamp
33  */
34 Sint64 now(void)
35 {
36         return giTimestamp;
37 }
38
39 /**
40  * \fn void Timer_CallTimers()
41  */
42 void Timer_CallTimers()
43 {
44          int    i;
45         void    (*callback)(void *);
46         
47         for(i = 0;
48                 i < NUM_TIMERS;
49                 i ++)
50         {
51                 if(gTimers[i].Callback == NULL) continue;
52                 if(giTimestamp < gTimers[i].FiresAfter) continue;
53                 callback = gTimers[i].Callback;
54                 gTimers[i].Callback = NULL;
55                 callback(gTimers[i].Argument);
56         }
57 }
58
59 /**
60  * \fn int Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument)
61  */
62 int Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument)
63 {
64          int    ret;
65         
66         if(Callback == NULL)    return -1;
67         
68         for(ret = 0;
69                 ret < NUM_TIMERS;
70                 ret++)
71         {
72                 if(gTimers[ret].Callback != NULL)       continue;
73                 gTimers[ret].Callback = Callback;
74                 gTimers[ret].FiresAfter = giTimestamp + Delta;
75                 gTimers[ret].Argument = Argument;
76                 //Log("Callback = %p", Callback);
77                 //Log("Timer %i fires at %lli", ret, gTimers[ret].FiresAfter);
78                 return ret;
79         }
80         return -1;
81 }
82
83 /**
84  * \fn void Time_RemoveTimer(int ID)
85  */
86 void Time_RemoveTimer(int ID)
87 {
88         if(ID < 0 || ID >= NUM_TIMERS)  return;
89         gTimers[ID].Callback = NULL;
90 }
91
92 /**
93  * \fn void Time_Delay(int Delay)
94  * \brief Delay for a small ammount of time
95  */
96 void Time_Delay(int Delay)
97 {
98         Sint64  dest = giTimestamp + Delay;
99         while(dest < giTimestamp)       Threads_Yield();
100 }
101
102 // === EXPORTS ===
103 EXPORT(now);
104 EXPORT(Time_CreateTimer);
105 EXPORT(Time_RemoveTimer);
106 EXPORT(Time_Delay);

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