Docsity

Lecture Slides on Threads - Embedded System Design | ECE 6960, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
Slide 1
'
&
$
%
ECE/CS 3720: Embedded System Design
(ECE 6960/2 and CS 6968)
Chris J. Myers
Lecture 11: Threads
Slide 2
'
&
$
%
Introduction to Threads
•Interrupts create a multithreaded environment with a
single foreground thread (the main program), and
multiple background threads (the ISRs).
•Projects where modules are loosely coupled, multiple
foreground threads may be necessary.
•The chapter presents techniques to implement multiple
foreground threads (the scheduler).
•It also presents synchronization tools, semaphores, that
allow threads to interact with each other.
1
Slide 3
'
&
$
%
Thread Memory
Slide 4
'
&
$
%
Thread States
2
pf3
pf4
pf5

Partial preview of the text

Download Lecture Slides on Threads - Embedded System Design | ECE 6960 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

& Slide 1 '

& Slide 2 ' Lecture 11: Threads Chris J. Myers(ECE 6960/2 and CS 6968) ECE/CS 3720: Embedded System Design

• It also presents synchronization tools, semaphoresforeground threads (the scheduler ). • The chapter presents techniques to implement multipleforeground threads may be necessary. • Projects where modules are loosely coupled, multiplemultiple background threads (the ISRs).single foreground thread (the main program), and • Interrupts create a multithreaded environment with a Introduction to Threads

, that

1 allow threads to interact with each other.

&^ Slide 3 '

Thread Memory

&^ Slide 4 '

2 Thread States

& Slide 5 '

Thread Lists

&^ Slide 6 '

3 Round-Robin Scheduler

&^ Slide 7 '

&^ Slide 8 ' 4. Resources that this thread has been granted.3. Priority.2. Age, or how long this thread has been active.1. Thread number, type, or name. • A TCB may also contain:3. A stack area for local variables and saved registers.2. The value of its stack pointer.1. A pointer so that it can be chained into a linked list.to each thread, and it must contain: • A thread control block (TCB) stores information private Thread Control Block

4 Thread Registers

& Slide 13 ' % $

&^ Slide 14 ' ProgB, } }; /* Initial PC /0x40,0,0,0,0, / CCR,B,A,X,Y /4, { 0}, / Id, clear stack /&sys[2].MoreStack[49], / Initial SP /{ &sys[0], / Pointer to Next /ProgA, }, / Initial PC /0x40,0,0,0,0, / CCR,B,A,X,Y /2, { 0}, / Id, clear stack /&sys[1].MoreStack[49], / Initial SP /{ &sys[2], / Pointer to Next /ProgA, }, / Initial PC /0x40,0,0,0,0, / CCR,B,A,X,Y /1, { 0}, / Id, clear stack /&sys[0].MoreStack[49], / Initial SP /{ &sys[1], / Pointer to Next */ TCBType sys[3]={ Thread Control Block in C

7 sts^ SP,x^ save Stack Pointer in TCBOC5Han ldx^ RunPt* Suspend thread which is currently runningjmp^ Startldx^ #TCB1^ First thread to runstaa DDRC^ PortC displays program executingMain^ ldaa #$FFRunPt rmb^2 pointer to thread runningId^ equ^4 Used to visualize thread runningSP^ equ^2 Stack pointer for this thread^ Next^ equ^0 pointer to next TCB^ Preemptive Thread Scheduler in Assembly

&^ Slide 15 '

&^ Slide 16 ' rtistaa TFLG1 acknowledge OC5ldaa #$08 ($20 on the 6812)std TOC5addd #20000 interrupts every 10 msldd TOC5lds SP,x set SP for this new threadstaa PORTB visualizes running threadldaa Id,xStart stx RunPtldx Next,x * launch next thread Preemptive Thread Scheduler in Assembly (cont)

TCBPtr RunPt; /* Pointer to current thread Preemptive Thread Scheduler in C

8 TFLG1=0x20; }^ /* ack by clearing TOC3F /TOC3=TCNT+20000; / Thread runs for 10 ms /" lds 2,x");asm(" ldx _RunPt\n"PORTB=RunPt->Id; / PortB=active thread */RunPt=RunPt->Next;" sts 2,x");asm(" ldx _RunPt\n"void ThreadSwitch(){#pragma interrupt_handler ThreadSwitch()

& Slide 17 '

&^ Slide 18 ' " rti");} /* Launch First Thread /" cli\n"" lds 2,x\n"asm(" ldx _RunPt\n"PORTB=RunPt->Id;TOC3=TCNT+20000;TMSK1 = 0x20; / Arm TOC3 /TFLG1 = 0x20; / Clear OC3F /TOC3vector=&ThreadSwitch;asm(" sei");RunPt=&sys[0]; / Specify first thread */ void main(void){ DDRC=0xFF; Preemptive Thread Scheduler in C (cont)

9 Profile of Three Threads

&^ Slide 19 '

  • (^) In a busy system, low-priority threads may • (^) Priority reduces latency for important tasks.ready. • (^) A thread is scheduled only if no higher priority thread is • (^) A (^) priority scheduler (^) assigns a priority to each thread. • (^) Not appropriate for real-time systems.thread to voluntarily release control on a periodic basis. • (^) A (^) non-preemptive (cooperative) scheduler (^) trusts each Other Scheduling Algorithms

starve.

&^ Slide 20 '

NewPt->SP=&(NewPt->CCR-1); /* SP when not runningif(NewPt==0)return;NewPt=(TCBPtr)malloc(sizeof(TCBType));TCBPtr NewPt; // pointer to new TCB^ void create(void (*program)(void), int TheId){ Dynamic Allocation of Threads

10 RunPt=NewPt;}^ /* first & only thread /elseRunPt->Next=NewPt;}^ / will run Next /NewPt->Next=RunPt->Next;if(RunPt){NewPt->PC=program;^ / Initial PC /NewPt->RegY=0;^ / Initial RegY /NewPt->RegX=0;^ / Initial RegX /NewPt->RegA=0;^ / Initial RegA /NewPt->RegB=0;^ / Initial RegB /NewPt->CCR=0x40;^ / Initial CCR, I=0 /NewPt->Id=TheId;^ / used to visualize */