comp.lang.ada
 help / color / mirror / Atom feed
* task model
@ 2009-10-15 17:43 Mike
  2009-10-15 18:00 ` Ludovic Brenta
  0 siblings, 1 reply; 13+ messages in thread
From: Mike @ 2009-10-15 17:43 UTC (permalink / raw)


I'm prototyping some tasking with multiple accepts, the code is as
follows:

with Ada.Text_Io;
use Ada.Text_Io;

procedure task_test is


   procedure Process_Primary(node: in Integer) is

   begin

       Put_Line("This is number" & Integer'Image(node));

   end Process_Primary;


   task proc1 is
     entry process1(node : Integer);
     entry continue;
   end proc1;

   task body proc1 is
   begin

     accept process1( node : Integer) do

       Process_Primary( node );

     end;

     accept continue do
       Put_Line("Called Continue task1");
     end;

   end proc1;

   task proc2 is
     entry process2(node : Integer);
     entry continue;
   end proc2;

   task body proc2 is
   begin

     accept process2( node : Integer) do

         Process_Primary( node );

     end;

     accept continue do
       Put_Line("Called Continue task2");
     end;

   end proc2;


begin
  for i in 1..2 loop
   proc1.process1(1);
   proc2.process2(2);
   proc1.continue;
   proc2.continue;
  end loop;
end task_test;
---------------------------------------------------------------------------------------------------------------------

I get the out put I expect:

This is number 1
This is number 2
Called Continue task1
Called Continue task2


But I also get the following error which I can't track down:

raised TASKING_ERROR : s-tasren.adb:486

I'm running on a Xeon blade with gnat under linux

Thanks
Mike



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-15 17:43 task model Mike
@ 2009-10-15 18:00 ` Ludovic Brenta
  2009-10-15 19:15   ` Anh Vo
  0 siblings, 1 reply; 13+ messages in thread
From: Ludovic Brenta @ 2009-10-15 18:00 UTC (permalink / raw)


Michael Tabak wrote on comp.lang.ada:
> I'm prototyping some tasking with multiple accepts, the code is as
> follows:
>
> with Ada.Text_Io;
> use Ada.Text_Io;
>
> procedure task_test is
>
>    procedure Process_Primary(node: in Integer) is
>
>    begin
>
>        Put_Line("This is number" & Integer'Image(node));
>
>    end Process_Primary;
>
>    task proc1 is
>      entry process1(node : Integer);
>      entry continue;
>    end proc1;
>
>    task body proc1 is
>    begin
>
>      accept process1( node : Integer) do
>
>        Process_Primary( node );
>
>      end;
>
>      accept continue do
>        Put_Line("Called Continue task1");
>      end;
>
>    end proc1;
>
>    task proc2 is
>      entry process2(node : Integer);
>      entry continue;
>    end proc2;
>
>    task body proc2 is
>    begin
>
>      accept process2( node : Integer) do
>
>          Process_Primary( node );
>
>      end;
>
>      accept continue do
>        Put_Line("Called Continue task2");
>      end;
>
>    end proc2;
>
> begin
>   for i in 1..2 loop
>    proc1.process1(1);
>    proc2.process2(2);
>    proc1.continue;
>    proc2.continue;
>   end loop;
> end task_test;
> ---------------------------------------------------------------------------------------------------------------------
>
> I get the out put I expect:
>
> This is number 1
> This is number 2
> Called Continue task1
> Called Continue task2
>
> But I also get the following error which I can't track down:
>
> raised TASKING_ERROR : s-tasren.adb:486

Your expectations are wrong :)

Your environment task contains a loop calling each task four times:
for each task the call sequence is processN, continue, processN,
continue.  But the tasks do not contain a loop, so they can accept
only the first two entry calls and then they terminate.  So the second
call to process1 raises Tasking_Error because task1 has terminated by
now.

HTH

--
Ludovic Brenta.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-15 18:00 ` Ludovic Brenta
@ 2009-10-15 19:15   ` Anh Vo
  2009-10-15 19:54     ` Dmitry A. Kazakov
  2009-10-15 19:55     ` sjw
  0 siblings, 2 replies; 13+ messages in thread
From: Anh Vo @ 2009-10-15 19:15 UTC (permalink / raw)


On Oct 15, 11:00 am, Ludovic Brenta <ludo...@ludovic-brenta.org>
wrote:
> Michael Tabak wrote on comp.lang.ada:
>
> > I'm prototyping some tasking with multiple accepts, the code is as
> > follows:
>
> > with Ada.Text_Io;
> > use Ada.Text_Io;
>
> > procedure task_test is
>
> >    procedure Process_Primary(node: in Integer) is
>
> >    begin
>
> >        Put_Line("This is number" & Integer'Image(node));
>
> >    end Process_Primary;
>
> >    task proc1 is
> >      entry process1(node : Integer);
> >      entry continue;
> >    end proc1;
>
> >    task body proc1 is
> >    begin
>
> >      accept process1( node : Integer) do
>
> >        Process_Primary( node );
>
> >      end;
>
> >      accept continue do
> >        Put_Line("Called Continue task1");
> >      end;
>
> >    end proc1;
>
> >    task proc2 is
> >      entry process2(node : Integer);
> >      entry continue;
> >    end proc2;
>
> >    task body proc2 is
> >    begin
>
> >      accept process2( node : Integer) do
>
> >          Process_Primary( node );
>
> >      end;
>
> >      accept continue do
> >        Put_Line("Called Continue task2");
> >      end;
>
> >    end proc2;
>
> > begin
> >   for i in 1..2 loop
> >    proc1.process1(1);
> >    proc2.process2(2);
> >    proc1.continue;
> >    proc2.continue;
> >   end loop;
> > end task_test;
> > ---------------------------------------------------------------------------­------------------------------------------
>
> > I get the out put I expect:
>
> > This is number 1
> > This is number 2
> > Called Continue task1
> > Called Continue task2
>
> > But I also get the following error which I can't track down:
>
> > raised TASKING_ERROR : s-tasren.adb:486
>
> Your expectations are wrong :)
>
> Your environment task contains a loop calling each task four times:
> for each task the call sequence is processN, continue, processN,
> continue.  But the tasks do not contain a loop, so they can accept
> only the first two entry calls and then they terminate.  So the second
> call to process1 raises Tasking_Error because task1 has terminated by
> now.

I would like to expand Ludovic's comment.

You have a couple of options to improve it.

1. Make sure the task not terminated before calling its entry. That is
using proc1'terminated syntax to query it.
2. Add infinite loop enclosing accept statements. So, entry call can
be served always. This makes tasks alive even after the program ends
(program will hang) unless Ctrl-C or other mean to end it. The clean
solution is add entry Shutdown to the task. When this entry is
invoked, just exit the loop. Thus, the task will terminate cleanly.

Anh Vo





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-15 19:15   ` Anh Vo
@ 2009-10-15 19:54     ` Dmitry A. Kazakov
  2009-10-15 20:15       ` Jeffrey R. Carter
  2009-10-16 14:06       ` Georg Bauhaus
  2009-10-15 19:55     ` sjw
  1 sibling, 2 replies; 13+ messages in thread
From: Dmitry A. Kazakov @ 2009-10-15 19:54 UTC (permalink / raw)


On Thu, 15 Oct 2009 12:15:56 -0700 (PDT), Anh Vo wrote:

> I would like to expand Ludovic's comment.
> 
> You have a couple of options to improve it.
> 
> 1. Make sure the task not terminated before calling its entry. That is
> using proc1'terminated syntax to query it.
> 2. Add infinite loop enclosing accept statements. So, entry call can
> be served always. This makes tasks alive even after the program ends
> (program will hang) unless Ctrl-C or other mean to end it. The clean
> solution is add entry Shutdown to the task. When this entry is
> invoked, just exit the loop. Thus, the task will terminate cleanly.

2'. In order to terminate a task upon parent's completion use select
statement with a terminate alternative:

select
   accept XYZ;
or terminate; -- Ends the task when the parent terminates
end select;

(Shutdown entry is used when terminate is not allowed, e.g. when a delay
alternative or else part is present in the select, or when some explicit
completion code need to be executed, however any local objects are properly
finalized when the terminate alternative is accepted.)

3. Do not do any asynchronous (lengthy) processing within a rendezvous,
i.e. within do...end of an accept. During the rendezvous the caller is
blocked. Rendezvous is meant for short synchronous things, like passing the
parameters and results, then the caller is released and the callee
completes things asynchronously to the caller.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-15 19:15   ` Anh Vo
  2009-10-15 19:54     ` Dmitry A. Kazakov
@ 2009-10-15 19:55     ` sjw
  1 sibling, 0 replies; 13+ messages in thread
From: sjw @ 2009-10-15 19:55 UTC (permalink / raw)


On Oct 15, 8:15 pm, Anh Vo <anhvofrc...@gmail.com> wrote:

> 1. Make sure the task not terminated before calling its entry. That is
> using proc1'terminated syntax to query it.

There's a race condition there!

if not proc1'terminated then
   --  <<<<<<<<<<<<<<<<<<<<<preempted by proc1 here, which terminates
   proc1'process1(2);
   --  tasking_error here!
end if;



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-15 19:54     ` Dmitry A. Kazakov
@ 2009-10-15 20:15       ` Jeffrey R. Carter
  2009-10-16 14:06       ` Georg Bauhaus
  1 sibling, 0 replies; 13+ messages in thread
From: Jeffrey R. Carter @ 2009-10-15 20:15 UTC (permalink / raw)


Dmitry A. Kazakov wrote:
> 
> 3. Do not do any asynchronous (lengthy) processing within a rendezvous,
> i.e. within do...end of an accept. During the rendezvous the caller is
> blocked. Rendezvous is meant for short synchronous things, like passing the
> parameters and results, then the caller is released and the callee
> completes things asynchronously to the caller.

You can do anything you choose in an accept statement. Be aware that the calling 
task is blocked until the accept completes.

-- 
Jeff Carter
"Oh Lord, bless this thy hand grenade, that with it thou
mayst blow thine enemies to tiny bits, in thy mercy."
Monty Python and the Holy Grail
24



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-15 19:54     ` Dmitry A. Kazakov
  2009-10-15 20:15       ` Jeffrey R. Carter
@ 2009-10-16 14:06       ` Georg Bauhaus
  2009-10-16 14:57         ` Brad Moore
                           ` (2 more replies)
  1 sibling, 3 replies; 13+ messages in thread
From: Georg Bauhaus @ 2009-10-16 14:06 UTC (permalink / raw)


4.  add an exception handler to your tasks.
Have it print a message, for example, and
then re-raise the exception.  Otherwise tasks
may fail in silence and you never learn what
has happened.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-16 14:06       ` Georg Bauhaus
@ 2009-10-16 14:57         ` Brad Moore
  2009-10-16 20:06         ` Jeffrey R. Carter
  2009-10-19  7:56         ` Jean-Pierre Rosen
  2 siblings, 0 replies; 13+ messages in thread
From: Brad Moore @ 2009-10-16 14:57 UTC (permalink / raw)


Georg Bauhaus wrote:
> 4.  add an exception handler to your tasks.
> Have it print a message, for example, and
> then re-raise the exception.  Otherwise tasks
> may fail in silence and you never learn what
> has happened.

Or use the Ada 2005 package, Ada.Task_Termination
which allows you to install a handler that
gets called when tasks terminate.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-16 14:06       ` Georg Bauhaus
  2009-10-16 14:57         ` Brad Moore
@ 2009-10-16 20:06         ` Jeffrey R. Carter
  2009-10-17 11:10           ` Georg Bauhaus
  2009-10-19  7:56         ` Jean-Pierre Rosen
  2 siblings, 1 reply; 13+ messages in thread
From: Jeffrey R. Carter @ 2009-10-16 20:06 UTC (permalink / raw)


Georg Bauhaus wrote:
> 4.  add an exception handler to your tasks.
> Have it print a message, for example, and
> then re-raise the exception.  Otherwise tasks
> may fail in silence and you never learn what
> has happened.

What's the point of re-raising? Exceptions don't propagate from tasks.

-- 
Jeff Carter
"Why, the Mayflower was full of Fireflies, and a few
horseflies, too. The Fireflies were on the upper deck,
and the horseflies were on the Fireflies."
Duck Soup
95



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-16 20:06         ` Jeffrey R. Carter
@ 2009-10-17 11:10           ` Georg Bauhaus
  2009-10-17 12:18             ` Dmitry A. Kazakov
  2009-10-17 17:34             ` Jeffrey R. Carter
  0 siblings, 2 replies; 13+ messages in thread
From: Georg Bauhaus @ 2009-10-17 11:10 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> writes:

> Georg Bauhaus wrote:
>
> What's the point of re-raising? Exceptions don't propagate from tasks.

Uhm, I forgot to mention that a handler at some nesting level
might be reporting, permitting more accurate diagnostic messages.
It could then decide there is or is not a possible rescue,
and accordingly re-raise, for example.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-17 11:10           ` Georg Bauhaus
@ 2009-10-17 12:18             ` Dmitry A. Kazakov
  2009-10-17 17:34             ` Jeffrey R. Carter
  1 sibling, 0 replies; 13+ messages in thread
From: Dmitry A. Kazakov @ 2009-10-17 12:18 UTC (permalink / raw)


On Sat, 17 Oct 2009 13:10:04 +0200, Georg Bauhaus wrote:

> "Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> writes:
> 
>> Georg Bauhaus wrote:
>>
>> What's the point of re-raising? Exceptions don't propagate from tasks.
> 
> Uhm, I forgot to mention that a handler at some nesting level
> might be reporting, permitting more accurate diagnostic messages.
> It could then decide there is or is not a possible rescue,
> and accordingly re-raise, for example.

The problem is that there is no nesting of tasks in terms of control flow.
There is a parent child relation, but it is no nesting.

If an exception is propagated out of a task, the parent task is doing its
things asynchronously to the child. In order to propagate an exception in
the parent you have only two options:

1. an asynchronous transfer of control = in effect aborting/interrupting
the parent;
2. to synchronize with the parent making a rendezvous with it.

Neither looks much promising.

More generally speaking in Ada tasks are objects. As an object a failed
task may complain no earlier than upon its finalization if no other
contacts (rendezvous) happen. This is too late in all senses.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-17 11:10           ` Georg Bauhaus
  2009-10-17 12:18             ` Dmitry A. Kazakov
@ 2009-10-17 17:34             ` Jeffrey R. Carter
  1 sibling, 0 replies; 13+ messages in thread
From: Jeffrey R. Carter @ 2009-10-17 17:34 UTC (permalink / raw)


Georg Bauhaus wrote:
>>
>> What's the point of re-raising? Exceptions don't propagate from tasks.
> 
> Uhm, I forgot to mention that a handler at some nesting level
> might be reporting, permitting more accurate diagnostic messages.
> It could then decide there is or is not a possible rescue,
> and accordingly re-raise, for example.

Not an exception handler for the task, then, but for a declare statement within 
the task. In that case, a re-raise might be useful.

However, the primary use I have had for such a structure is to enclose an accept 
statement, allowing an explicitly raised exception to propagate from the accept 
to the caller, while being handled and ignored in the called task.

-- 
Jeff Carter
"You've got the brain of a four-year-old boy,
and I bet he was glad to get rid of it."
Horse Feathers
47



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: task model
  2009-10-16 14:06       ` Georg Bauhaus
  2009-10-16 14:57         ` Brad Moore
  2009-10-16 20:06         ` Jeffrey R. Carter
@ 2009-10-19  7:56         ` Jean-Pierre Rosen
  2 siblings, 0 replies; 13+ messages in thread
From: Jean-Pierre Rosen @ 2009-10-19  7:56 UTC (permalink / raw)


Georg Bauhaus a �crit :
> 4.  add an exception handler to your tasks.
> Have it print a message, for example, and
> then re-raise the exception.  Otherwise tasks
> may fail in silence and you never learn what
> has happened.
This pattern can be enforced by the following rule ine AdaControl:

check exception_propagation (task);

-- 
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2009-10-19  7:56 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-15 17:43 task model Mike
2009-10-15 18:00 ` Ludovic Brenta
2009-10-15 19:15   ` Anh Vo
2009-10-15 19:54     ` Dmitry A. Kazakov
2009-10-15 20:15       ` Jeffrey R. Carter
2009-10-16 14:06       ` Georg Bauhaus
2009-10-16 14:57         ` Brad Moore
2009-10-16 20:06         ` Jeffrey R. Carter
2009-10-17 11:10           ` Georg Bauhaus
2009-10-17 12:18             ` Dmitry A. Kazakov
2009-10-17 17:34             ` Jeffrey R. Carter
2009-10-19  7:56         ` Jean-Pierre Rosen
2009-10-15 19:55     ` sjw

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