Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 33

Chapter 6:

Event Control Block


(ECB)

1
Synchronization & Communication

Synchronization
• Semaphore (counting semaphore)
• Mutual Exclusion Semaphore (binary
semaphore + PCP)
• Event Flag
Communication
• Message Mailbox
• Message Queue

2
General operations
• OSXxxCreate
• OSXxxDel
• OSXxxPend
Wait for Xxx
• OSXxxAccept
A non-blocking version of OSXxxPend
• OSXxxPost
Release Xxx
• OSXxxQuery

3
Blocking System call
TaskA

TaskB

“Device Not Ready”

4
Nonblocking System call
TaskA

return -1
“Device Not Ready”

5
Asynchronous System call
TaskA TaskA

return 0
“Device Not Ready”

6
Semantics of ECB

• ECB (Event Control Signal Wait


ISR ECB Task
Block) (1)
Timeout
(2)

(3) A
• An ISR or a task can Task
Signal
ECB
Wait
Task
(1) (2)
signal an ECB Timeout
(3)

• Only a task can wait for ISR Task


Signal

an ECB (error!!!) ECB


Wait
B

• An optional timeout
Signal Wait
Task
Task

can be specified in case


the ECB is not signaled Task Wait/Signal

(4)
Timeout

within the specified ECB C

time period Task Wait/Signal Timeout


(4)

7
Semantics of ECB

• Multiple tasks can wait Signal Wait


ISR ECB Task
(1) (2)
for an ECB. Timeout
(3) A
– Only the highest priority Task
Signal
(1)
ECB
Wait
(2)
Task

task (HPT) is made Timeout


(3)

ready to run when the ISR Task


ECB is signaled. Signal
Wait
B
ECB

• When a ECB is used as Task


Signal Wait
Task

a semaphore, tasks can


both wait and signal Task Wait/Signal

(4)
Timeout

ECB C
the ECB. Task Wait/Signal Timeout
(4)

8
The use of ECB
• A building block to implement services such as
– Semaphore
– Mutual Exclusion Semaphore
– Message Mailbox
– Message Queue

• All ECB functions only consider how to manipulate


data structures.
Caller must consider synchronization issues

9
ECB data structure

10
ECB data structure
Type of event control block
.OSEventType
.OSEventGrp ~ OSRdyGrp
.OSEventCnt Semaphore Count…
.OSEventPtr Pointer to message or queue structure
7 6 5 4 3 2 1 0

15 14 13 12 11 10 9 8

.
. ~ OSRdyTbl
.
55 54 53 52 51 50 49 48

63 62 61 60 59 58 57 56

“red” fields are Initialized by OS_EventWaitListInit 11


~: is “similar to”
OSEventType
OSEventGrp
OSEventCnt
OSEventPtr
OSEventTbl[]

OS_EVENT_TYPE_SEM OS_EVENT_TYPE_MUTEX
0x00 0x00
cnt prio OS_MUTEX_AVAILABLE

NULL NULL
0x00 … 0x00 0x00 … 0x00

OS_EVENT_TYPE_QM OS_EVENT_TYPE_MBOX
0x00 0x00
cnt 0x00
Point to Q Point to msg
0x00 … 0x00 0x00 … 0x00 12
ECB Functions
• OS_EventWaitListInit()
Initialize an ECB
• OS_EventTaskRdy()
Make a task ready
• OS_EventTaskWait()
Put the task to sleep
• OS_EventTO()
Make a task ready

13
1 1

ready queue
1
1 1

2. OS_XXX_Post 1. OS_XXX_Pend
Task (OS_Event_TaskRdy) ECB (OS_Event_TaskWait)
Task
(waiting
A queue) B
5

14
1 1

ready queue
1
1 1

OS_XXX_Post 1. OS_XXX_Pend
Task (OS_Event_TaskRdy) ECB (OS_Event_TaskWait)
Task
(waiting
A queue) B
5
2. OS_EventTO

15
OS_EventWaitListInit()

"loop unrolling"

16
OS_EventTaskRdy()
• This function is called by the POST functions
for a semaphore, a mutex, a message mailbox
or a message queue when the ECB is signaled.

• OS_EventTaskRdy() removes the highest


priority task from the wait list of the ECB and
makes this task ready to run.

17
OS_EventTaskRdy() - 1
Waiting Queue Ready Queue

OSRdyGrp 1 1
EventGrp 1 1

1
EventTbl OSRdyTbl
1 1 1

1 1

18
OS_EventTaskRdy() - 2
Waiting Queue Ready Queue

OSRdyGrp 1 1
EventGrp 1

EventTbl OSRdyTbl
1 1 1

1 1

19
OS_EventTaskRdy() - 3
Waiting Queue Ready Queue

OSRdyGrp 1 1 1
EventGrp 1

1
EventTbl OSRdyTbl
1 1 1

1 1

20
OS_EventTaskRdy() - 4
OSTCBDly=?

OSTCBEventPtr

OSTCBStat

1 1 Task Control Block


(TCB)

Event Control Block


(ECB)
21
OS_EventTaskRdy() - 5
OSTCBDly=?

OSTCBEventPtr

OSTCBStat

1 1 Task Control Block


(TCB)

Event Control Block


(ECB)
22
OS_EventTaskRdy() - 6
OSTCBDly= 0

OSTCBEventPtr

OSTCBStat ≒ Rdy

1 1 Task Control Block


(TCB)

Event Control Block


(ECB)
23
OS_EventTaskRdy()
INT8U OS_EventTaskRdy (OS_EVENT *pevent, void *msg, INT8U msk) {
OS_TCB *ptcb;
INT8U x, y, bitx, bity, prio; Find highest priority task
waiting for message
y = OSUnMapTbl[pevent->OSEventGrp];
bity = OSMapTbl[y]; Remove this task from the
x = OSUnMapTbl[pevent->OSEventTbl[y]]; waiting list
bitx = OSMapTbl[x];
prio = (INT8U)((y << 3) + x);
if ((pevent->OSEventTbl[y] &= ~bitx) == 0x00) Prevent OSTimeTick() from
pevent->OSEventGrp &= ~bity; readying task
ptcb = OSTCBPrioTbl[prio];
ptcb->OSTCBDly = 0; Unlink ECB from this task
ptcb->OSTCBEventPtr = (OS_EVENT *)0;
ptcb->OSTCBMsg = msg; Clear bit associated with event
ptcb->OSTCBStat &= ~msk; type
if (ptcb->OSTCBStat == OS_STAT_RDY) {
OSRdyGrp |= bity; OSRdyTbl[y] |= bitx; Set the task ready to run if the
} task is not suspended
return (prio);
24
}
OS_EventTaskWait()
• This function is called by the PEND functions
for a semaphore, a mutex, a message mailbox
or a message queue when a task must wait on
an ECB.

• It removes the current task from the ready list


and places it in the wait list of the ECB.

25
OS_EventTaskWait() - 1
Waiting Queue Ready Queue

OSRdyGrp 1 1 1
EventGrp 1

1
EventTbl OSRdyTbl
1 1 1

1 1

26
OS_EventTaskWait() - 2
Waiting Queue Ready Queue

OSRdyGrp 1 1
EventGrp 1

EventTbl OSRdyTbl
1 1 1

1 1

27
OS_EventTaskWait() - 3
Waiting Queue Ready Queue

OSRdyGrp 1 1
EventGrp 1 1

1
EventTbl OSRdyTbl
1 1 1

1 1

28
OS_EventTaskWait()

void OS_EventTaskWait (OS_EVENT *pevent) Task no longer ready


{
OSTCBCur->OSTCBEventPtr = pevent;
if ((OSRdyTbl[OSTCBCur->OSTCBY] &= ~OSTCBCur->OSTCBBitX) == 0x00) {
OSRdyGrp &= ~OSTCBCur->OSTCBBitY;
}
pevent->OSEventTbl[OSTCBCur->OSTCBY] |= OSTCBCur->OSTCBBitX;
pevent->OSEventGrp |= OSTCBCur->OSTCBBitY;
}

Put task in waiting list

29
OS_EventTO()
• OS_EventTO = OS-Event-Time-Out

• This function is called by the PEND functions


for a semaphore, a mutex, a message mailbox
or a message queue when the ECB is not
signaled within the specified timeout period.

30
Usage: OS_EventTaskRdy()

Taski Taskj

xxx_post()
xxx_pend( 0 )
xxx_post()

xxx_pend( 0 )

31
Usage: OS_EventTO()
Taskj
Taski Taskj
Waiting queue

Ready queue
xxx_post()
xxx_pend( 3 )
xxx_post()

xxx_pend( 0 )

3
0
1
2
32
OS_EventTO()

void OS_EventTO (OS_EVENT *pevent)


{
if ((pevent->OSEventTbl[OSTCBCur->OSTCBY] &= ~OSTCBCur->OSTCBBitX) ==
0x00) {
pevent->OSEventGrp &= ~OSTCBCur->OSTCBBitY; Remove the task
} from the waiting list
OSTCBCur->OSTCBStat = OS_STAT_RDY; of the ECB
OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0;
}

33

You might also like