开发者

Ada entry and when statement usage

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.


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;


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜