ADA95 – part III Adam.Czajka@cs.put.poznan.pl Real-Time Systems Lecture 3 Copyright, 2002 © Adam...

Post on 18-Jan-2016

214 views 0 download

Tags:

transcript

ADA95 – part III ADA95 – part III

Adam.Czajka@cs.put.poznan.pl

Real-Time SystemsReal-Time Systems

Lecture 3Lecture 3

Copyright, 2002 © Adam Czajka

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 synchronousCommunication is synchronous

Communication is asymmetric Communication is asymmetric

(client (client server) server)

task_name.entry proc_name (parameters);

Communication

Copyright, 2002 © Adam Czajka

TasksCommunication

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;a : Integer; T.E1(5, a);T.E1(5, a);

Copyright, 2002 © Adam Czajka

TasksPriorities

task type task_id (p : Any_Priority) is pragma Priority(p); ... end task_id;   T1 : task_id(10);

Copyright, 2002 © Adam Czajka

TasksInteraction :

time server client1 client2

accept E1

server.E1(); in,inout

end;out,inout

Copyright, 2002 © Adam Czajka

TasksInteraction :

time server client1 client2

accept E1

server.E1();

in,inout

end;out,inout

Copyright, 2002 © Adam Czajka

TasksProducer-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

TasksProducer-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 isbegin Buffer.Get(Elem); ....end Producer;

task body Producer isbegin .... Buffer.Put(Elem);end Producer;

Copyright, 2002 © Adam Czajka

TasksSelective waiting :

select accept E1(parameters) do .... end E1; or accept E2(parameters) do .... end E2;end select;

Copyright, 2002 © Adam Czajka

TasksSelective waiting – example :

task Server is entry E1(...); entry E2(...); .... entry Ei(...);end Server;

Copyright, 2002 © Adam Czajka

Tasks

task body Server isbegin 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

TasksConditional 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

TasksConditional 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

TasksConditional accept :

.... or when counter < Size => accept Put(X : in Integer)do counter := counter + 1; tmp(counter) := X; end Put; end select; end loop;end Buffer;

buf : Buffer(10);buf : Buffer(10);El : Integer;El : Integer;  Buffer.Put(5);Buffer.Put(5);Buffer.Get(El);Buffer.Get(El);

Copyright, 2002 © Adam Czajka

TasksWait instructions :

delay time; delay until time;

loop

delay 3.0

....

end loop;

Problem !!!Problem !!!

Copyright, 2002 © Adam Czajka

TasksWait instructions :

next_time := tm_Start;

loop

delay next_time;

....

next_time := next_time + tm_Slice;

end loop;

OK.OK.

Copyright, 2002 © Adam Czajka

TasksWait 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

TasksWait 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

TasksBuilding a server :

server_loop:loop ..... select accept E1(...) do ..... end; else null; end select;...delay timeSlice;end loop server_loop;

Copyright, 2002 © Adam Czajka

TasksFinishing a task :

task Server is entry E1(...); ..... entry Finish;end Server;

Copyright, 2002 © Adam Czajka

TasksFinishing a task :task body Server isbegin loop select accept E1(...) do ..... end; or ..... or accept Finish do exit; end select; end loop;end Server;

Copyright, 2002 © Adam Czajka

TasksFinishing a task :task body Server isbegin loop select accept E1(...) do ..... end; or ..... or terminate;

end select; end loop;end Server;

Copyright, 2002 © Adam Czajka

TasksConditional 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 Only EntryEntry subroutines can have barriers, i.e. subroutines can have barriers, i.e.

whenwhen statements. statements.

Copyright, 2002 © Adam Czajka

Protected objectsLocks :

 

Actions Lock type

procedure, entry

modifications Read/Write

function reading Read 

Copyright, 2002 © Adam Czajka

Protected objectsLocks :

 

 

State

Caller

Read Read/Write

Entry No No

Procedure No No

Function Yes No

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

SummarySummary

Tasks

Protected objects