4 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 // Internal prototypes
22 Returns the current system time.
24 @return The current system time
28 CoreCurrentSystemTime (
33 Checks the sorted timer list against the current system time.
34 Signals any expired event timer.
36 @param CheckEvent Not used
37 @param Context Not used
43 IN EFI_EVENT CheckEvent
,
48 Inserts the timer event.
50 @param Event Points to the internal structure of timer event
55 CoreInsertEventTimer (
63 LIST_ENTRY mEfiTimerList
= INITIALIZE_LIST_HEAD_VARIABLE (mEfiTimerList
);
64 EFI_LOCK mEfiTimerLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL
- 1);
65 EFI_EVENT mEfiCheckTimerEvent
= NULL
;
67 EFI_LOCK mEfiSystemTimeLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_HIGH_LEVEL
);
68 UINT64 mEfiSystemTime
= 0;
74 Initializes timer support.
84 Status
= CoreCreateEvent (
91 ASSERT_EFI_ERROR (Status
);
96 Returns the current system time.
98 @return The current system time
102 CoreCurrentSystemTime (
108 CoreAcquireLock (&mEfiSystemTimeLock
);
109 SystemTime
= mEfiSystemTime
;
110 CoreReleaseLock (&mEfiSystemTimeLock
);
117 Called by the platform code to process a tick.
119 @param Duration The number of 100ns elasped since the last call
132 // Check runtiem flag in case there are ticks while exiting boot services
134 CoreAcquireLock (&mEfiSystemTimeLock
);
137 // Update the system time
139 mEfiSystemTime
+= Duration
;
142 // If the head of the list is expired, fire the timer event
145 if (!IsListEmpty (&mEfiTimerList
)) {
146 Event
= CR (mEfiTimerList
.ForwardLink
, IEVENT
, u
.Timer
.Link
, EVENT_SIGNATURE
);
148 if (Event
->u
.Timer
.TriggerTime
<= mEfiSystemTime
) {
149 CoreSignalEvent (mEfiCheckTimerEvent
);
153 CoreReleaseLock (&mEfiSystemTimeLock
);
158 Checks the sorted timer list against the current system time.
159 Signals any expired event timer.
161 @param CheckEvent Not used
162 @param Context Not used
168 IN EFI_EVENT CheckEvent
,
176 // Check the timer database for expired timers
178 CoreAcquireLock (&mEfiTimerLock
);
179 SystemTime
= CoreCurrentSystemTime ();
181 while (!IsListEmpty (&mEfiTimerList
)) {
182 Event
= CR (mEfiTimerList
.ForwardLink
, IEVENT
, u
.Timer
.Link
, EVENT_SIGNATURE
);
185 // If this timer is not expired, then we're done
187 if (Event
->u
.Timer
.TriggerTime
> SystemTime
) {
192 // Remove this timer from the timer queue
195 RemoveEntryList (&Event
->u
.Timer
.Link
);
196 Event
->u
.Timer
.Link
.ForwardLink
= NULL
;
201 CoreSignalEvent (Event
);
204 // If this is a periodic timer, set it
206 if (Event
->u
.Timer
.Period
) {
208 // Compute the timers new trigger time
210 Event
->u
.Timer
.TriggerTime
= Event
->u
.Timer
.TriggerTime
+ Event
->u
.Timer
.Period
;
213 // If that's before now, then reset the timer to start from now
215 if (Event
->u
.Timer
.TriggerTime
<= SystemTime
) {
216 Event
->u
.Timer
.TriggerTime
= SystemTime
;
217 CoreSignalEvent (mEfiCheckTimerEvent
);
223 CoreInsertEventTimer (Event
);
227 CoreReleaseLock (&mEfiTimerLock
);
232 Inserts the timer event.
234 @param Event Points to the internal structure of timer event
239 CoreInsertEventTimer (
247 ASSERT_LOCKED (&mEfiTimerLock
);
250 // Get the timer's trigger time
252 TriggerTime
= Event
->u
.Timer
.TriggerTime
;
255 // Insert the timer into the timer database in assending sorted order
257 for (Link
= mEfiTimerList
.ForwardLink
; Link
!= &mEfiTimerList
; Link
= Link
->ForwardLink
) {
258 Event2
= CR (Link
, IEVENT
, u
.Timer
.Link
, EVENT_SIGNATURE
);
260 if (Event2
->u
.Timer
.TriggerTime
> TriggerTime
) {
265 InsertTailList (Link
, &Event
->u
.Timer
.Link
);
271 Sets the type of timer and the trigger time for a timer event.
273 @param UserEvent The timer event that is to be signaled at the
275 @param Type The type of time that is specified in
277 @param TriggerTime The number of 100ns units until the timer
280 @retval EFI_SUCCESS The event has been set to be signaled at the
282 @retval EFI_INVALID_PARAMETER Event or Type is not valid
288 IN EFI_EVENT UserEvent
,
289 IN EFI_TIMER_DELAY Type
,
290 IN UINT64 TriggerTime
298 return EFI_INVALID_PARAMETER
;
301 if (Event
->Signature
!= EVENT_SIGNATURE
) {
302 return EFI_INVALID_PARAMETER
;
305 if (Type
< 0 || Type
> TimerRelative
|| !(Event
->Type
& EVT_TIMER
)) {
306 return EFI_INVALID_PARAMETER
;
309 CoreAcquireLock (&mEfiTimerLock
);
312 // If the timer is queued to the timer database, remove it
314 if (Event
->u
.Timer
.Link
.ForwardLink
!= NULL
) {
315 RemoveEntryList (&Event
->u
.Timer
.Link
);
316 Event
->u
.Timer
.Link
.ForwardLink
= NULL
;
319 Event
->u
.Timer
.TriggerTime
= 0;
320 Event
->u
.Timer
.Period
= 0;
322 if (Type
!= TimerCancel
) {
324 if (Type
== TimerPeriodic
) {
325 Event
->u
.Timer
.Period
= TriggerTime
;
328 Event
->u
.Timer
.TriggerTime
= CoreCurrentSystemTime () + TriggerTime
;
329 CoreInsertEventTimer (Event
);
331 if (TriggerTime
== 0) {
332 CoreSignalEvent (mEfiCheckTimerEvent
);
336 CoreReleaseLock (&mEfiTimerLock
);