comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Problem with task component
Date: Mon, 24 Sep 2012 09:23:49 +0100
Date: 2012-09-24T09:23:49+01:00	[thread overview]
Message-ID: <m2r4pr7rsa.fsf@pushface.org> (raw)
In-Reply-To: k3or1c$v1i$1@dont-email.me

"J-P. Rosen" <rosen@adalog.fr> writes:

> Le 19/09/2012 23:48, Simon Wright a écrit :
>> Using interrupts is in the next phase, for now I'm just polling by
>> reading the chip's registers so I must delay & can't use terminate. A
>> pain.
> I don't understant what you'd want. Terminate is selected when a whole
> subsystem is quiet. I you have a delay alternative, a task can be
> revived, and in turn revive the whole subsystem. There is no way
> terminate could be selected.

The language in ARM9.3 is, now you point it out, quite opaque,
especially if you approach it with wrong assumptions.

The general case (common in embedded systems) is to have a task
executing a loop waiting for something to happen and to need to stop the
task.

As Dmitry and others have pointed out, if the "something" is a network
I/O, you have to arrange to cancel the I/O (eg by closing the socket)
and you can then exit the task.

But, if what you want is to delay, you want to write

   loop
      select
         delay Interval;
         --  do work
      or
         --  exit the loop if there's some reason to stop
      end select;
   end loop;

What I wanted to do was to be able to write 'terminate' and to have the
runtime realise that finalization of the object containing the task
would be a good reason to take the terminate alternative[1].

   [I don't know if that would work if I'd used an accept instead of a
   delay, which would have made the code compilable?????]

Instead, the idiom needs to be

   loop
      select
         delay Interval;
         --  do work
      or
        accept Stop;
        exit;
      end select;
   end loop;

[1] See, for example, the last three paras of John English's book ch19
sec3,
http://faculty.cs.wwu.edu/reedyc/AdaResources/bookhtml/ch19.htm#19.3

   This solution requires the master task to call Shutdown explicitly
   when it wants to terminate the task. The disadvantage with this
   approach is that it’s possible to forget to call Shutdown. A better
   solution is to add a terminate alternative to the select statement:

    task body Spreadsheet_Task is
    begin
        loop
            select
                accept Recalculate;
                Do_Recalculation;
            or
                accept Shutdown;
                exit;
            or
                terminate;
            end select;
        end loop;
    end Spreadsheet_Task;

   The terminate alternative must be the last one in a select statement,
   and it can’t contain anything except a terminate statement like the
   one shown above. When the master task gets to the end of the block
   where the spreadsheet was declared, the spreadsheet task will
   terminate the next time that the select statement is executed (or
   immediately, if the task is already waiting in the select
   statement). This means that the master doesn’t have to do anything
   special to terminate the task, but it can still call Shutdown if it
   wants to terminate the task before the end of the block where it was
   declared.



  reply	other threads:[~2012-09-24  8:23 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-19 18:22 Problem with task component Simon Wright
2012-09-19 18:48 ` Dmitry A. Kazakov
2012-09-19 20:40   ` Simon Wright
2012-09-19 21:11     ` Dmitry A. Kazakov
2012-09-19 21:48       ` Simon Wright
2012-09-24  5:28         ` J-P. Rosen
2012-09-24  8:23           ` Simon Wright [this message]
2012-09-24 13:37             ` J-P. Rosen
2012-09-24 14:17               ` Simon Wright
2012-09-24 17:53               ` Robert A Duff
2012-09-24 20:40                 ` J-P. Rosen
2012-09-24 15:44             ` Adam Beneschan
2012-09-24 16:13               ` AdaMagica
2012-09-24 16:43                 ` Adam Beneschan
2012-09-24 16:29               ` Dmitry A. Kazakov
2012-09-24 20:49                 ` J-P. Rosen
2012-09-25  7:35                   ` Dmitry A. Kazakov
2012-09-25 17:52                     ` J-P. Rosen
2012-09-25 18:35                       ` Dmitry A. Kazakov
2012-09-25 19:22                         ` J-P. Rosen
2012-09-26  7:27                           ` Dmitry A. Kazakov
2012-09-26 11:49                             ` Georg Bauhaus
2012-09-26 14:13                               ` Dmitry A. Kazakov
2012-09-19 19:44 ` Adam Beneschan
replies disabled

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