Fixed bad handling of VFS_AllocHandle_
[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 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         void    *arg;
47         
48         for(i = 0; i < NUM_TIMERS; i ++)
49         {
50                 if(gTimers[i].Callback == NULL) continue;
51                 if(giTimestamp < gTimers[i].FiresAfter) continue;
52                 callback = gTimers[i].Callback; arg = gTimers[i].Argument;
53                 gTimers[i].Callback = NULL;
54                 callback(arg);
55         }
56 }
57
58 /**
59  * \fn int Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument)
60  */
61 int Time_CreateTimer(int Delta, tTimerCallback *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