At 11:44 AM 12/05/2001, Brian Pane wrote:
Aaron Bannert wrote:

On Wed, Dec 05, 2001 at 12:35:00PM +0100, Sander Striker wrote:

<comment>
Ack.  No.  Declare a new scope like so:
#define UNLOCK(mutex) \
 do { \
   if (mutex) \
       apr_thread_mutex_unlock(mutex); \
 } while(0)

Calling it like UNLOCK(mutex) (i.e. no semicolon) is just badness
and will shoot us down the road later.
</comment>
Ok, will change that.

If the semicolon is the only problem, just remove the semicolon from the macro:

#define UNLOCK(mutex) \
   if (mutex) \
       apr_thread_mutex_unlock(mutex)

I recommend still creating a scope around the if-statement: #define UNLOCK(mutex) \ { \ if (mutex) \ apr_thread_mutex_unlock(mutex); \ }

That will guard against unintended effects when the macro
is used in code like this:

   if (condition)
     UNLOCK(mutex)
   else
     ...

That will fail if it's called like this:

if (condition)
    UNLOCK(mutex);
else
    ...

since it will expand to:

if (condition)
    {
    if (mutex)
        apr_thread_mutex_unlock(mutex);
    };
else
    ...

Note the ; after the close brace, that makes the else illegal.

The only truly safe construct that I've seen is the do/while recommended above.

--
Greg Marr
[EMAIL PROTECTED]
"We thought you were dead."
"I was, but I'm better now." - Sheridan, "The Summoning"



Reply via email to