- Implemented execp*.
[planlOS.git] / system / include / ke / timer.h
blobfdda1516f7cbcaad8042fb82fb0d601b0c6c874e
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 /**
22 * \file timer.h
23 * Time/timer functions
26 #ifndef KE_TIMER_H_INCLUDED
27 #define KE_TIMER_H_INCLUDED
29 #include <stdint.h>
31 struct KeTimer;
33 /**
34 * Timer callback function
36 typedef void (*KeTimerCallback)(struct KeTimer *timer);
38 /**
39 * Generic timer struct. If the timer is triggered, an interrupt is caused, so
40 * don't use this timer with too small intervals as it has absolute priority.
42 typedef struct KeTimer
44 /**
45 * Time to be waited in microseconds. If the timer is periodic, this is a
46 * relative value, otherwise it's an absolute value which can be computed by
47 * adding your time to keGetTime().
49 uint64_t time;
50 /**
51 * Sets whether this is a periodic timer, e.g. gets reactivated after it
52 * expired.
54 int periodic;
56 /**
57 * Callback to be called after the timer has expired.
59 KeTimerCallback callback;
61 /**
62 * Absolute time at which the timer expires. Must not be set by the user.
64 uint64_t targettime;
66 /**
67 * 1, if the timer is currently active, e.g. is the next timer in the queue.
68 * Must not be set by the user.
70 int active;
71 /**
72 * 1, if the timer is queued. Must not be set by the user.
74 int queued;
75 /**
76 * Next timer in the queue.
78 struct KeTimer *next;
79 /**
80 * Previous timer in the queue.
82 struct KeTimer *prev;
83 } KeTimer;
85 /**
86 * Initializes the timer functions. Is called by the operating system at
87 * startup.
89 void keInitTimer(uint32_t frequency);
91 /**
92 * Installs the timer system on the current CPU.
94 void keInstallTimer(void);
96 /**
97 * Creates a new timer. The user then has to specify callback in the timer
98 * struct before passing it to keSetTimer().
100 KeTimer *keCreateTimer(void);
102 * Unqueues and destroys a timer.
103 * \todo Stopping the timer does not quite work yet.
105 int keDestroyTimer(KeTimer *timer);
107 * Adds a timer to the timer queue.
108 * \param timer Timer to be activated
109 * \param time Time to be waited. If the timer is not periodic, this is an
110 * absolute value.
111 * \param periodic If set to 1, the timer is activated again if it expires.
113 int keSetTimer(KeTimer *timer, uint64_t time, int periodic);
115 * Cancels an activated timer.
116 * \todo Does not quite work yet if the timer does not belong to the current
117 * CPU.
119 int keCancelTimer(KeTimer *timer);
121 * Adjusts a timer.
122 * \todo Does not quite work yet if the timer does not belong to the current
123 * CPU.
125 int keAdjustTimer(KeTimer *timer, uint64_t time);
128 * Returns the current time in microseconds since the start of the operating
129 * system.
131 uint64_t keGetTime(void);
133 * Returns the interval in which threads are rescheduled.
135 uint32_t keGetTimePerTick(void);
138 * Returns the current time in seconds since unix epoch.
140 uint32_t keGetUnixTime(void);
142 #endif