DOC PREVIEW
U of U ECE 3720 - Threads
Pages 5

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1'&$%ECE/CS 3720: Embedded System Design(ECE 6960/2 and CS 6968)Chris J. MyersLecture 11: ThreadsSlide 2'&$%Introduction to Threads• Interrupts create a multithreaded environment with asingle foreground thread (the main program), andmultiple background threads (the ISRs).• Projects where modules are loosely coupled, multipleforeground threads may be necessary.• The chapter presents techniques to implement multipleforeground threads (the scheduler).• It also presents synchronization tools, semaphores, thatallow threads to interact with each other.1Slide 3'&$%Thread MemorySlide 4'&$%Thread States2Slide 5'&$%Thread ListsSlide 6'&$%Round-Robin Scheduler3Slide 7'&$%Thread Control Block• A thread control block (TCB) stores information privateto each thread, and it must contain:1. A pointer so that it can be chained into a linked list.2. The value of its stack pointer.3. A stack area for local variables and saved registers.• A TCB may also contain:1. Thread number, type, or name.2. Age, or how long this thread has been active.3. Priority.4. Resources that this thread has been granted.Slide 8'&$%Thread Registers4Slide 9'&$%Assembly for the ThreadsProgA pshxtsxldd #5std 0,xLoopA ldaa #2staa PORTCldd 0,xjsr substd 0,xbra LoopAProgB pshxtsxldd #6std 0,xLoopB ldaa #4staa PORTCldd 0,xjsr substd 0,xbra LoopBSub pshxtsxstd 0,xldaa #1staa PORTCldd 0,xaddd #1pulxrtsSlide 10'&$%C for the Threadsint Sub(int j){ int i;PORTC=1; /* Port C=program being executed */i=j+1;return(i);}void ProgA(){ int i;i=5;while(1) { PORTC=2; i=Sub(i);}}void ProgB(){ int i;i=6;while(1) { PORTC=4; i=Sub(i);}}5Slide 11'&$%Thread Control Block in AssemblyTCB1 fdb TCB2fdb IS1fcb 1rmb 49IS1 rmb 1fcb $40fdb 0,0,0fdb ProgATCB2 fdb TCB3fdb IS2fcb 2rmb 49IS2 rmb 1fcb $40fdb 0,0,0fdb ProgATCB3 fdb TCB1 linkfdb IS3 SPfcb 4 Idrmb 49IS3 rmb 1fcb $40 CCRfdb 0,0,0 DXYfdb ProgB PCSlide 12'&$%Thread Control Block in Cstruct TCB{ struct TCB *Next; /* Link to Next TCB */unsigned char *SP; /* SP when not running */unsigned int Id; /* output to PortB */unsigned char MoreStack[49]; /* more stack */unsigned char CCR; /* Initial CCR */unsigned char RegB; /* Initial RegB */unsigned char RegA; /* Initial RegA */unsigned int RegX; /* Initial RegX */unsigned int RegY; /* Initial RegY */void (*PC)(void); /* Initial PC */};typedef struct TCB TCBType;typedef TCBType * TCBPtr;6Slide 13'&$%Thread Control Block in CTCBType sys[3]={{ &sys[1], /* Pointer to Next */&sys[0].MoreStack[49], /* Initial SP */1, { 0}, /* Id, clear stack */0x40,0,0,0,0, /* CCR,B,A,X,Y */ProgA, }, /* Initial PC */{ &sys[2], /* Pointer to Next */&sys[1].MoreStack[49], /* Initial SP */2, { 0}, /* Id, clear stack */0x40,0,0,0,0, /* CCR,B,A,X,Y */ProgA, }, /* Initial PC */{ &sys[0], /* Pointer to Next */&sys[2].MoreStack[49], /* Initial SP */4, { 0}, /* Id, clear stack */0x40,0,0,0,0, /* CCR,B,A,X,Y */ProgB, } }; /* Initial PC */Slide 14'&$%Preemptive Thread Scheduler in AssemblyNext equ 0 pointer to next TCBSP equ 2 Stack pointer for this threadId equ 4 Used to visualize thread runningRunPt rmb 2 pointer to thread runningMain ldaa #$FFstaa DDRC PortC displays program executingldx #TCB1 First thread to runjmp Start* Suspend thread which is currently runningOC5Han ldx RunPtsts SP,x save Stack Pointer in TCB7Slide 15'&$%Preemptive Thread Scheduler in Assembly (cont)* launch next threadldx Next,xStart stx RunPtldaa Id,xstaa PORTB visualizes running threadlds SP,x set SP for this new threadldd TOC5addd #20000 interrupts every 10 msstd TOC5ldaa #$08 ($20 on the 6812)staa TFLG1 acknowledge OC5rtiSlide 16'&$%Preemptive Thread Scheduler in CTCBPtr RunPt; /* Pointer to current thread */#pragma interrupt_handler ThreadSwitch()void ThreadSwitch(){asm(" ldx _RunPt\n"" sts 2,x");RunPt=RunPt->Next;PORTB=RunPt->Id; /* PortB=active thread */asm(" ldx _RunPt\n"" lds 2,x");TOC3=TCNT+20000; /* Thread runs for 10 ms */TFLG1=0x20; } /* ack by clearing TOC3F */8Slide 17'&$%Preemptive Thread Scheduler in C (cont)void main(void){ DDRC=0xFF;RunPt=&sys[0]; /* Specify first thread */asm(" sei");TOC3vector=&ThreadSwitch;TFLG1 = 0x20; /* Clear OC3F */TMSK1 = 0x20; /* Arm TOC3 */TOC3=TCNT+20000;PORTB=RunPt->Id;asm(" ldx _RunPt\n"" lds 2,x\n"" cli\n"" rti");} /* Launch First Thread */Slide 18'&$%Profile of Three Threads9Slide 19'&$%Other Scheduling Algorithms• A non-preemptive (cooperative) scheduler trusts eachthread to voluntarily release control on a periodic basis.• Not appropriate for real-time systems.• A priority scheduler assigns a priority to each thread.• A thread is scheduled only if no higher priority thread isready.• Priority reduces latency for important tasks.• In a busy system, low-priority threads may starve.Slide 20'&$%Dynamic Allocation of Threadsvoid create(void (*program)(void), int TheId){TCBPtr NewPt; // pointer to new TCBNewPt=(TCBPtr)malloc(sizeof(TCBType));if(NewPt==0)return;NewPt->SP=&(NewPt->CCR-1); /* SP when not running */NewPt->Id=TheId; /* used to visualize */NewPt->CCR=0x40; /* Initial CCR, I=0 */NewPt->RegB=0; /* Initial RegB */NewPt->RegA=0; /* Initial RegA */NewPt->RegX=0; /* Initial RegX */NewPt->RegY=0; /* Initial RegY */NewPt->PC=program; /* Initial PC */if(RunPt){NewPt->Next=RunPt->Next;RunPt->Next=NewPt;} /* will run Next */elseRunPt=NewPt;} /* first & only thread


View Full Document

U of U ECE 3720 - Threads

Course: Ece 3720-
Pages: 5
Download Threads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Threads 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?