discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: macros


From: Richard Frith-Macdonald
Subject: Re: macros
Date: Fri, 23 Feb 2001 15:58:00 +0000

On Friday, February 23, 2001, at 02:36 PM, David Relson wrote:


Helge has already answered mostly ... but I want to clarify a point -

> While looking at the macros and their usage, I did notice an oddity:  In  
> approx 6 modules, both AUTORELEASE and RETAIN are used.  For example in  
> NSTimeZone.m, the following lines appear: 
>  
>          zone = AUTORELEASE(RETAIN(defaultTimeZone)); 
>          zone = AUTORELEASE(RETAIN(systemTimeZone)); 
>  
> These seem really odd.  First, using both RETAIN and AUTORELEASE gives an  
> expensive NOP - their effects cancel one another (in an involved,  
> time-consuming manner).

The reason for this is generally thread safety - you will almost always see
this sort of thing in lock protected regions of code.

The idea is that the code fragment that owns the object retains it and adds
it to the autorelease pool before it unlocks a lock permitting other threads
to access the object.  That way if another thread releases the object, it
doesn't matter because it's still retained and in the autorelease pool of
the first thread, and will not go away until the autorelease pool is 
deallocated.

In some sections of code you may find code like - 

if (isMultiThreaded == YES)
  {
    AUTORELEASE(RETAIN(object));
    [myLock unlock];
  }
return object;

Where, for efficiency in unthreaded code, the extra work involved in making 
things
thread-safe has been avoided.

Ideally all the gnustep-base code would be like that, but as it's long-winded to
write, the reality falls a little short, and unthreaded code is not as much
faster than threaded code as it could be.

  


reply via email to

[Prev in Thread] Current Thread [Next in Thread]