RE: Is gcc thread-unsafe?

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Andrew Haley <aph@...>
Cc: Linux Kernel Mailing List <linux-kernel@...>, Tomash Brechko <tomash.brechko@...>
Date: Friday, November 2, 2007 - 1:18 pm

> Another conclusion from the cited text is that in contrast with what

It all depends upon what threading standard you are using. If GCC is going
to support POSIX threading, it cannot require that thread-shared data be
marked 'volatile' since POSIX does not require this.

It can offer semantic guarantees for volatile-qualified data if it wants to.
But POSIX provides a set of guarantees that do not require marking data as
'volatile' and if GCC is going to support POSIX threading, it has to support
providing those guarantees.

As far as I know, no threading standard either requires 'volatile' or states
that it is sufficient for any particular purpose. So there seems to be no
reason to declare thread-shared variables as
volatile except as some kind of platform-specific optimization.

POSIX mutexes are sufficient. They are necessary if there is no other way to
get the guarantees you need. Nothing prevents GCC from providing any
guarantees it wants for 'volatile' qualified data. But POSIX mutexes must
work as POSIX specifies or GCC cannot support POSIX threading.

This is the nightmare scenario (thanks to Hans-J. Boehm):

int x;
bool need_to_lock;
pthread_mutex_t mutex;

for(int i=0; i<50; i++)
{
 if(unlikely(need_to_lock)) pthread_mutex_lock(&mutex);
 x++;
 if(unlikely(need_to_lock)) pthread_mutex_unlock(&mutex);
}

Now suppose the compiler optimizes this as follows:

register=x;
for(int i=0; i<50; i++)
{
 if(need_to_lock)
 {
  x=register; pthread_mutex_lock(&mutex) register=x;
 }
 register++;
 if(need_to_lock)
 {
  x=register; pthread_mutex_unlock(&mutex); register=x;
 }
}
x=register;

This is a perfectly legal optimization for single-threaded code. It may in
fact be an actual optimization. Clearly, it totally destroys threaded code.

This shows that, unfortunately, the normal assumption that not knowing
anything about the pthread functions ensures that optimizations won't break
them is incorrect.

DS


-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: Is gcc thread-unsafe?, Bart Van Assche, (Fri Oct 26, 10:40 am)
Re: Is gcc thread-unsafe?, Linus Torvalds, (Fri Oct 26, 11:27 am)
Re: Is gcc thread-unsafe?, Linus Torvalds, (Fri Oct 26, 12:28 pm)
Re: Is gcc thread-unsafe?, Bart Van Assche, (Fri Oct 26, 1:07 pm)
Re: Is gcc thread-unsafe?, Alan Cox, (Fri Oct 26, 2:08 pm)
Re: Is gcc thread-unsafe?, Andi Kleen, (Fri Oct 26, 4:39 pm)
Re: Is gcc thread-unsafe?, Linus Torvalds, (Fri Oct 26, 2:14 pm)
Re: Is gcc thread-unsafe?, Andrew Haley, (Fri Oct 26, 1:12 pm)
Re: Is gcc thread-unsafe?, Linus Torvalds, (Fri Oct 26, 1:25 pm)
Re: Is gcc thread-unsafe?, Linus Torvalds, (Fri Oct 26, 11:09 am)
Re: Is gcc thread-unsafe?, Giacomo Catenazzi, (Fri Oct 26, 5:45 pm)
Re: Is gcc thread-unsafe?, Linus Torvalds, (Fri Oct 26, 6:24 pm)
Re: Is gcc thread-unsafe?, Andrew Haley, (Fri Oct 26, 11:34 am)
RE: Is gcc thread-unsafe?, David Schwartz, (Fri Oct 26, 2:06 pm)
RE: Is gcc thread-unsafe?, Andrew Haley, (Tue Oct 30, 6:20 am)
Re: Is gcc thread-unsafe?, Bart Van Assche, (Fri Nov 2, 11:29 am)
RE: Is gcc thread-unsafe?, David Schwartz, (Fri Nov 2, 1:18 pm)
Re: Is gcc thread-unsafe?, Andrew Haley, (Fri Nov 2, 11:38 am)
Re: Is gcc thread-unsafe?, Bart Van Assche, (Sun Nov 4, 11:13 am)
Re: Is gcc thread-unsafe?, Linus Torvalds, (Sun Nov 4, 1:45 pm)
Re: Is gcc thread-unsafe?, Bart Van Assche, (Sun Nov 4, 2:06 pm)
Re: Is gcc thread-unsafe?, Andrew Haley, (Sun Nov 4, 1:58 pm)