public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: threadsafeness at egcs1.0.3 and 1.1
@ 1998-07-13 15:28 Mike Stump
  1998-07-13 19:49 ` Ulrich Drepper
  1998-07-14  0:08 ` Martin von Loewis
  0 siblings, 2 replies; 16+ messages in thread
From: Mike Stump @ 1998-07-13 15:28 UTC (permalink / raw)
  To: drepper, egcs

> To: egcs@cygnus.com
> Date: 10 Jul 1998 20:24:11 -0700
> From: Ulrich Drepper <drepper@cygnus.com>
> Stamped: newsgate-cygnus

> martin@mira.isdn.cs.tu-berlin.de (Martin von Loewis) writes:

> > int f()
> > {
> >   static int i = get_some_value();
> >   return i;
> > }

> long).  Plus there is the problem of handling this case if no
> threaded program is used.  (Note that it isn't possible to have a
> simple "not done"/"done" flag since the second thread arriving must
> not return from the get_some_value call until the first did and this
> requires interaction with the thread package.  Busy waiting is no
> option since the waiting thread might have a higher priority.)

No, this is defined in C++ as i having the value zero on the second
call until someone starts changing i.  The implementation is required
to not block, pend or busy wait.  We can extend this out to threaded
apps uniformly.  The setting and testing of the single flag that
controls initialization should be critical regioned.

But yes, even when we do all this, it doesn't have nice semantics in
beefy threaded programs, unless you use sigatomic_t and volatile.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
  1998-07-13 15:28 threadsafeness at egcs1.0.3 and 1.1 Mike Stump
@ 1998-07-13 19:49 ` Ulrich Drepper
  1998-07-14  0:08 ` Martin von Loewis
  1 sibling, 0 replies; 16+ messages in thread
From: Ulrich Drepper @ 1998-07-13 19:49 UTC (permalink / raw)
  To: Mike Stump; +Cc: egcs

mrs@wrs.com (Mike Stump) writes:

> No, this is defined in C++ as i having the value zero on the second
> call until someone starts changing i.  The implementation is required
> to not block, pend or busy wait.  We can extend this out to threaded
> apps uniformly.  The setting and testing of the single flag that
> controls initialization should be critical regioned.

No, this is certainly wrong.  The handling must be as for recursive
mutexes.  With them it is allowed for one thread to allocate the mutex
as often as it wants.  It is used in situations where a certain region
is protected using a mutex and calling one function in the region
might call another one.

A different thread has to wait to get the mutex and this is exactly
what has to happen here.

Assume this:

void
very_important_logging (string s)
{
  static int pid = my_getpid ();
  if (pid == 0)
    /* Recursive call, don't do anything.  */
    return;
  cout << process_name[pid] << ": " << s << endl;
}

If my_getpid() can lead to an error which results in calling
very_important_logging again the value of `pid' is 0 as you said and
the thread must not block.  In this case it simply returns and might
do some reasonable work (maybe terminate the program or whatever).

If a second thread would enter the scene which the other thread
executes the my_getpid function it would simply fail to print anything
and return and we loose an important message.


I.e., given your interpretation (which is not founded by the standard
since it does not handle threads at all) the value 0 would signal two
things: the own thread executes the initializer and another thread
executes the initializer.  This is the differenciation made in the
POSIX standard at it makes sense.  Your interpretation wouldn't allow
use to differentiate these two cases.


On the other hand making the call to the initializer similar to a
pthread_once call would mean that we a) can differentiate the cases,
and b) keep the semantic of any code as if it would run in single
threaded programs.

-- 
---------------.      drepper at gnu.org  ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com   `------------------------

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
  1998-07-13 15:28 threadsafeness at egcs1.0.3 and 1.1 Mike Stump
  1998-07-13 19:49 ` Ulrich Drepper
@ 1998-07-14  0:08 ` Martin von Loewis
  1 sibling, 0 replies; 16+ messages in thread
From: Martin von Loewis @ 1998-07-14  0:08 UTC (permalink / raw)
  To: mrs; +Cc: drepper, egcs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1365 bytes --]

> > > int f()
> > > {
> > >   static int i = get_some_value();
> > >   return i;
> > > }
> 
[one thread initialising, and the other one entering]
> 
> No, this is defined in C++ as i having the value zero on the second
> call until someone starts changing i.  The implementation is required
> to not block, pend or busy wait.

No, this is undefined:

>> If control re­enters the declaration (recursively) while the object
>> is being initialized, the behavior is undefined.

Even though it says "(recursively)", I'd read that as 'access is
undefined while initialization is in progress'. So blocking in a MT
context seems acceptable.

Furthermore, the second thread must not also perform the
initialization, because i is initialized "before its block is first
entered".

Also, just skipping initialization (and thus accessing the
zero-initialized state) is not in the spirit of the spec. If
initialization is not complete, "it will be tried again the next time
control enters the declaration". Formally, this applies only to
initializations left through exceptions, but providing this semantics
in the MT case seems like a reasonable extension.

In short, g++ currently doesn't comply with the standard (no
exception-awareness), so this needs to be changed, anyway.
Considering MT here seems seems like a good idea to me, at
least as an option.

Regards,
Martin

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
  1998-07-13  8:40       ` Ulrich Drepper
@ 1998-07-14 16:57         ` Nicholas Lee
  0 siblings, 0 replies; 16+ messages in thread
From: Nicholas Lee @ 1998-07-14 16:57 UTC (permalink / raw)
  To: Ulrich Drepper; +Cc: egcs

On 13 Jul 1998, Ulrich Drepper wrote:

> n.lee@math.auckland.ac.nz (Nicholas Lee) writes:
> 
> > His basic comment is that function statics have value in that they nail
> > down the point of initialisation a some function call time.  
> 
> I don't know whether I understand this sentence.  But it seems this is
> exactly what we are talking about.  We have to initialise the function
> static, but how is the question.  It's no problem in single-threaded
> programs, we are talking about multi-threaded programs.

He compares it with Class and global statics where the order of
initialisation is fixed within translation units, but not across them. 

Class Static initialisation order might be even more of a problem in a
threaded application, where different threads might enter different
translation units at different times or at the same time. 


Nicholas

--
	"I reserve the right to contradict myself."

Nicholas Lee (Li Peng Ming)  n.lee with math.auckland.ac in nz
Auckland University, New Zealand	






^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
@ 1998-07-13 17:29 Mike Stump
  0 siblings, 0 replies; 16+ messages in thread
From: Mike Stump @ 1998-07-13 17:29 UTC (permalink / raw)
  To: drepper; +Cc: egcs

I don't have enough of a clue, I defer to your expertise.  Thanks for
the clue, I'll try and remember it.

> From: Ulrich Drepper <drepper@cygnus.com>
> Date: 13 Jul 1998 16:21:37 -0700

> No, this is certainly wrong.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
@ 1998-07-13 12:40 Mike Stump
  0 siblings, 0 replies; 16+ messages in thread
From: Mike Stump @ 1998-07-13 12:40 UTC (permalink / raw)
  To: scherrey; +Cc: egcs

> Date: Fri, 10 Jul 1998 23:03:51 -0400
> From: Ben Scherrey <scherrey@gte.net>
> To: Mike Stump <mrs@wrs.com>
> CC: egcs@cygnus.com

> Whoa! Someone said manual. Where do I get one?????
> 	...or is this some kind of sick joke?

544 pages of manual, and you didn't read it all yet?  Talk about lead
a horse to water...  You can either order one from the FSF, or print
one up yourself.

The sources are in *.texi in the source tree.  I usually just refer to
the .texi file, as it is the more accurate and up-to-date.  I have a
printed copy from the FSF, if I want to do bulk reading or touch
paper.

To print it up yourself, you will need tex and a really good
(reliable) postscript printer (one that errors every 10 pages won't
cut it).

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
  1998-07-13  4:36     ` Nicholas Lee
@ 1998-07-13  8:40       ` Ulrich Drepper
  1998-07-14 16:57         ` Nicholas Lee
  0 siblings, 1 reply; 16+ messages in thread
From: Ulrich Drepper @ 1998-07-13  8:40 UTC (permalink / raw)
  To: Nicholas Lee; +Cc: egcs

n.lee@math.auckland.ac.nz (Nicholas Lee) writes:

> His basic comment is that function statics have value in that they nail
> down the point of initialisation a some function call time.  

I don't know whether I understand this sentence.  But it seems this is
exactly what we are talking about.  We have to initialize the function
static, but how is the question.  It's no problem in single-threaded
programs, we are talking about multi-threaded programs.

-- 
---------------.      drepper at gnu.org  ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com   `------------------------

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
       [not found]     ` <199807112057.WAA00409.cygnus.egcs@mira.isdn.cs.tu-berlin.de>
@ 1998-07-13  6:39       ` Ulrich Drepper
  0 siblings, 0 replies; 16+ messages in thread
From: Ulrich Drepper @ 1998-07-13  6:39 UTC (permalink / raw)
  To: egcs

martin@mira.isdn.cs.tu-berlin.de (Martin von Loewis) writes:

> This would also simplify linking to non-threaded code: the libgcc2
> __cpp_once function would just call the initializer in the
> non-threaded case.

As I've said, you cannot reimplement the function without knowing
exactly how the thread package used works.  If you are not going for
efficiency you can use a mutex which is very expensive and still has
to be adapted for the platform + used thread package.  E.g., what
happens on Solaris where two thread packages are in use?

-- 
---------------.      drepper at gnu.org  ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com   `------------------------

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
  1998-07-10 21:04   ` Ulrich Drepper
  1998-07-11 14:02     ` Martin von Loewis
@ 1998-07-13  4:36     ` Nicholas Lee
  1998-07-13  8:40       ` Ulrich Drepper
       [not found]     ` <199807112057.WAA00409.cygnus.egcs@mira.isdn.cs.tu-berlin.de>
  2 siblings, 1 reply; 16+ messages in thread
From: Nicholas Lee @ 1998-07-13  4:36 UTC (permalink / raw)
  To: Ulrich Drepper; +Cc: egcs

On 10 Jul 1998 20:24:11 -0700, you wrote:

>martin@mira.isdn.cs.tu-berlin.de (Martin von Loewis) writes:
>
>> int f()
>> {
>>   static int i = get_some_value();
>>   return i;
>> }
>> 
>> If two threads call f concurrently, initialization might be performed
>> twice. Since this can be handled on the application level in many
>> cases, it is not considered a serious problem (by some).
>
[...]
>I see this case as a real boundary case which probably shouldn't
>handled at all and the use of non-const initializing of static
>variables should be outlawed.

Have you read Meyer's comment in MEC++, about function static vs class
static? (pg 133-134)

His basic comment is that function statics have value in that they nail
down the point of initialisation a some function call time.  

Nicholas

--
        "I reserve the right to contradict myself" 
n.lee with math.auckland.ac in nz
NO commercial email please! No SPAM for me.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
  1998-07-10 21:04   ` Ulrich Drepper
@ 1998-07-11 14:02     ` Martin von Loewis
  1998-07-13  4:36     ` Nicholas Lee
       [not found]     ` <199807112057.WAA00409.cygnus.egcs@mira.isdn.cs.tu-berlin.de>
  2 siblings, 0 replies; 16+ messages in thread
From: Martin von Loewis @ 1998-07-11 14:02 UTC (permalink / raw)
  To: egcs

> This is a case for calling pthread_once which is associated with a
> quite high cost (the pthread_once_t is normally at least two words
> long).

Interesting. There is an additional problem in local static
initializers for C++: If initialization throws an exception,
initialization is considered incomplete, and should be re-tried on the
next call (of course, after destructors of subobjects have been
called). 

So pthread_once doesn't really work here: We can't have once routines
that fail. Instead, we would need to duplicate glibc pthread_once with
a mutex and a condition, except that it restores the once_t to NEVER
instead of DONE in case of an exception.

This would also simplify linking to non-threaded code: the libgcc2
__cpp_once function would just call the initializer in the
non-threaded case.

Regards,
Martin

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
       [not found] ` <199807102001.WAA01324.cygnus.egcs@mira.isdn.cs.tu-berlin.de>
@ 1998-07-10 21:04   ` Ulrich Drepper
  1998-07-11 14:02     ` Martin von Loewis
                       ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Ulrich Drepper @ 1998-07-10 21:04 UTC (permalink / raw)
  To: egcs

martin@mira.isdn.cs.tu-berlin.de (Martin von Loewis) writes:

> int f()
> {
>   static int i = get_some_value();
>   return i;
> }
> 
> If two threads call f concurrently, initialization might be performed
> twice. Since this can be handled on the application level in many
> cases, it is not considered a serious problem (by some).

This is a case for calling pthread_once which is associated with a
quite high cost (the pthread_once_t is normally at least two words
long).  Plus there is the problem of handling this case if no threaded
program is used.  (Note that it isn't possible to have a simple "not
done"/"done" flag since the second thread arriving must not return from
the get_some_value call until the first did and this requires
interaction with the thread package.  Busy waiting is no option since
the waiting thread might have a higher priority.)

I see this case as a real boundary case which probably shouldn't
handled at all and the use of non-const initializing of static
variables should be outlawed.

-- 
---------------.      drepper at gnu.org  ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com   `------------------------

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
  1998-07-10 13:03 Mike Stump
@ 1998-07-10 20:04 ` Ben Scherrey
  0 siblings, 0 replies; 16+ messages in thread
From: Ben Scherrey @ 1998-07-10 20:04 UTC (permalink / raw)
  To: Mike Stump; +Cc: egcs

Whoa! Someone said manual. Where do I get one?????
	...or is this some kind of sick joke?

	Ben Scherrey

Mike Stump wrote:
> If your truely interested in this topic, you can collect the
> information and put a new section into the manual...

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
  1998-07-09 13:20 Thomas Wagner
  1998-07-09 19:08 ` Jeffrey A Law
@ 1998-07-10 13:32 ` Martin von Loewis
       [not found] ` <199807102001.WAA01324.cygnus.egcs@mira.isdn.cs.tu-berlin.de>
  2 siblings, 0 replies; 16+ messages in thread
From: Martin von Loewis @ 1998-07-10 13:32 UTC (permalink / raw)
  To: honus; +Cc: egcs

> I haven't been able to find any information of the threadsafeness of C++
> with v1.0.3.  I know about the threadsafeness of the SGI STL, but what about
> internal compiler issues like exceptions.  Are they threadsafe?  If not,
> will they be in 1.1?

Exception handling is not thread-safe in 1.0, it will be in 1.1
(provided you have a supported thread library on your system).

There is a minor issue left: initialization of local static objects,
as in

int f()
{
  static int i = get_some_value();
  return i;
}

If two threads call f concurrently, initialization might be performed
twice. Since this can be handled on the application level in many
cases, it is not considered a serious problem (by some).

If get_some_value produces an exception, the code that g++ emits does
not conform with the standard, so this needs to be rewritten, anyway.
Anybody looking into it might consider thread-safety here, as well.

Regards,
Martin

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
@ 1998-07-10 13:03 Mike Stump
  1998-07-10 20:04 ` Ben Scherrey
  0 siblings, 1 reply; 16+ messages in thread
From: Mike Stump @ 1998-07-10 13:03 UTC (permalink / raw)
  To: egcs, honus

> Date: Thu, 09 Jul 1998 16:20:11 -0400
> From: Thomas Wagner <honus+egcs@cmu.edu>
> To: egcs@cygnus.com

> I haven't been able to find any information of the threadsafeness of
> C++ with v1.0.3.

EH is not thread safe in 1.0.3.  Use a snapshot, or 1.1.  Local static
objects are not thread safe in 1.0.3, nor in 1.1.

If your truely interested in this topic, you can collect the
information and put a new section into the manual...

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: threadsafeness at egcs1.0.3 and 1.1
  1998-07-09 13:20 Thomas Wagner
@ 1998-07-09 19:08 ` Jeffrey A Law
  1998-07-10 13:32 ` Martin von Loewis
       [not found] ` <199807102001.WAA01324.cygnus.egcs@mira.isdn.cs.tu-berlin.de>
  2 siblings, 0 replies; 16+ messages in thread
From: Jeffrey A Law @ 1998-07-09 19:08 UTC (permalink / raw)
  To: Thomas Wagner; +Cc: egcs

  In message < 2353046752.900001211@DUCAT.INI.CMU.EDU >you write:
  > internal compiler issues like exceptions.  Are they threadsafe?  If not,
  > will they be in 1.1?
Exceptions will be thread safe in egcs-1.1, but not egcs-1.0.3.
jeff

^ permalink raw reply	[flat|nested] 16+ messages in thread

* threadsafeness at egcs1.0.3 and 1.1
@ 1998-07-09 13:20 Thomas Wagner
  1998-07-09 19:08 ` Jeffrey A Law
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Thomas Wagner @ 1998-07-09 13:20 UTC (permalink / raw)
  To: egcs

I haven't been able to find any information of the threadsafeness of C++
with v1.0.3.  I know about the threadsafeness of the SGI STL, but what about
internal compiler issues like exceptions.  Are they threadsafe?  If not,
will they be in 1.1?

Thanks,
Tom
--
honus+@cmu.edu, Principal Software Engineer, the NetBill project
Information Networking Institute, Carnegie Mellon University
"Always there's that space between what you feel and what you do,
   and in that gap all human sadness lies." -George Rodrigue

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~1998-07-14 16:57 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-07-13 15:28 threadsafeness at egcs1.0.3 and 1.1 Mike Stump
1998-07-13 19:49 ` Ulrich Drepper
1998-07-14  0:08 ` Martin von Loewis
  -- strict thread matches above, loose matches on Subject: below --
1998-07-13 17:29 Mike Stump
1998-07-13 12:40 Mike Stump
1998-07-10 13:03 Mike Stump
1998-07-10 20:04 ` Ben Scherrey
1998-07-09 13:20 Thomas Wagner
1998-07-09 19:08 ` Jeffrey A Law
1998-07-10 13:32 ` Martin von Loewis
     [not found] ` <199807102001.WAA01324.cygnus.egcs@mira.isdn.cs.tu-berlin.de>
1998-07-10 21:04   ` Ulrich Drepper
1998-07-11 14:02     ` Martin von Loewis
1998-07-13  4:36     ` Nicholas Lee
1998-07-13  8:40       ` Ulrich Drepper
1998-07-14 16:57         ` Nicholas Lee
     [not found]     ` <199807112057.WAA00409.cygnus.egcs@mira.isdn.cs.tu-berlin.de>
1998-07-13  6:39       ` Ulrich Drepper

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).