Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ada entry and when statement usage

Tags:

ada

I am a newbie in Ada programming language and I am working on concurrent programming, but I am having a problem with one implementation. This might be very dummy question. The code is:

type status is array(1..6) of boolean; --boolean values for each track
track_available :status:=(others=>true); --true if track is available
protected track_handler is

entry track_req(n:in track_part_type); --n is track number
entry track_rel(n:in track_part_type); --n is track number

end track_handler;


protected body track_handler is
--implement entries
entry track_req(n: in track_part_type) when track_available(n) is --here where the error occurs
    begin
        req(n);
    end track_req;

entry track_rel(n: in track_part_type) when track_available(n) is
    begin
        rel(n);
    end track_rel;
end track_handler;

    procedure req(nr : track_part_type) is
    begin
        --null;
        track_available(nr):=false;
    end req;

    procedure rel(nr : track_part_type) is
    begin
        --null;
        track_available(nr):=true;
    end rel;

Here I get a compilation error for "when track_available(n)" statement saying that "n is undefined". I think variable n is out of scope, but I also need to check if the n'th index of the array is true or false. How can I overcome this problem?

Thank you.

like image 350
Ahmet Keskin Avatar asked Jan 31 '11 13:01

Ahmet Keskin


People also ask

What is entry in Ada?

An entry looks much like a procedure. It has an identifier and may have in, out or in out parameters. Ada supports communication from task to task by means of the entry call. Information passes between tasks through the actual parameters of the entry call.

Where is Ada used?

Examples of systems where Ada is used include avionics, air traffic control, railways, banking, military and space technology. Ada's dynamic memory management is high-level and type-safe. Ada has no generic or untyped pointers; nor does it implicitly declare any pointer type.

How do you write a for loop in Ada?

In Ada, loops always start with the loop reserved word and end with end loop . To leave the loop, use exit — the C++ and Java equivalent being break . This statement can specify a terminating condition using the exit when syntax. The loop opening the block can be preceded by a while or a for .

How do you write Hello World in Ada?

Put_Line ("Hello, World!" Put_Line ("Hello, World!"


2 Answers

You can't actually use an entry's parameters in its own guard. You got that much, I gather.

The way guards work, all of them are evaluated before the wait starts, and only the ones that are active at that time will be available. They don't get periodicly re-evaluated or dynamicaly read or anything.

This means it will be tough to get the logic for your guards right, unless you write your code so that only other entries in your protected object modify the guards. If you want to use some data from outside of your protected object to control its behavior, you will probably need to use some mechanisim other than guards to do it. Like check just inside the entry and exit immediately or something.

There is one possibility for what you are trying to do though: Entry families. You should be able to use an entry family index in a guard.

The spec would change to:

entry track_req(track_part_type); 
entry track_rel(track_part_type); 

And the body would change to

entry track_req(for n in track_part_type) when track_available(n) is 
    begin
        req(n);
    end track_req;

entry track_rel(for n in track_part_type) when track_available(n) is
    begin
        rel(n);
    end track_rel;
end track_handler;
like image 113
T.E.D. Avatar answered Oct 05 '22 03:10

T.E.D.


In the code below you are trying to use track_available(n), before it has been fully defined by (n: in track_part_type).

entry track_req(n: in track_part_type) when track_available(n) is 

See also http://en.wikibooks.org/wiki/Ada_Programming/Tasking#Protected_types

NWS

like image 38
NWS Avatar answered Oct 05 '22 04:10

NWS