2 * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
5 #ifndef _KERNEL_USER_TIMER_H
6 #define _KERNEL_USER_TIMER_H
12 #include <util/DoublyLinkedList.h>
16 #include <user_timer_defs.h>
19 struct thread_creation_attributes
;
29 struct UserTimer
: DoublyLinkedListLinkImpl
<UserTimer
> {
38 void SetEvent(UserEvent
* event
)
41 virtual void Schedule(bigtime_t nextTime
, bigtime_t interval
,
42 uint32 flags
, bigtime_t
& _oldRemainingTime
,
43 bigtime_t
& _oldInterval
) = 0;
46 virtual void GetInfo(bigtime_t
& _remainingTime
,
48 uint32
& _overrunCount
) = 0;
51 static int32
HandleTimerHook(struct timer
* timer
);
52 virtual void HandleTimer();
54 inline void UpdatePeriodicStartTime();
55 inline void CheckPeriodicOverrun(bigtime_t now
);
57 inline void CancelTimer();
66 bool fScheduled
; // fTimer scheduled
71 struct SystemTimeUserTimer
: public UserTimer
{
72 virtual void Schedule(bigtime_t nextTime
, bigtime_t interval
,
73 uint32 flags
, bigtime_t
& _oldRemainingTime
,
74 bigtime_t
& _oldInterval
);
75 virtual void GetInfo(bigtime_t
& _remainingTime
,
77 uint32
& _overrunCount
);
80 virtual void HandleTimer();
82 void ScheduleKernelTimer(bigtime_t now
,
83 bool checkPeriodicOverrun
);
87 struct RealTimeUserTimer
: public SystemTimeUserTimer
{
88 virtual void Schedule(bigtime_t nextTime
, bigtime_t interval
,
89 uint32 flags
, bigtime_t
& _oldRemainingTime
,
90 bigtime_t
& _oldInterval
);
95 bigtime_t fRealTimeOffset
;
99 virtual void HandleTimer();
102 // conceptually package private
103 DoublyLinkedListLink
<RealTimeUserTimer
> fGlobalListLink
;
107 struct TeamTimeUserTimer
: public UserTimer
{
108 TeamTimeUserTimer(team_id teamID
);
109 ~TeamTimeUserTimer();
111 virtual void Schedule(bigtime_t nextTime
, bigtime_t interval
,
112 uint32 flags
, bigtime_t
& _oldRemainingTime
,
113 bigtime_t
& _oldInterval
);
114 virtual void GetInfo(bigtime_t
& _remainingTime
,
115 bigtime_t
& _interval
,
116 uint32
& _overrunCount
);
120 void Update(Thread
* unscheduledThread
,
121 Thread
* lockedThread
= NULL
);
122 void TimeWarped(bigtime_t changedBy
);
125 virtual void HandleTimer();
128 void _Update(bool unscheduling
,
129 Thread
* lockedThread
= NULL
);
134 int32 fRunningThreads
;
138 // conceptually package private
139 DoublyLinkedListLink
<TeamTimeUserTimer
> fCPUTimeListLink
;
143 struct TeamUserTimeUserTimer
: public UserTimer
{
144 TeamUserTimeUserTimer(team_id teamID
);
145 ~TeamUserTimeUserTimer();
147 virtual void Schedule(bigtime_t nextTime
, bigtime_t interval
,
148 uint32 flags
, bigtime_t
& _oldRemainingTime
,
149 bigtime_t
& _oldInterval
);
150 virtual void GetInfo(bigtime_t
& _remainingTime
,
151 bigtime_t
& _interval
,
152 uint32
& _overrunCount
);
162 // conceptually package private
163 DoublyLinkedListLink
<TeamUserTimeUserTimer
> fCPUTimeListLink
;
167 struct ThreadTimeUserTimer
: public UserTimer
{
168 ThreadTimeUserTimer(thread_id threadID
);
169 ~ThreadTimeUserTimer();
171 virtual void Schedule(bigtime_t nextTime
, bigtime_t interval
,
172 uint32 flags
, bigtime_t
& _oldRemainingTime
,
173 bigtime_t
& _oldInterval
);
174 virtual void GetInfo(bigtime_t
& _remainingTime
,
175 bigtime_t
& _interval
,
176 uint32
& _overrunCount
);
182 void TimeWarped(bigtime_t changedBy
);
185 virtual void HandleTimer();
189 Thread
* fThread
; // != NULL only when active
193 // conceptually package private
194 DoublyLinkedListLink
<ThreadTimeUserTimer
> fCPUTimeListLink
;
198 struct UserTimerList
{
202 UserTimer
* TimerFor(int32 id
) const;
203 void AddTimer(UserTimer
* timer
);
204 void RemoveTimer(UserTimer
* timer
)
205 { fTimers
.Remove(timer
); }
206 int32
DeleteTimers(bool userDefinedOnly
);
209 typedef DoublyLinkedList
<UserTimer
> TimerList
;
216 typedef DoublyLinkedList
<RealTimeUserTimer
,
217 DoublyLinkedListMemberGetLink
<RealTimeUserTimer
,
218 &RealTimeUserTimer::fGlobalListLink
> > RealTimeUserTimerList
;
220 typedef DoublyLinkedList
<TeamTimeUserTimer
,
221 DoublyLinkedListMemberGetLink
<TeamTimeUserTimer
,
222 &TeamTimeUserTimer::fCPUTimeListLink
> > TeamTimeUserTimerList
;
224 typedef DoublyLinkedList
<TeamUserTimeUserTimer
,
225 DoublyLinkedListMemberGetLink
<TeamUserTimeUserTimer
,
226 &TeamUserTimeUserTimer::fCPUTimeListLink
> > TeamUserTimeUserTimerList
;
228 typedef DoublyLinkedList
<ThreadTimeUserTimer
,
229 DoublyLinkedListMemberGetLink
<ThreadTimeUserTimer
,
230 &ThreadTimeUserTimer::fCPUTimeListLink
> > ThreadTimeUserTimerList
;
233 } // namespace BKernel
236 using BKernel::RealTimeUserTimer
;
237 using BKernel::RealTimeUserTimerList
;
238 using BKernel::SystemTimeUserTimer
;
239 using BKernel::TeamUserTimeUserTimer
;
240 using BKernel::TeamUserTimeUserTimerList
;
241 using BKernel::TeamTimeUserTimer
;
242 using BKernel::TeamTimeUserTimerList
;
243 using BKernel::ThreadTimeUserTimer
;
244 using BKernel::ThreadTimeUserTimerList
;
245 using BKernel::UserTimer
;
246 using BKernel::UserTimerList
;
251 status_t
user_timer_create_thread_timers(Team
* team
, Thread
* thread
);
252 status_t
user_timer_create_team_timers(Team
* team
);
254 status_t
user_timer_get_clock(clockid_t clockID
, bigtime_t
& _time
);
255 void user_timer_real_time_clock_changed();
257 void user_timer_stop_cpu_timers(Thread
* thread
, Thread
* nextThread
);
258 void user_timer_continue_cpu_timers(Thread
* thread
,
259 Thread
* previousThread
);
260 void user_timer_check_team_user_timers(Team
* team
);
262 status_t
_user_get_clock(clockid_t clockID
, bigtime_t
* _time
);
263 status_t
_user_set_clock(clockid_t clockID
, bigtime_t time
);
265 int32
_user_create_timer(clockid_t clockID
, thread_id threadID
,
266 uint32 flags
, const struct sigevent
* event
,
267 const thread_creation_attributes
* threadAttributes
);
268 status_t
_user_delete_timer(int32 timerID
, thread_id threadID
);
269 status_t
_user_get_timer(int32 timerID
, thread_id threadID
,
270 struct user_timer_info
* info
);
271 status_t
_user_set_timer(int32 timerID
, thread_id threadID
,
272 bigtime_t startTime
, bigtime_t interval
, uint32 flags
,
273 struct user_timer_info
* oldInfo
);
278 #endif // _KERNEL_USER_TIMER_H