Safe Queue?
description
Transcript of Safe Queue?
![Page 1: Safe Queue?](https://reader036.fdocuments.us/reader036/viewer/2022082819/56813c8e550346895da63757/html5/thumbnails/1.jpg)
Safe Queue?…while (next(head) == tail); // block if fullqueue[head] = data; // write dataET0 = 0; // disable T-0 interruptshead = next(head); // update pointerET1 = 1; // enable T-1 interrupts…unsigned char next(unsigned char ptr) {
if (ptr == QMX) ptr = 0;else ptr++return(ptr);
}
![Page 2: Safe Queue?](https://reader036.fdocuments.us/reader036/viewer/2022082819/56813c8e550346895da63757/html5/thumbnails/2.jpg)
Consider the compilerC code for blocking queue…while (next(head) == tail);queue[head] = data;ET0 = 0; head = next(head); ET1 = 1; …unsigned char next(unsigned char ptr) {
if (ptr == 9) ptr = 0;else ptr++return(ptr);
}
Compiler output?MOV R0,TAILMOV R1,HEAD
LOOP: ACALL NEXT SUBB A,R0
JZ LOOPMOV R3,#QUEUEADD R3,R1MOV @R3,R4CLR ET0ACALL NEXTMOV HEAD,ASETB ET0
Assume next() returns value in accumulator, leaves R1 as is
![Page 3: Safe Queue?](https://reader036.fdocuments.us/reader036/viewer/2022082819/56813c8e550346895da63757/html5/thumbnails/3.jpg)
I think we’re safe nowvolatile data unsigned char tail;…while (next(head) == tail);queue[head] = data;ET0 = 0; head = next(head); ET1 = 1; …unsigned char next(unsigned char ptr) {
if (ptr == 9) ptr = 0;else ptr++return(ptr);
}
MOV R1,HEADLOOP: ACALL NEXT
MOV R0,TAIL ;refreshSUBB A,R0JZ LOOPMOV R3,#QUEUEADD R3,R1MOV @R3,R4CLR ET0ACALL NEXTMOV HEAD,ASETB ET0
*compiler knows that sfr’s are volatile (ports, flags)
![Page 4: Safe Queue?](https://reader036.fdocuments.us/reader036/viewer/2022082819/56813c8e550346895da63757/html5/thumbnails/4.jpg)
Embedded Software Architectures
• No Operating System– Round robin: sequential polling for events– Round robin w/ interrupts– Function Queue Scheduling
• Real Time Operating System
![Page 5: Safe Queue?](https://reader036.fdocuments.us/reader036/viewer/2022082819/56813c8e550346895da63757/html5/thumbnails/5.jpg)
Maestro w/ Round Robin Only
void main (void) {if (TF0) music_task(); // process timer overflowif (R1) serial_input_task(); // process serial input
}
Would this work for maestro (lab4)?
How do we know?
![Page 6: Safe Queue?](https://reader036.fdocuments.us/reader036/viewer/2022082819/56813c8e550346895da63757/html5/thumbnails/6.jpg)
Task DiagramWorst case: character comes one cycle before TF0Worst Case Latency = max run time of all other tasks
What is the worstcase serial processingtime? Depends on response message length--could be bad!How much latency can wetolerate? Practically none
main
music
serial
char arrivestimer0 overflow occurs (TF0)
serial task main
w.c. latency deadline
![Page 7: Safe Queue?](https://reader036.fdocuments.us/reader036/viewer/2022082819/56813c8e550346895da63757/html5/thumbnails/7.jpg)
Round Robin w/ Interruptsvolatile bit fEvent;void isr(void) {
time_critical_processing();fEvent = TRUE;
}void main (void) {
if (R1) serial_input_task();if (fEvent) {
housekeeping();fEvent = FALSE;
}}
Why not put housekeeping into the ISR too?Then our worst case latency to a separate time critical event would be poor
Would this work for maestro? See next slide
![Page 8: Safe Queue?](https://reader036.fdocuments.us/reader036/viewer/2022082819/56813c8e550346895da63757/html5/thumbnails/8.jpg)
Maestro in RR+INTvolatile bit fEndOfSlice; void isr(void) {
process_tones();if (!--sliceCount) {
changeTones();sliceCount = SliceSize fEndOfSlice = TRUE;
}}
main () {if (R1) serial_input_task();if (fEndOfSlice) {
if (--TNE==0) process_next_event();
fEndOfSlice = FALSE;}
}
What are the time critical functions?Flipping channels, change tones at end of timeslice
What are the housekeeping functions? TNE count down and stream processing (TNE, Tones)
Do these have hard time constraints too?Yes, one time slice (18ms)…good enough? Not necessarily…serial is undefined!
![Page 9: Safe Queue?](https://reader036.fdocuments.us/reader036/viewer/2022082819/56813c8e550346895da63757/html5/thumbnails/9.jpg)
Task DiagramWorst case analysis: character comes one cycle before TF0Problem: housekeeping can still miss its deadline, depends on serial task execution time
main
housekeeping
serial
char arrivestimer0 overflow occurs (TF0)
Can we do better?
isrtime slice start time slice end
deadline
![Page 10: Safe Queue?](https://reader036.fdocuments.us/reader036/viewer/2022082819/56813c8e550346895da63757/html5/thumbnails/10.jpg)
Benefits
• Modularization of the Application
• Some Virtual Machine Features– Device Drivers (Virtual Devices)
![Page 11: Safe Queue?](https://reader036.fdocuments.us/reader036/viewer/2022082819/56813c8e550346895da63757/html5/thumbnails/11.jpg)
Embedded Software
• Software States v. Finite State Machines
• Hierarchical State
• Thread/Process Communication– Critical Sections– Synchronization– Messaging and Signaling