comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Delayed deallocation of non-terminated task in Gnat?
Date: Tue, 30 Aug 2011 21:23:54 +0200
Date: 2011-08-30T21:23:54+02:00	[thread overview]
Message-ID: <w5j08a2jvbg4$.1niz8hbw0o4om.dlg@40tude.net> (raw)
In-Reply-To: 9c4q5iFbssU1@mid.individual.net

On Tue, 30 Aug 2011 21:57:54 +0300, Niklas Holsti wrote:

> Dmitry A. Kazakov wrote:
>> On Tue, 30 Aug 2011 08:38:52 -0700 (PDT), Adam Beneschan wrote:
>> 
>>>  I'm having a bit of difficulty figuring out from
>>> the RM what the exact semantics of finalizing an unterminated task
>>> object are, but I'm pretty sure that aborting the task is not one of
>>> them.  (Note that this applies only to tasks without discriminants.
>>> If the task has discriminants, freeing it is an
>>> error---13.11.2(11ff).)
>> 
>> What is the difference? Is it an attempt to have freed tasks running
>> further? Or an attempt to construct a race condition, e.g. when the task
>> being freed has an open terminate alternative or else has been accepted a
>> rendezvous and now is going down? 
> 
> I assume that by "it" above, Dmitry is referring to the AdaCore notice 
> about a change in the way Unchecked_Deallocation works in on tasks in 
> GNAT, as quoted by Marc in his original post:

No, I meant the rationale behind ARM 13.11.2(11), which looks dubious to
me.

> "If Unchecked_Deallocation is called on a non-terminated task (which was 
> previously a no-op), the task is now marked to be freed automatically 
> when it terminates."
> 
> It seems to me that the new behaviour is more useful than the old behaviour.

Maybe. But it is unclear if it is consistent with the expectations of a
naive user. Which is: Unchecked_Deallocation awaits for the task
termination and then frees whatever memory the task is using.

>> Considering this standard pattern:
>> 
>> type Object;
>> task type Worker (Self : not null access Object'Class) is
>>    entry Shut_Down;
>> end Worker;
>> type Worker_Ptr is access Worker;
>> type Object is new Ada.Finalization.Limited_Controlled with record
>>    Worker : Worker_Ptr;
>> end Object;
>> overriding procedure Finalize (This : in out Object);
> 
> (I don't think that the use of a controlled type is central to the 
> example. Am I wrong, Dmitry?)

It is the most common case when a task has a discriminant. Since task as a
component is a non-starter, the only work-around would be a controlled type
removing its access-to-task component from Finalize.

>> Now the following is a race with a bounded error:
> 
> As I understand it, there was a race under the old behaviour, but not 
> under the new behaviour.

Well, it could be read as if the choice offered by ARM 13.11.2(12) was
taken. Though according to ARM, it is still a bounded error, and maybe even
worse than that (see the scenario below).

>> procedure Finalize (This : in out Object) is
>>    procedure Free is new Ada.Unchecked_Deallocation (...);
>> begin
>>    if This.Worker /= null then
>>       This.Worker.Shut_Down;
>>       Free (This.Worker);
> 
> With the old behaviour of Unchecked_Deallocation, the effect of this 
> Free call depended on the state of This.Worker: if the task was not yet 
> terminated, Free had no effect; if the task was terminated, Free 
> deallocated the task object.
> 
> With the new behaviour, the task object is always deallocated, either by 
>   the Free call (if the task is already terminated at that time) or by 
> the RTS when the task terminates later, after the Free call.

This is unclear. Because if Free is not blocked, the Object's Finalize
continues, possibly ends (and then the object is freed), while the task is
still running and possibly accessing the object through its discriminant.
This kind of error is unbounded in contradiction to ARM 13.11.2(11). It
would be rather catastrophic behavior.

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



  reply	other threads:[~2011-08-30 19:23 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-30 13:22 Delayed deallocation of non-terminated task in Gnat? Marc C
2011-08-30 15:20 ` Dmitry A. Kazakov
2011-08-31 18:12   ` Robert A Duff
2011-08-31 19:23     ` Dmitry A. Kazakov
2011-08-31 20:58       ` Robert A Duff
2011-08-31 21:16         ` Jeffrey Carter
2011-08-31 20:25     ` J-P. Rosen
2011-08-31 21:09       ` Robert A Duff
2011-08-31 21:53         ` Simon Wright
2011-08-30 15:38 ` Adam Beneschan
2011-08-30 16:42   ` Dmitry A. Kazakov
2011-08-30 18:57     ` Niklas Holsti
2011-08-30 19:23       ` Dmitry A. Kazakov [this message]
2011-08-31 16:39   ` Adam Beneschan
2011-08-31 18:21     ` Robert A Duff
2011-08-31 23:28       ` Adam Beneschan
2011-09-01 11:58         ` Robert A Duff
2011-08-31 20:30     ` J-P. Rosen
2011-08-31 22:36       ` Adam Beneschan
2011-09-01  5:24         ` J-P. Rosen
2011-08-31 18:08 ` Robert A Duff
replies disabled

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