comp.lang.ada
 help / color / mirror / Atom feed
From: Wilhelm.Spickermann@t-online.de (Wilhelm Spickermann)
Subject: Re: Q: Protected types and entries (long)
Date: 1999/02/16
Date: 1999-02-16T00:00:00+00:00	[thread overview]
Message-ID: <36C9C47D.F3461845@t-online.de> (raw)
In-Reply-To: 36C98180.98296205@siemens.at

Erik Margraf wrote:
> 
> Recently I started to learn about protected types. I tried to do
> something like the following:
> 
> (Not 100% Ada :-)
> 
> ...
>    type buffer is array (...) of character;
>    protected type Object is
>      entry put (data : in buffer);
>    private
>      internal_buffer : array (BIG_Enough) of character;
>      items_in_buffer : integer := 0;
>    end;
> ...
>    protected Object body
> 
>    entry put (data : in buffer)
>         when data'size + items_in_buffer <= internal_buffer�size is
>    begin
>     ...
>    end;
>    end Object;
> 
> Since the reference to a formal parameter of an entry is not allowed,
> gnat
> refused to compile this ;-). I changed the code to
> 
>    type buffer is array (...) of character;
>    protected type Object is
>      entry put (data : in buffer);
>    private
>      entry p_put (data : in buffer);
>      internal_buffer : array (BIG_Enough) of character;
>      data_size : integer;
>      items_in_buffer : integer := 0;
>    end;
> ...
>    protected Object body
>    entry put (data : in buffer)
>         when true is
>    begin
>       data_size := data�size;
>       if data_size + items_in_buffer > internal_buffer�size then
>         requeue p_put;
>       end if;
>    end;
> 
>    entry p_put (data : in buffer)
>         when data_size + items_in_buffer <= internal_buffer�size is
>    begin
>     ...
>    end;
>    end Object;
> 
> This compiles.
> Now my questions:
> 
>         - Can someone tell me WHY this limitation in the barrier exists?
>         - Is my "solution" really a solution to this problem?
>         - (What) Should I do something different?
> Thanks
> 
> Erik Margraf
> 
> --
> --------------------------------------------------------------------
> -- Erik Margraf
> -- Siemens Austria PSE KB2
> -- erik.margraf@siemens.at
> -- +43 1 1707 45887
> --------------------------------------------------------------------
I think the second solution will not work. Think of several tasks
waiting
for the barrier of p_put to become open. Then one task with a small
data'size enters put: It will change data_size and open the barrier for
all of them...
(The barrier does not belong to a call -- it belongs to the entry!)

The solution to your problem is decribed in Burns, Wellings: Concurrency 
in Ada (8.1.2).

The reason for the limitations on barriers is the high efficiency of 
implementation which was achieved by it. Protected objects are even more
efficient than semaphores, because one thread of control can execute the
protected operation of another and continue to work without any context
switch. 

Example: We have a one element buffer which is initially empty, a task R
         which is reading elements all day long and task W which is
writing
         all day:
 - task R tries to read and gets blocked
 - task W writes
 - task W executes the protected read of R and makes R executable
 - task W writes
 - task W tries to write the third element and gets blocked
 (cf. Ada 95 Rationale: 9.1.3)

Wilhelm Spickermann




  reply	other threads:[~1999-02-16  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-02-16  0:00 Q: Protected types and entries (long) Erik Margraf
1999-02-16  0:00 ` Wilhelm Spickermann [this message]
1999-02-19  0:00 ` Samuel Mize
1999-03-01  0:00 ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox