Code associated with "Multitasking Mysteries Revealed" Bill Lamie February 1997 Listing 1: Control loop method for an application performing high-priority serial input. main() { /*Look for high-priority serial input. */ poll_for_serial_input(); /*Some other program task Òa.Ó */ program_task_a(); /*Some other program task Òb.Ó */ program_task_b(); /*Some other program task Òc.Ó */ program_task_(c); } Listing 2: Context switching on a Motorola 68000 target _save_task_context: ; Assume that interrupt stack frame ; is present at the top of the stack movem.l d0-d7/a0-a6,-(a7) ; Save 68000 registers ori.w #$0700,sr ; Lockout interrupts movea.l _current_task,a0 ; Get current task pointer move.l a7,(a0) ; Save taskÕs stack pointer clr.l _current_task ; Clear current task pointer movea.l _system_stack,a7 ; Switch to the system stack jmp _scheduler ; Return to the scheduler ....... _scheduler: move.l _next_task,d0 ; Pickup the next task to execute beq _scheduler ; If nothing to do, just keep checking! jmp _restore_task_context; Otherwise, transfer control to task ....... _restore_task_context: ori.w #$0700,sr ; Lockout interrupts move.l d0,_current_task ; Setup current task pointer movea.l d0,a0 ; Prepare to use task pointer movea.l (a0),a7 ; Switch to taskÕs stack movem.l (a7)+,d0-d7/a0-a6 ; Recover taskÕs registers rte ; Recover taskÕs SR and return to it! Listing 3: Polling calls to check for the presence of high-priority main() { /*Look for high-priority serial input. */ poll_for_serial_input(); /*Some other program task Òa.Ó */ program_task_a(); /*Look for high-priority serial input again. */ poll_for_serial_input(); /*Some other program task Òb.Ó */ program_task_b(); /*Look for high-priority serial input. */ poll_for_serial_input(); /*Some other program task Òc.Ó */ program_task_c(); } Listing 4: code segments for tasks_1, 2, and 3. task_a_b_and_c() { /*Loop forever. */ while(1) { /*Wait for message from queue. */ my_queue_pend(); /*Send message to next taskÕs queue. */ send_to_next_task(); } }