Download presentation
Presentation is loading. Please wait.
Published bySylvia Sullivan Modified over 9 years ago
1
ADA95 – part III Adam.Czajka@cs.put.poznan.pl Real-Time Systems Lecture 3 Copyright, 2002 © Adam Czajka
2
Contents Tasks Protected objects
3
Copyright, 2002 © Adam Czajka Tasks task type task_id [(quantifier)] is [entry id (parameters);] [private [entry id (parameters);]] end [task_id];
4
Copyright, 2002 © Adam Czajka Tasks task body task_id is declarations..... begin... instructions... [accept id (parameters);]... instructions... end [task_id];
5
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;
6
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;
7
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
8
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);
9
Copyright, 2002 © Adam Czajka Tasks Priorities task type task_id (p : Any_Priority) is pragma Priority(p);... end task_id; T1 : task_id(10);
10
Copyright, 2002 © Adam Czajka Tasks Interaction : time server client1 client2 accept E1 server.E1(); in,inout end; out,inout
11
Copyright, 2002 © Adam Czajka Tasks Interaction : time server client1 client2 accept E1 server.E1(); in,inout end; out,inout
12
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;
13
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;
14
Copyright, 2002 © Adam Czajka Tasks Selective waiting : select accept E1(parameters) do.... end E1; or accept E2(parameters) do.... end E2; end select;
15
Copyright, 2002 © Adam Czajka Tasks Selective waiting – example : task Server is entry E1(...); entry E2(...);.... entry E i (...); end Server;
16
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 :
17
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;
18
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;....
19
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);
20
Copyright, 2002 © Adam Czajka Tasks Wait instructions : delay time; delay until time; loop delay 3.0.... end loop; Problem !!!
21
Copyright, 2002 © Adam Czajka Tasks Wait instructions : next_time := tm_Start; loop delay next_time;.... next_time := next_time + tm_Slice; end loop; OK.
22
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;
23
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;
24
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;
25
Copyright, 2002 © Adam Czajka Tasks Finishing a task : task Server is entry E1(...);..... entry Finish; end Server;
26
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;
27
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;
28
Copyright, 2002 © Adam Czajka Tasks Conditional communication with Server: select Server.E1(...);..... or delay wait_time;..... end select;
29
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
30
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.
31
Copyright, 2002 © Adam Czajka Protected objects Locks : ActionsLock type procedure, entry modificationsRead/Write functionreadingRead
32
Copyright, 2002 © Adam Czajka Protected objects Locks : State Caller ReadRead/Write EntryNo ProcedureNo FunctionYesNo
33
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 :
34
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 :
35
Copyright, 2002 © Adam Czajka Protected objects buf1 : Buffer(200); El : Integer; buf1.Put(100); buf1.Get(El); Example :
36
Copyright, 2002 © Adam Czajka Summary Tasks Protected objects
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.