LECTURE 8 Introduction to Ada I 586.1 Continuation of tasking You should be working on Homework 6 and starting Homework 7 and 8. Only turn in one compilation listing for the combined 6,7 and 8. Real time demonstrations of your program running are a part of the assignment. Have the completed program ready so you just type $ RUN Control of tasks can be very complicated. The language provides several structures for various cases. The skeletons below may be used inside the task body after the task begin. For simplicity the "entry" and "accept" are shown without the optional parameters. package XXX is task YYY is -- or task type YYY is entry ZZZ ... ; end YYY ; end XXX ; package body XXX is task body YYY is -- optional declarations begin *** STRUCTURES BELOW GO HERE *** end YYY ; end XXX ; 1. A simple accept, one shot only then task terminates accept ZZZ do accept ZZZ ; -- statements -- statements end ZZZ ; -- statements 2. A simple accept, can be used many times, exists until sponsor dies loop accept ZZZ do -- statements end ZZZ ; -- statements end loop ; 3. A pair of entries that must go 1,2,1,2,1,2... loop accept ZZ1 do -- statements end ZZ1 ; -- statements accept ZZ2 do -- statements end ZZ2 ; -- statements end loop ; 4. A pair of entries that can be used in any order, may live forever loop select accept ZZ1 do -- statements end ZZ1 ; -- statements or accept ZZ2 do -- statements end ZZ2 ; -- statements end select ; end loop ; 5. A pair of entries that can be used in any order, the task XXX terminates when there is no possible callers for ZZ1 or ZZ2. loop select accept ZZ1 do -- statements end ZZ1 ; -- statements or accept ZZ2 do -- statements end ZZ2 ; -- statements or terminate ; end select ; end loop ; 6. A pair of entries that can be used in any order, each time through the loop, if neither ZZ1 nor ZZ2 has a caller waiting the statements in the "else" part are executed. Watch out! You have no control over the loop timing or the context switching algorithm. loop select accept ZZ1 do -- statements end ZZ1 ; -- statements or accept ZZ2 do -- statements end ZZ2 ; -- statements else -- statements end select ; end loop ; 7. A pair of entries that can be used in any order, If there are no callers for ZZ1 or ZZ2 control goes to some other task. After a delay of at least T seconds, this task comes around the loop again. loop select accept ZZ1 do -- statements end ZZ1 ; -- statements or accept ZZ2 do -- statements end ZZ2 ; -- statements or delay T ; end select ; end loop ; 8. Guards may be used inside the "select" structure on the "accept", "terminate" and "delay", but not on the "else". Just one of the cases from 5,6 or 7 may be used in any given structure. Guards are of the form : when BOOLEAN_EXPRESSION => If the BOOLEAN_EXPRESSION is true the normal action is taken. If false it is as though the structure following the guard did not exists. The three cases below use the "no rendezvous" form of the "accept" just for brevity. 8a select when BOOL1 => accept ZZ1 ; -- statements or when BOOL2 => accept ZZ2 ; -- statements or when BOOL3 => terminate ; end select ; 8b select when BOOL1 => accept ZZ1 ; -- statements or when BOOL2 => accept ZZ2 ; -- statements or when BOOL3 => delay T ; end select ; 8c no guard is allowed after "else" in select 9. To have a procedure PERIODIC called approximately every 1 second and desiring no overall slippage over long time intervals, the following task can be set up. Just for variety, the task is defined in a procedure rather than in a package. with CALENDAR ; use CALENDAR ; procedure MAIN is task GO_PERIOD ; -- task specification, short form task body GO_PERIOD is INTERVAL : constant DURATION := 1.0 ; -- seconds NEXT_TIME : TIME ; begin NEXT_TIME := CLOCK ; -- function call returns time now loop delay NEXT_TIME - CLOCK ; -- do this about once per second, good on average NEXT_TIME := NEXT_TIME + INTERVAL ; end loop ; end GO_PERIOD ; begin -- task started just before "do something" -- do something end MAIN ;