Field3D
|
00001 //----------------------------------------------------------------------------// 00002 00003 /* 00004 * Copyright (c) 2009 Sony Pictures Imageworks Inc 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in the 00016 * documentation and/or other materials provided with the 00017 * distribution. Neither the name of Sony Pictures Imageworks nor the 00018 * names of its contributors may be used to endorse or promote 00019 * products derived from this software without specific prior written 00020 * permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00029 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00031 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00032 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 00033 * OF THE POSSIBILITY OF SUCH DAMAGE. 00034 */ 00035 00036 //----------------------------------------------------------------------------// 00037 00042 //----------------------------------------------------------------------------// 00043 00044 #ifndef _INCLUDED_Field3D_REF_COUNT_H_ 00045 #define _INCLUDED_Field3D_REF_COUNT_H_ 00046 00047 //#define FIELD3D_USE_ATOMIC_COUNT 00048 00049 //----------------------------------------------------------------------------// 00050 #include <boost/intrusive_ptr.hpp> 00051 00052 #ifdef FIELD3D_USE_ATOMIC_COUNT 00053 #include <boost/detail/atomic_count.hpp> 00054 #else 00055 #include <boost/thread/mutex.hpp> 00056 #endif 00057 00058 #include "ns.h" 00059 00060 FIELD3D_NAMESPACE_OPEN 00061 00062 //----------------------------------------------------------------------------// 00063 00064 class RefBase 00065 { 00066 public: 00067 00068 // Typedefs ------------------------------------------------------------------ 00069 00070 typedef boost::intrusive_ptr<RefBase> Ptr; 00071 00072 // Constructors -------------------------------------------------------------- 00073 00076 00077 RefBase() 00078 : m_counter(0) 00079 {} 00080 00082 RefBase(const RefBase&) 00083 : m_counter(0) 00084 {} 00085 00087 RefBase& operator= (const RefBase&) 00088 { return *this; } 00089 00091 virtual ~RefBase() 00092 {} 00093 00095 00097 size_t refcnt() 00098 { return m_counter; } 00099 00101 void ref() const 00102 { 00103 #ifndef FIELD3D_USE_ATOMIC_COUNT 00104 boost::mutex::scoped_lock lock(m_refMutex); 00105 #endif 00106 ++m_counter; 00107 } 00108 00110 void unref() const 00111 { 00112 #ifndef FIELD3D_USE_ATOMIC_COUNT 00113 boost::mutex::scoped_lock lock(m_refMutex); 00114 #endif 00115 --m_counter; 00116 // since we use intrusive_pointer no need 00117 // to delete the object ourselves. 00118 } 00119 00120 private: 00122 #ifdef FIELD3D_USE_ATOMIC_COUNT 00123 mutable boost::detail::atomic_count m_counter; 00124 #else 00125 mutable long m_counter; 00127 mutable boost::mutex m_refMutex; 00128 #endif 00129 00130 }; 00131 00132 //----------------------------------------------------------------------------// 00133 // Intrusive Pointer reference counting 00134 //----------------------------------------------------------------------------// 00135 00136 inline void 00137 intrusive_ptr_add_ref(RefBase* r) 00138 { 00139 r->ref(); 00140 } 00141 00142 //----------------------------------------------------------------------------// 00143 00144 inline void 00145 intrusive_ptr_release(RefBase* r) 00146 { 00147 r->unref(); 00148 00149 if (r->refcnt() == 0) 00150 delete r; 00151 } 00152 00153 //----------------------------------------------------------------------------// 00154 00155 FIELD3D_NAMESPACE_HEADER_CLOSE 00156 00157 //----------------------------------------------------------------------------// 00158 00159 #endif // Include guard 00160