comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Advice, tasking and hardware
Date: Sat, 28 May 2016 07:38:41 -0700 (PDT)
Date: 2016-05-28T07:38:41-07:00	[thread overview]
Message-ID: <8a5c6387-b476-4332-b0c9-611cbecd64b3@googlegroups.com> (raw)
In-Reply-To: <niaaji$1nar$1@gioia.aioe.org>

On Friday, May 27, 2016 at 2:27:37 PM UTC-6, Dmitry A. Kazakov wrote:
> On 2016-05-27 21:13, Shark8 wrote:
> > On Friday, May 27, 2016 at 1:50:50 AM UTC-6, Dmitry A. Kazakov wrote:
> >> 6. Task entries cannot return unconstrained objects.
> >
> > This can be worked around:
> >    We can add to the previous task the following:
> > 	    Entry Data( Item : out Natural );
> > 	    Entry Data( Item : out String );
> 
> This is a very low-level and very fragile design. Consider ensuring that 
> nothing comes between querying the length and the body that could change 
> the string.

That's actually easy to do with Ada's tasking:
		select
		    accept Done do
			Finished:= True;
		    end Done;
		or
		    accept Get (Data : in String) do
			Internal_Data:= String_Holder.To_Holder( Data );
		    end Get;
		or
		    accept Put do
			Ada.Text_IO.Put_Line( "DATA: "& Internal_Data.Element );
		    end Put;
		or
		    accept Data (Item : out Natural) do
			Item:= Internal_Data.Element'Length;
		    end Data;
		    accept Data (Item : out String) do
			Item:= Internal_Data.Element;
		    end Data;
		    
		end select;

> You start doing that with entry barriers risking running 
> into a deadlock.

As you can see, no barrier needed.
This is one of the nice things about Ada's tasking: you can directly encode a protocol. -- And with package specifications, you can put the task in the private part and declare the public interface in the public portion as [inline]subprograms which, in the body do the proper entry-calls.

package example is
  Function Get_String return String with Inline;
  -- ...
  
private
  Task Text_IO is
  --...
end example;

package body example is
  Function Get_String return String is
    Length : Natural;
  begin
    Text_IO.Data( Length );
    Return Result : String(1..Length) do
      Text_IO.Data( Result );
    End Return;
  end Get_String;
  --...
end example;

> Then consider a possibility that the caller of a 
> get-length request dies prematurely, or that another task steals the 
> string body and so on.

Again, precluded by the construction of the select statement shown above.
Once the task accepts a get-length it *MUST* next accept a get-data, so if multiple threads call the get-data then only one gets processed and that one is the only one that can then be accepted for a get-data because all the rest are waiting on get-length to be serviced.

> Note that the "discussion" started around the claim that design based on 
> semaphore is more low-level than one based on monitor (the task serves 
> as a monitor).
> (There was a reason why protected objects were introduced in Ada 95)

And tasking shows itself to be a higher-level construct; sure, protected objects have their place, but they *AREN'T* capable of directly mapping to a protocol w/o forcing the manual creation of barriers.
 
> P.S. The Ada-way of returning string is this:
> 
>     entry Get_Text (Text : in out String; Last : out Integer);

I would argue that the Ada way would be creating a function that returns the string of the proper length in the public part of the spec and keeping the implementation hidden in the body.


  parent reply	other threads:[~2016-05-28 14:38 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-25 21:24 Advice, tasking and hardware patrick
2016-05-26  1:09 ` Jeffrey R. Carter
2016-05-26  8:13   ` Simon Wright
2016-05-26  7:26 ` Dmitry A. Kazakov
2016-05-26 16:41   ` patrick
2016-05-26 17:56     ` Dmitry A. Kazakov
2016-05-26 20:35     ` Jeffrey R. Carter
2016-05-26 19:35   ` Jeffrey R. Carter
2016-05-26 20:51     ` patrick
2016-05-27  7:50     ` Dmitry A. Kazakov
2016-05-27 18:00       ` Simon Wright
2016-05-27 19:06       ` Jeffrey R. Carter
2016-05-27 22:05         ` Randy Brukardt
2016-05-27 23:09           ` Jeffrey R. Carter
2016-05-27 19:13       ` Shark8
2016-05-27 20:27         ` Dmitry A. Kazakov
2016-05-27 22:27           ` Randy Brukardt
2016-05-28  6:49             ` Dmitry A. Kazakov
2016-05-28 14:38           ` Shark8 [this message]
2016-05-28 15:45             ` Dmitry A. Kazakov
2016-05-28  0:25 ` rieachus
2016-05-28  1:57   ` patrick
2016-05-28  4:13   ` Jeffrey R. Carter
2016-06-01 14:37     ` rieachus
2016-06-01 19:09       ` Dmitry A. Kazakov
2016-06-06  3:33         ` rieachus
2016-06-06  7:18           ` Dmitry A. Kazakov
2016-06-07 16:53             ` rieachus
2016-06-07 20:21               ` Dmitry A. Kazakov
2016-06-08  4:06                 ` rieachus
2016-06-08  7:29                   ` Dmitry A. Kazakov
2016-06-08 12:56                     ` rieachus
2016-06-08  0:19               ` Dennis Lee Bieber
replies disabled

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