Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / engine / RefCounter.cxx
1 //  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "RefCounter.hxx"
20
21 #include "Mutex.hxx"
22
23 //#define REFCNT
24
25 #include <iostream>
26 #include <sstream>
27
28 //#define _DEVDEBUG_
29 #include "YacsTrace.hxx"
30
31 using namespace YACS::ENGINE;
32
33 unsigned int RefCounter::_totalCnt=0;
34
35 static YACS::BASES::Mutex _globalMutexForTS;
36
37 void RefCounter::incrRef() const
38 {
39   _globalMutexForTS.lock();
40 #ifdef REFCNT
41   RefCounter::_totalCnt++;
42 #endif
43   _cnt++;
44   _globalMutexForTS.unlock();
45 }
46
47 bool RefCounter::decrRef()
48 {
49   _globalMutexForTS.lock();
50 #ifdef REFCNT
51   RefCounter::_totalCnt--;
52 #endif
53   bool ret=(--_cnt==0);
54   _globalMutexForTS.unlock();
55   if(ret)
56     delete this;
57   return ret;
58 }
59
60 RefCounter::RefCounter():_cnt(1)
61 {
62 #ifdef REFCNT
63   RefCounter::_totalCnt++;
64 #endif
65 }
66
67 RefCounter::~RefCounter()
68 {
69 #ifdef REFCNT
70   if(_cnt > 0)
71     {
72       DEBTRACE("Ref count > 0: " << this << " " << _cnt);
73       AttachDebugger();
74     }
75 #endif
76 }