comp.lang.ada
 help / color / mirror / Atom feed
From: "Ben Brosgol" <brosgol@world.std.com>
Subject: Re: Erroneous execution? (was Re: gnat: time-slicing)
Date: Fri, 2 Aug 2002 03:59:25 GMT
Date: 2002-08-02T03:59:25+00:00	[thread overview]
Message-ID: <H077Bu.J1D@world.std.com> (raw)
In-Reply-To: wcck7name3h.fsf@shell01.TheWorld.com

> > > > with Ada.Text_IO; use Ada.Text_IO;
> > > >
> > > > procedure Task_Demo is
> > > >   task A;
> > > >   task body A is
> > > >   begin
> > > >     Put_Line("b");
> > > >     Put_Line("b");
> > > >   end A;
> > > > begin
> > > >   Put_Line("a");
> > > >   Put_Line("a");
> > > > end Task_Demo;
> > > > ---------------
> > >
> > > The above example is erroneous, which means that the output is totally
> > > unpredictable.  The reason it's erroneous is that two tasks are
messing
> > > with the same shared variable (the standard output file) at the same
> > > time.
> >
> > Bob-
> >
> > Are you sure this is erroneous?
>
> No...
>
> You're trying to language-lawyer me to death, Ben.  ;-)

Not quite that extreme, but at least to the point of moderate pain and
suffering :-)

> I *think* it's erroneous, and I'm sure it was *intended* to be erroneous
> by the language designers.

I'm not so sure that it was intended to be erroneous.  If each task were
invoking Put_Line(Some_File, ...) on the same explicitly-passed file
parameter, then yes I agree that the execution would be erroneous.  But
where the shared variable is hidden (i.e., not passed as a parameter), I
believe that the implementation needs to provide the protection (for
subprograms from predefined packages).

As an example of this principle, suppose that the implementor of
Ada.Numerics.Elementary_Functions decided to keep a hash table of (arg,
sin(arg)) pairs in the package body; i.e., sin(arg) does a hash table lookup
and possible insertion.  There's no way that the user would know this, so if
the implementation did not protect the shared hash table from simultaneous
access, calling sin(x) from concurrent tasks could result in erroneous
execution.  So A(3) kicks in and requires the protection to be supplied.  I
think the same principle applies to the Put(Char), although in this case it
may be a bit more obvious that there's a shared variable lurking in the
shadows.

> >...The "shared variable" is hidden in the
> > private part or body of a predefined library package and ARM paragraph
A(3)
> > says that in such cases it is the responsibility of the implementation
to
> > protect the shared variable from getting trashed (*).  Now Put_Line is
> > equivalent to looping over Put() and then doing a New_Line so the user
may
> > see interspersed output -- e.g., either or both "b"s might not be
> > immediately followed by a newline -- but the implementation would be
> > nonconformant with the ARM if, for example, simultaneous calls on Put
caused
> > the machine to reboot (which was the effect one saw in an early version
of
> > the old Alsys DOS Ada compiler :-)
>
> But the AARM annotation attached to this paragraph says:
>
>     3.a   Ramification: For example, simultaneous calls to Text_IO.Put
will
>           work properly, so long as they are going to two different files.
On
>           the other hand, simultaneous output to the same file constitutes
>           erroneous use of shared variables.

Well of course the AARM has no formal status :-)  Also, the wording in 3.a
is ambiguous, since it might be referring to the Put procedure that has a
File_Type as an explicit parameter, in which case I agree that the
simultaneous calls are erroneous.

> which indicates the intent pretty clearly.

Or maybe not so clearly :-)

> The program above is doing
> output to the same file simultaneously.

Agreed, but I still think there's a difference (in the obligation of the
implementation to protect against simultaneous access) based on whether the
file is passed as an explicit parameter or is hidden in the implementation.

> If you want RM exegesis, I'll point you to A.10.6(2), which says the
> above is equivalent to passing the current output file as a parameter,
> which is clearly a shared variable.

A.10.6.2 says: "All procedures Get and Put have forms with a file parameter,
written first. Where this parameter is omitted, the appropriate (input or
output) current default file is understood to be specified."  This is not
quite the same as saying that the current default file is understood to be
passed as a parameter.  I.e., "specified" could be interpreted as "the file
that is operated upon by the procedure".

> > I'm not recommending that anyone actually write code such as Task_Demo,
but
> > I don't think it's an example of erroneous execution.
> >
> > (*) The rule actually says "The implementation shall ensure that each
> > language defined subprogram is reentrant in the sense that concurrent
calls
> > on the same subprogram perform as specified, so long as all parameters
that
> > could be passed by reference denote nonoverlapping objects."  But I
believe
> > the intent is as I indicated.
> >
> > -Ben
>
> P.S. *You* wrote much of Annex A!

True, which is why I raised this issue in the first place :-) although if
you do not have a similar recollection then maybe what I am remembering (the
non-erroneousness of simultaneous calls on Put(Char)) is an intermediate
decision that was later changed?  In any event I will not claim the credit /
blame for the wording of A(3); that was from the pen of Chairman Tuck.

-Ben






  reply	other threads:[~2002-08-02  3:59 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-15 11:25 gnat: time-slicing Jan Prazak
2002-07-15  8:44 ` Dale Stanbrough
2002-07-15 19:10   ` Jan Prazak
2002-07-15 17:16     ` David C. Hoos
2002-07-15 23:30       ` Jan Prazak
2002-07-16  0:54         ` Jan Prazak
2002-07-16 10:46         ` Lutz Donnerhacke
2002-07-16 11:57           ` Aaro Koskinen
2002-07-16 12:57           ` SteveD
2002-07-16 15:18           ` Florian Weimer
2002-07-16 13:29     ` Marin David Condic
2002-07-17 19:29       ` Jan Prazak
2002-07-15 13:07 ` time-slicing David C. Hoos
2002-07-15 14:56   ` time-slicing Ian Broster
2002-07-15 19:10   ` time-slicing Jan Prazak
2002-07-15 19:10   ` time-slicing Jan Prazak
2002-07-15 19:05     ` time-slicing Anders Gidenstam
2002-07-15 23:30       ` time-slicing Jan Prazak
2002-07-15 20:33         ` time-slicing Darren New
2002-07-16 16:30         ` time-slicing Pascal Obry
2002-07-16 23:05           ` time-slicing Jan Prazak
2002-07-16 21:33     ` time-slicing Robert Dewar
2002-07-15 21:03 ` gnat: time-slicing tmoran
2002-07-16 13:04   ` Jan Prazak
2002-07-16 21:29 ` Robert Dewar
2002-07-17 19:29   ` Jan Prazak
2002-07-17 16:44     ` Pascal Obry
2002-07-17 21:38       ` Jan Prazak
2002-07-17 19:21         ` Randy Brukardt
2002-07-17 22:44           ` Jan Prazak
2002-07-17 19:57             ` Marin David Condic
2002-07-18 18:38               ` Larry Kilgallen
2002-07-20 11:52                 ` Robert Dewar
2002-07-17 19:43         ` Pascal Obry
2002-07-18 18:55           ` Jan Prazak
2002-07-18 17:01             ` Pascal Obry
2002-07-18 17:03             ` Pascal Obry
2002-07-18 22:38         ` chris.danx
2002-07-18  2:50     ` R. Tim Coslet
2002-07-18 12:54       ` SteveD
2002-07-20 11:56       ` Robert Dewar
2002-07-18 12:02     ` Frank J. Lhota
2002-07-19  2:33 ` Robert A Duff
2002-07-19 13:32   ` Jan Prazak
2002-07-19 23:46   ` Keith Thompson
2002-07-20  0:36     ` Robert A Duff
2002-07-20  4:25       ` Darren New
2002-07-20 11:47     ` Robert Dewar
2002-07-21 10:58       ` Keith Thompson
2002-07-31 22:28       ` Robert A Duff
2002-08-01 19:28   ` Erroneous execution? (was Re: gnat: time-slicing) Ben Brosgol
2002-08-01 22:03     ` Robert A Duff
2002-08-02  3:59       ` Ben Brosgol [this message]
2002-08-13 22:30         ` Robert A Duff
2002-08-02  4:17       ` Robert Dewar
2002-07-19  3:17 ` time-slicing SteveD
2002-07-19 13:32   ` time-slicing Jan Prazak
2002-07-19 12:41     ` time-slicing SteveD
replies disabled

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