comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Termination of periodic tasks
Date: Tue, 17 Jun 2014 10:45:38 +0200
Date: 2014-06-17T10:45:38+02:00	[thread overview]
Message-ID: <a1wv6jbm09et.1x4jn9fg3e0ds.dlg@40tude.net> (raw)
In-Reply-To: slrnlpvsl2.i0l.lithiumcat@nat.rebma.instinctive.eu

On Tue, 17 Jun 2014 07:47:49 +0000 (UTC), Natasha Kerensikova wrote:

> Hello,
> 
> On 2014-06-17, Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> wrote:
>> On Tue, 17 Jun 2014 06:57:38 +0000 (UTC), Natasha Kerensikova wrote:
>>
>> [...]
>>> Is there a way to work around such a situation?
>>
>> The solution is always same: wrap a task pointer in a controlled object.
>> Manage the task from the object's Initialize and Finalize.
>>
> But if I change Global_Worker from an access-to-task to a controlled
> object, its master will still be the environment task, so Finalize
> wouldn't be called before termination of the worker task.
> 
> Or am I missing something?

You should put it in a separate package to ensure different scopes of the
controlled object and the access-to-task type.

> Is there a special construct that yield an
> earlier call to Finalize than Cron_Entry? Would you happen to have a
> working example?

with Ada.Finalization;

package Workers is
   type Manager is new Ada.Finalization.Limited_Controlled with private;

private
   task type Worker is
      entry Live;
      entry Die;
   end Worker;
   type Worker_Ptr is access Worker;
   
   type Manager is new Ada.Finalization.Limited_Controlled with record
      Staff : Worker_Ptr;
   end record;
   overriding procedure Initialize (Handler : in out Manager);
   overriding procedure Finalize (Handler : in out Manager);

end Workers;
--------------------------------------------------------------
with Ada.Unchecked_Deallocation;

with Ada.Text_IO;  use Ada.Text_IO;

package body Workers is
   
   task body Worker is
   begin
      accept Live;
      Put_Line ("I am born!");
      loop
         select
            accept Die;
            exit;
         else delay 1.0;
            Put_Line ("I am alive!");
         end select;
      end loop;
      Put_Line ("You killed me!");
   end Worker;

   procedure Initialize (Handler : in out Manager) is
   begin
      Handler.Staff := new Worker;
      Handler.Staff.Live;
   end Initialize;
   
   procedure Finalize (Handler : in out Manager) is
      procedure Free is
         new Ada.Unchecked_Deallocation (Worker, Worker_Ptr);
   begin
      if Handler.Staff /= null then
         Handler.Staff.Die;
         while not Handler.Staff'Terminated loop
            delay 0.01;
         end loop;
         Free (Handler.Staff);
      end if;
   end Finalize;

end Workers;
-------------------------------------------------------
with Ada.Text_IO;  use Ada.Text_IO;
with Workers;      use Workers;

procedure Test is
   X : Manager;
begin
   delay 5.0;
   Put_Line ("Good bye!");
end Test;
-----------------------------------------------------

X is finalized before Workers (and Worker_Ptr), so it does not wait for all
instances to terminate.

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


  reply	other threads:[~2014-06-17  8:45 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-15 10:10 Termination of periodic tasks Natasha Kerensikova
2014-06-15 12:11 ` Dmitry A. Kazakov
2014-06-15 15:23 ` J-P. Rosen
2014-06-16 13:54   ` Natasha Kerensikova
2014-06-17 20:14     ` Charles H. Sampson
2014-06-18  7:32       ` Dmitry A. Kazakov
2014-06-15 16:54 ` Jeffrey Carter
2014-06-16 14:02   ` Natasha Kerensikova
2014-06-16 15:08     ` Dmitry A. Kazakov
2014-06-16 17:08     ` Jeffrey Carter
2014-06-17  6:57       ` Natasha Kerensikova
2014-06-17  7:37         ` Dmitry A. Kazakov
2014-06-17  7:47           ` Natasha Kerensikova
2014-06-17  8:45             ` Dmitry A. Kazakov [this message]
2014-06-17  9:00               ` Natasha Kerensikova
2014-06-17 12:55                 ` Dmitry A. Kazakov
2014-06-17 14:51                   ` J-P. Rosen
2014-06-17 16:44                     ` Dmitry A. Kazakov
2014-06-17 20:00                       ` Randy Brukardt
2014-06-17 20:16                         ` Jeffrey Carter
2014-06-17 21:30                         ` Simon Wright
2014-06-17 12:02         ` Jacob Sparre Andersen
2014-06-17 19:32           ` Natasha Kerensikova
2014-06-17 17:53         ` Jeffrey Carter
2014-06-17 20:03           ` Randy Brukardt
replies disabled

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