You are on page 1of 18

Chapter 5:

Time Management

1
Time Management
• OSTimeDly()
• OSTimeDlyHMSM()
• OSTimeDlyResume()
• OSTimeGet()
• OSTimeSet()

2
Properties of μC/OS-IITime Management

• The system overhead is proportioned to the


frequency of ticks
– Compared to the uTime package of linux
void InterruptServiceRoutine{
.
• Delay a task until tx .
t=OSTimeGet(); .
}
if (tx-t>0)
void HeighPriorityTask {
OSTimeDly(tx-t); .
– What’s wrong .
.
OSTime=5 }
OSTime= 6 3
Time Management Services
• OSTimeDly(), OSTimeDlyHMSM()
– Allow a task to delay itself for a number of ticks
– If your application must delay for at least one tick,
you must call OSTimeDly(2)
• OSTimeDlyResume()
Resume a delayed task
• OSTimeSet(), OSTimeGet()
Set/obtain the system timer

4
OSTimeDly()

Store the number of ticks in the


TCB of the current task.

Because the task is no longer ready, the


scheduler should be called to active the next
highest priority task.
5
OSTimeDlyHMSM()

6
OSTimeTick()

OSTickISR() {
OSTimeTick() {
OSTimeTick()
foreach(…)
}
}

7
OSTCBList
Timer (1)
TCB TCB TCB TCB TCB
. . . . .
. . . . .
. . . . .
OSTCBDly OSTCBDly OSTCBDly OSTCBDly OSTCBDly
. . . . .
. . . . .
. . . . .

OSTickISR() {
OSTimeTick() {
OSTimeTick()
foreach(…)
}
}

8
OSTCBList
Timer (2)
TCB TCB TCB TCB TCB
. . . . .
. . . . .
. . . . .
OSTCBDly OSTCBDly OSTCBDly OSTCBDly OSTCBDly
. . . . .
. . . . .
. . . . .

OSTickISR() { OSTimeTick() { TickTask() {


OSMBoxPend()
OSMBoxPost() foreach(…) OSTimeTick()
OS_Sched()
} } }

9
Clear the
time delay
OSTimeDlyResume()

Make the
task ready
to run See if the task is ready to run.
A delayed task might also
have been suspended.

10
OSTimeDlyResume()

OSChangePrio() may change this value

11
OSTimeGet() & OSTimeSet()

The tick interrupt may


The mayread/write
read/write
thevariable
the variable“OSTime”
“OSTime” while
while wewe
call
callOSTimeGet/Set.
OSTimeGet/Set.

12
OSTimeDlyUntil()
void function1() {
OS_ENTER_CRITICAL();
t=OSTimeGet();
if (tx-t>0)
OSTimeDly(tx-t);
OS_EXIT_CRITICAL();
}

void function2() {
OSSchedLock();
t=OSTimeGet();
if (tx-t>0)
OSTimeDly(tx-t);
OSSchedUnlock()
}

13
OSTimeDlyUntil()
void function2() {
OSTimeLockUntil(tx);
}

/* OSTimeDlyUntil() is a new system call.*/


void OSTimeDlyUntil(INT16U tx) {
int ticks;
OS_ENTER_CRITICAL();
ticks = tx- OSTime
if (ticks > 0) {
if ((OSRdyTbl[OSTCBCur->OSTCBY] &= ~OSTCBCur->OSTCBBitX) == 0) {
OSRdyGrp &= ~OSTCBCur->OSTCBBitY;
}
OSTCBCur->OSTCBDly = ticks;
OS_EXIT_CRITICAL();
OS_Sched();
}
14
A Lightweight Implementation of Time Service (BSD)

• Idea: using a linked list to describe waiting


events
– The list is sorted in time order
– The time for each event is kept as a difference
from the time of the previous event in the linked
list

3ticks 2ticks 4ticks 1ticks


NIL
(3) (5) (9) (10)

15
Example
12 ticks 2 ticks 4 ticks 1 ticks
before NIL
(12) (14) (18) (19)

INT32U TestNewTimer (void) {

OSTimeDly(16); /*at time 16*/

12 ticks 2 ticks 2 ticks 2 ticks 1 ticks


after NIL
(12) (14) (16) (18) (19)

16
Enhancement
• The hardware timer can trigger the software
timer until the time of the next time event.

3ticks 2ticks 4ticks 1ticks


NIL
(3) (5) (9) (10)

Set H/W Set H/W


timer = 3 timer = 4
Set H/W Set H/W
timer = 2 timer = 1

17
Enhancement
• The relation between time granularity
and system overhead can be broken if
we modify the tick service.

• We should maintain the semantics of


the time-related services

18

You might also like