comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Abortable Timed Action
Date: Sat, 09 Jan 2016 08:45:50 +0000
Date: 2016-01-09T08:45:50+00:00	[thread overview]
Message-ID: <ly37u7p2dd.fsf@pushface.org> (raw)
In-Reply-To: n6p5tu$9mv$1@adenine.netfront.net

"T.G." <anon@anon.org> writes:

> The reason why I had an explicit Finish instead of using terminate was
> that I was thinking of creating the timer dynamically and then freeing
> it with Ada.Unchecked_Deallocation. So I wanted to Finish the task
> before freeing it. I'm not sure if calling Free on an access actually
> terminates the task Normally.

GNAT certainly used to have issues in this area. Indeed, you could abort
the task and then deallocate it, and end with a memory leak (the task
control block wasn't actually freed); the cure was to wait until
'Terminated.

I believe that this is no longer a problem.

> delay until is an interesting idea. I'm assuming that time drift would
> be an issue in periodic actions that repeat at a certain interval, but
> in that case delay until could also have issues if it loses some
> accuracy on each iteration.

The common solution is something like

   with Ada.Calendar;
   with Ada.Text_IO; use Ada.Text_IO;
   procedure Delay_Until is
      task T is
         entry Exec_After (T : Duration);
      end T;
      task body T is
         Start : Ada.Calendar.Time;
         Next : Ada.Calendar.Time;
         Interval : Duration;
         use type Ada.Calendar.Time;
      begin
         accept Exec_After (T : Duration) do
            Start := Ada.Calendar.Clock;
            Next := Start + T;
            Interval := T;
         end Exec_After;
         loop
            delay until Next;
            Next := Next + Interval;  -- *not* Ada.Calendar.Clock + Interval
            Put_Line (Duration'Image (Ada.Calendar.Clock - Start));
         end loop;
      end T;
   begin
      T.Exec_After (0.5);
   end Delay_Until;

  reply	other threads:[~2016-01-09  8:45 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-31  4:15 Abortable Timed Action T.G.
2015-12-31  6:40 ` Anh Vo
2015-12-31  7:32   ` T.G.
2015-12-31 16:21     ` Anh Vo
2015-12-31 18:09       ` T.G.
2016-01-06 21:14         ` Anh Vo
2016-01-08 20:24           ` T.G.
2016-01-09  8:45             ` Simon Wright [this message]
2016-01-09  9:10               ` Dmitry A. Kazakov
2016-01-09 14:41               ` Bob Duff
2016-01-09 15:59               ` T.G.
replies disabled

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