| 1 |
/* |
|---|
| 2 |
* Copyright (C) 2005-2006 Alo Sarv <madcat_@users.sourceforge.net> |
|---|
| 3 |
* |
|---|
| 4 |
* This program is free software; you can redistribute it and/or modify |
|---|
| 5 |
* it under the terms of the GNU General Public License as published by |
|---|
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 |
* (at your option) any later version. |
|---|
| 8 |
* |
|---|
| 9 |
* This program is distributed in the hope that it will be useful, |
|---|
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 |
* GNU General Public License for more details. |
|---|
| 13 |
* |
|---|
| 14 |
* You should have received a copy of the GNU General Public License |
|---|
| 15 |
* along with this program; if not, write to the Free Software |
|---|
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 17 |
*/ |
|---|
| 18 |
|
|---|
| 19 |
#ifndef __THREADSAFE_PTRS_H__ |
|---|
| 20 |
#define __THREADSAFE_PTRS_H__ |
|---|
| 21 |
|
|---|
| 22 |
#include <hnbase/osdep.h> |
|---|
| 23 |
#include <boost/thread.hpp> |
|---|
| 24 |
#include <map> |
|---|
| 25 |
|
|---|
| 26 |
namespace boost { |
|---|
| 27 |
MSVC_ONLY(extern "C" {) |
|---|
| 28 |
extern HNBASE_EXPORT std::map<void*, uint64_t> g_tsRefCounter; |
|---|
| 29 |
extern HNBASE_EXPORT boost::recursive_mutex g_tsRefCounterMutex; |
|---|
| 30 |
MSVC_ONLY(}) |
|---|
| 31 |
|
|---|
| 32 |
template<typename T> |
|---|
| 33 |
inline void updateRefs(T *t, bool add) { |
|---|
| 34 |
boost::recursive_mutex::scoped_lock l(g_tsRefCounterMutex); |
|---|
| 35 |
if (add) { |
|---|
| 36 |
++g_tsRefCounter[t]; |
|---|
| 37 |
} else { |
|---|
| 38 |
std::map<void*, uint64_t>::iterator it( |
|---|
| 39 |
g_tsRefCounter.find(t) |
|---|
| 40 |
); |
|---|
| 41 |
assert(it->second); |
|---|
| 42 |
--it->second; |
|---|
| 43 |
if (!it->second) { |
|---|
| 44 |
delete reinterpret_cast<T*>(it->first); |
|---|
| 45 |
g_tsRefCounter.erase(it); |
|---|
| 46 |
} |
|---|
| 47 |
} |
|---|
| 48 |
} |
|---|
| 49 |
template<typename T> |
|---|
| 50 |
inline void intrusive_ptr_add_ref(T *t) { |
|---|
| 51 |
updateRefs(t, true); |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
template<typename T> |
|---|
| 55 |
inline void intrusive_ptr_release(T *t) { |
|---|
| 56 |
updateRefs(t, false); |
|---|
| 57 |
} |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
#endif |
|---|