Re: ick, ick -- grossness in linux kernel

From: David Gay (dgay@EECS.Berkeley.EDU)
Date: 10/09/00


From: David Gay <dgay@EECS.Berkeley.EDU>
Message-Id: <200010092256.PAA16357@barnowl.CS.berkeley.EDU>
Subject: Re: ick, ick -- grossness in linux kernel 
Date: Mon, 09 Oct 2000 15:56:54 -0700


> 		do { __asm__ __volatile__("pushfl ; popl %0 ; cli":"=g" (
> flags  ):   :"memory") ;       spin_lock( &tqueue_lock ); } while (0) ;
> 
> Currently my lock checker complains about this code, because it thinks
> that the spin_lock may get executed multiple times (which would be a Bad
> Thing).
> 
> Anyone know why this code gets wrapped in a useless do loop?  I assume
> it's to fix some bug in some version of gcc.

No, it's a standard C hack to get a macro to baheve more like a real
function: if it was just 

#define FOO() { stuff; }

then

  if (x) FOO();
  else baz();

would not behave as expected (you would get a syntax error).

With 

#define FOO() do { stuff; } while (0)

Note that the other standard form for this is something like

#define FOO() if (1) { stuff; } else 

I think you might want to constant fold if and while conditions to avoid
surprises like this.

David



This archive was generated by hypermail 2b30 : 11/04/02 PST