base_shared_ptr.hh

Go to the documentation of this file.
00001 /*
00008  * LEGAL:   COPYRIGHT (C) JIM E. BROOKS 2007
00009  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00010  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00011  *****************************************************************************/
00012 
00303 
00304 #ifndef BASE_SHARED_PTR_HH
00305 #define BASE_SHARED_PTR_HH 1
00306 
00307 #include "base_common.hh"
00308 
00309 namespace base {
00310 
00312 // Shared
00313 //
00314 // Clients can just derive using "public Shared" since mRefCnt is private.
00315 // "private Shared" is ideal but every client would have to declare SharedPtr a friend.
00316 //
00317 class Shared
00318 {
00319 public:
00320     uint GetRefCnt( void ) const { CHECK_TYPESIG(this,TYPESIG_SHARED);
00321                                    return mRefCnt; }
00325     void IncRefCnt( void ) { ++mRefCnt; }
00326 
00327 protected:
00328     // Shared initially has zero references.
00329     // Copy ctor makes a copy also with zero references.
00330     Shared( void )              : mRefCnt(0) { SET_TYPESIG(this,TYPESIG_SHARED); }
00331     Shared( const Shared& src ) : mRefCnt(0) { SET_TYPESIG(this,TYPESIG_SHARED); }
00332     ~Shared() { INVALIDATE_TYPESIG(this,TYPESIG_SHARED); }
00333 
00334     // operator=() is explicitly defined as NOP (compiler would copy ref-cnts).
00335     // Reassigning the value of a Shared should not affect ref-cnts.
00336     Shared& operator=( const Shared& src ) { return *this; }
00337 
00338 private:
00339     mutable int mRefCnt;
00340 
00341 public:
00342     
00343 
00344 template<typename SHARED> friend class SharedPtr;
00345 template<typename SHARED> friend class SharedPtrNull;
00346 template<typename SHARED> friend class SharedPtrLocked;
00347 template<typename SHARED> friend class SharedPtrNullLocked;
00348 };
00349 
00350 // A faux-pas for the sake of finer-granularity is to put the lock inside the Shared class.
00351 // That was tried but it lead to having to acquire two locks in the copy methods which
00352 // caused potential deadlock and code would've become convoluted to avoid deadlock.
00353 #if COMPILE_THREADS
00354 namespace restrict { BASE_GLOBAL FastLock gLockShared; }
00355 #endif
00356 
00357 // SharedPtr methods contain the macro TEST_SHARED_PTR_YIELD_CPU()
00358 // to help better test lock contention.
00359 #ifndef TEST_SHARED_PTR_YIELD_CPU
00360 #define TEST_SHARED_PTR_YIELD_CPU()
00361 #endif
00362 
00370 #define SharedPtr SharedPtr
00371 #define SharedPtr0 SharedPtrNull
00372 
00373 // Explicit long names used by tests.
00374 #define SharedPtrNoLock     SharedPtr
00375 #define SharedPtrNullNoLock SharedPtrNull
00376 
00377 // SharedPtr<FastLock>
00378 #define SharedPtr  SharedPtrLocked
00379 #define THREADABLE  Threadable
00380 #define IF_NOT_NULL(PTR)
00381 #define SHARED_PTR_NULL  0
00382 #define SHARED_PTR_LOCK()   THREAD_CODE( restrict::gLockShared.Lock() )
00383 #define SHARED_PTR_UNLOCK() THREAD_CODE( restrict::gLockShared.Unlock() )
00384 #include "base_shared_ptr2.hh"
00385 #undef SharedPtr
00386 #undef THREADABLE
00387 #undef IF_NOT_NULL
00388 #undef SHARED_PTR_NULL
00389 #undef SHARED_PTR_LOCK
00390 #undef SHARED_PTR_UNLOCK
00391 
00392 // SharedPtrNull<FastLock>
00393 #define SharedPtr  SharedPtrNullLocked
00394 #define THREADABLE  Threadable
00395 #define IF_NOT_NULL(PTR)  if ( EX( (PTR) != (NULL) ) )
00396 #define SHARED_PTR_NULL  1
00397 #define SHARED_PTR_LOCK()   THREAD_CODE( restrict::gLockShared.Lock() )
00398 #define SHARED_PTR_UNLOCK() THREAD_CODE( restrict::gLockShared.Unlock() )
00399 #include "base_shared_ptr2.hh"
00400 #undef SharedPtr
00401 #undef THREADABLE
00402 #undef IF_NOT_NULL
00403 #undef SHARED_PTR_NULL
00404 #undef SHARED_PTR_LOCK
00405 #undef SHARED_PTR_UNLOCK
00406 
00407 // SharedPtr<NoLock>
00408 #define SharedPtr  SharedPtr
00409 #define THREADABLE  NonThreadable
00410 #define IF_NOT_NULL(PTR)
00411 #define SHARED_PTR_NULL  0
00412 #define SHARED_PTR_LOCK()
00413 #define SHARED_PTR_UNLOCK()
00414 #include "base_shared_ptr2.hh"
00415 #undef SharedPtr
00416 #undef THREADABLE
00417 #undef IF_NOT_NULL
00418 #undef SHARED_PTR_NULL
00419 #undef SHARED_PTR_LOCK
00420 #undef SHARED_PTR_UNLOCK
00421 
00422 // SharedPtrNull<NoLock>
00423 #define SharedPtr  SharedPtrNull
00424 #define THREADABLE  NonThreadable
00425 #define IF_NOT_NULL(PTR)  if ( EX( (PTR) != (NULL) ) )
00426 #define SHARED_PTR_NULL  1
00427 #define SHARED_PTR_LOCK()
00428 #define SHARED_PTR_UNLOCK()
00429 #include "base_shared_ptr2.hh"
00430 #undef SharedPtr
00431 #undef THREADABLE
00432 #undef IF_NOT_NULL
00433 #undef SHARED_PTR_NULL
00434 #undef SHARED_PTR_LOCK
00435 #undef SHARED_PTR_UNLOCK
00436 
00437 } // namespace base
00438 
00439 #endif // BASE_SHARED_PTR_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:06 2007