comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: About task-safeness
Date: Thu, 3 Feb 2011 18:33:00 -0600
Date: 2011-02-03T18:33:00-06:00	[thread overview]
Message-ID: <iifhfu$m71$1@munin.nbi.dk> (raw)
In-Reply-To: 8qv8neF9i0U1@mid.individual.net

"Niklas Holsti" <niklas.holsti@tidorum.invalid> wrote in message 
news:8qv8neF9i0U1@mid.individual.net...
> Randy Brukardt wrote:
...
>> Note that "protection" may simply be declaring the object Atomic. 
>> Presuming the compiler supports that, there isn't a problem with multiple 
>> tasks accessing the same counter.
>
> Randy, could you be more explicit about your suggested use of Atomic? As I 
> understand it, even if a counter variable N is Atomic, two tasks 
> concurrently executing an assignment of the form
>
>    N := N + 1;
>
> can interleave their actions so that N is increased by only 1, not by 2 as 
> intended.

Good point. All of the examples I've used have been where one task set the 
value to some constant (like True or False), and other read it. That's safe, 
while doing both may not be.

Practically(*), however, this will be generated atomically on an x86 (at 
least on Janus/Ada): it will generate an
    Inc [N]
instruction, which I don't think can be interupted between the read and the 
write. The net effect is that the operation will be undivided on a single 
processor. (I don't really know how multicore systems might affect this; 
there may be additional locks needed here.)

(*) Note that Ada language rules might make generating this atomically 
impossible, such as if an overflow check is needed. In that case, separate 
read and write instructions might be needed. That's not a problem in the 
Janus/Ada runtime (where checks are suppressed always, mostly for size and 
speed reasons), but it might be in your code.

And of course, this is very target dependent. N := N + A; is much harder to 
generate atomically, and expressions involved atomic array components might 
be impossible.

                                   Randy.

P.S. None of this actually depends on "Atomic" in the current Janus/Ada 
compiler, as it isn't actually supported. But this optimization is done for 
all objects. In addition, Janus/Ada currently treats all writes as volatile. 
Most likely, the next version of Janus/Ada will have a full implementation 
of Atomic (it's been fully implemented in the front end for years - Isaac 
did it as part of an ACATS review contract back in the mid-90s. But I never 
implemented it in the optimizer and back-end - and that needs to be fixed.)





      parent reply	other threads:[~2011-02-04  0:33 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-02 20:51 About task-safeness mockturtle
2011-02-02 21:01 ` Vinzent Hoefler
2011-02-02 21:14   ` mockturtle
2011-02-02 22:16     ` Maciej Sobczak
2011-02-02 22:28       ` Shark8
2011-02-02 22:40         ` Peter C. Chapin
2011-02-03  8:33           ` Dmitry A. Kazakov
2011-02-03 17:59       ` Vinzent Hoefler
2011-02-02 22:38 ` J-P. Rosen
2011-02-03  2:44 ` Randy Brukardt
2011-02-03  8:53   ` Niklas Holsti
2011-02-03 11:07     ` Georg Bauhaus
2011-02-03 11:22       ` AdaMagica
2011-02-03 18:13         ` Jeffrey Carter
2011-02-04  0:33     ` Randy Brukardt [this message]
replies disabled

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