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

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