Re: Is gcc thread-unsafe?

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Bart Van Assche <bart.vanassche@...>
Cc: Linux Kernel Mailing List <linux-kernel@...>, Andrew Haley <aph@...>
Date: Friday, October 26, 2007 - 12:28 pm

On Fri, 26 Oct 2007, Linus Torvalds wrote:


Thinking some more about this, you really have two cases:

 - full functions taking/releasing locks (possibly conditionally, ie 
   with something lik etrylock and/or based on argument values). 

   You simply *cannot* require these to be marked, because the locking may 
   have been done indirectly. Yes, you can mark things like 
   "pthread_mutex_trylock()" as being an acquire-function, but the fact 
   is, users will then wrap these things in *other* functions, and return 
   their return values.

   Ergo: a compiler *must* assume that a function call that it 
   didn't inline involves locking. There's no point in adding some 
   gcc-specific attributes to system header files, because it's not going 
   to fix anything in any portable program.

 - inline assembly (together with, potentially, compiler primitives). 
   That's the only other way to reliably do locking from C. 

   This one gcc could certainly extend on. But would there really be any 
   upside? It would be easier/better to say that inline assembly (at least 
   if it clobbers memory or is volatile) has the same serialization issues 
   as a function call.

So the fact is, any compiler that turns

	if (conditional)
		x++;

into an unconditional write to x (where 'x' is potentially visible to the 
outside - global visibility or has had its address taken) is just broken. 

No ifs, buts or maybes about it. You simply cannot do that optimization, 
because there is no way for the compiler to know whether the conditional 
implies that you hold a lock or not.

				Linus
-
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)