ADA95 – part III Real-Time Systems Lecture 3 Copyright, 2002 © Adam Czajka
Contents Tasks Protected objects
Copyright, 2002 © Adam Czajka Tasks task type task_id [(quantifier)] is [entry id (parameters);] [private [entry id (parameters);]] end [task_id];
Copyright, 2002 © Adam Czajka Tasks task body task_id is declarations..... begin... instructions... [accept id (parameters);]... instructions... end [task_id];
Copyright, 2002 © Adam Czajka Tasks task type TP1(P : integer) is entry E1(x: integer); entry E2(); end TP1; task type TP2(P : integer) is entry E1(); end TP2;
Copyright, 2002 © Adam Czajka Tasks T1 : TP1(3); T2 : TP2; T3 : array (1..5) of T2; type p_T1 is access T1; type p_T2 is access T2; p1 : p_T1 := new T1(3); p2 : p_T2 := new T2;
Copyright, 2002 © Adam Czajka Tasks Communication is synchronous Communication is synchronous Communication is asymmetric (client server) Communication is asymmetric (client server) task_name.entry proc_name (parameters); Communication
Copyright, 2002 © Adam Czajka Tasks Communication task type T is entry E1(x : in Integer; y : out Integer); end T; task body T is... accept E1(x : in Integer; y : out Integer) do... end E1;... end T; a : Integer; T.E1(5, a); T.E1(5, a);
Copyright, 2002 © Adam Czajka Tasks Priorities task type task_id (p : Any_Priority) is pragma Priority(p);... end task_id; T1 : task_id(10);
Copyright, 2002 © Adam Czajka Tasks Interaction : time server client1 client2 accept E1 server.E1(); in,inout end; out,inout
Copyright, 2002 © Adam Czajka Tasks Interaction : time server client1 client2 accept E1 server.E1(); in,inout end; out,inout
Copyright, 2002 © Adam Czajka Tasks Producer-Consumer : task Buffer is entry Get(X : out Integer); entry Put(X : in Integer); end Buffer; task type Producer; task type Consumer;
Copyright, 2002 © Adam Czajka Tasks Producer-Consumer : task body Buffer is Tmp : Integer; begin loop accept Get(X : out Integer) do X := Tmp; end; accept Put(X : in Integer) do Tmp := X; end; end loop; end Buffer; task body Consumer is begin Buffer.Get(Elem);.... end Producer; task body Producer is begin.... Buffer.Put(Elem); end Producer;
Copyright, 2002 © Adam Czajka Tasks Selective waiting : select accept E1(parameters) do.... end E1; or accept E2(parameters) do.... end E2; end select;
Copyright, 2002 © Adam Czajka Tasks Selective waiting – example : task Server is entry E1(...); entry E2(...);.... entry E i (...); end Server;
Copyright, 2002 © Adam Czajka Tasks task body Server is begin loop select accept E1(...) do..... end E1; or accept E2(...) do..... end E2;..... end select; end loop; end Server; Selective waiting – example :
Copyright, 2002 © Adam Czajka Tasks Conditional accept : when condition => accept E1(...) do..... end E1; task type Buffer(Size: Integer) is entry Get(X : out Integer); entry Put(X : in Integer); end Buffer;
Copyright, 2002 © Adam Czajka Tasks Conditional accept : task body Buffer(Size : Integer) is counter : Integer; tmp : array (1..Size) of Integer; begin loop select when counter > 0 => accept Get(X : out Integer)do X := tmp(counter); counter := counter – 1; end Get;....
Copyright, 2002 © Adam Czajka Tasks Conditional accept :.... or when counter accept Put(X : in Integer)do counter := counter + 1; tmp(counter) := X; end Put; end select; end loop; end Buffer; buf : Buffer(10); El : Integer; Buffer.Put(5);Buffer.Get(El);
Copyright, 2002 © Adam Czajka Tasks Wait instructions : delay time; delay until time; loop delay end loop; Problem !!!
Copyright, 2002 © Adam Czajka Tasks Wait instructions : next_time := tm_Start; loop delay next_time;.... next_time := next_time + tm_Slice; end loop; OK.
Copyright, 2002 © Adam Czajka Tasks Wait instructions : declare use Ada.Calendar; period : constant duration := 3.0; next_time : Time := starting_time; begin loop delay until next_time;.... next_time := next_time + period; end loop; end;
Copyright, 2002 © Adam Czajka Tasks Wait instructions : main_loop: loop..... select accept E1(...) do..... end; or delay wait_time;..... exit main_loop; end select; end loop main_loop;
Copyright, 2002 © Adam Czajka Tasks Building a server : server_loop: loop..... select accept E1(...) do..... end; else null; end select;... delay timeSlice; end loop server_loop;
Copyright, 2002 © Adam Czajka Tasks Finishing a task : task Server is entry E1(...);..... entry Finish; end Server;
Copyright, 2002 © Adam Czajka Tasks Finishing a task : task body Server is begin loop select accept E1(...) do..... end; or..... or accept Finish do exit; end select; end loop; end Server;
Copyright, 2002 © Adam Czajka Tasks Finishing a task : task body Server is begin loop select accept E1(...) do..... end; or..... or terminate; end select; end loop; end Server;
Copyright, 2002 © Adam Czajka Tasks Conditional communication with Server: select Server.E1(...);..... or delay wait_time;..... end select;
Copyright, 2002 © Adam Czajka protected type obj_name [ (quantifier) ] is entry E1(...); procedure P1(...); function F1(...);..... [private declarations_of_shared_variables procedure P2(...); ] end obj_name; Protected objects
Copyright, 2002 © Adam Czajka Protected objects protected body obj_name is..... entry E1(...) when Condition is begin..... end E1;..... end obj_name; Only Entry subroutines can have barriers, i.e. when statements.
Copyright, 2002 © Adam Czajka Protected objects Locks : ActionsLock type procedure, entry modificationsRead/Write functionreadingRead
Copyright, 2002 © Adam Czajka Protected objects Locks : State Caller ReadRead/Write EntryNo ProcedureNo FunctionYesNo
Copyright, 2002 © Adam Czajka Protected objects protected type Buffer(Size : Integer := 100) is entry Put(X : in Integer); entry Get(X : out Integer); private buf : array(1..N) of Integer counter : Integer range 0..N := 0; end Buffer; Example :
Copyright, 2002 © Adam Czajka Protected objects protected body Buffer is entry Put(X : in Integer) when counter < N is begin counter := counter + 1; buf(counter) := X; end Put; entry Get(X : out Integer) when counter > 0 is begin X := buf(counter); counter := counter – 1; end Get; end Buffer; Example :
Copyright, 2002 © Adam Czajka Protected objects buf1 : Buffer(200); El : Integer; buf1.Put(100); buf1.Get(El); Example :
Copyright, 2002 © Adam Czajka Summary Tasks Protected objects