Salome HOME
Copyright update 2022
[modules/yacs.git] / src / engine / RefCounter.cxx
1 // Copyright (C) 2006-2022  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, or (at your option) any later version.
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
20 #include "RefCounter.hxx"
21
22 #include "Mutex.hxx"
23
24 //#define REFCNT
25
26 #include <iostream>
27 #include <sstream>
28
29 //#define _DEVDEBUG_
30 #include "YacsTrace.hxx"
31
32 using namespace YACS::ENGINE;
33
34 unsigned int RefCounter::_totalCnt=0;
35
36 static YACS::BASES::Mutex _globalMutexForTS;
37
38 void RefCounter::incrRef() const
39 {
40   _globalMutexForTS.lock();
41 #ifdef REFCNT
42   RefCounter::_totalCnt++;
43 #endif
44   _cnt++;
45   _globalMutexForTS.unLock();
46 }
47
48 bool RefCounter::decrRef() const
49 {
50   _globalMutexForTS.lock();
51 #ifdef REFCNT
52   RefCounter::_totalCnt--;
53 #endif
54   bool ret=(--_cnt==0);
55   _globalMutexForTS.unLock();
56   if(ret)
57     delete this;
58   return ret;
59 }
60
61 RefCounter::RefCounter():_cnt(1)
62 {
63 #ifdef REFCNT
64   RefCounter::_totalCnt++;
65 #endif
66 }
67
68 RefCounter::RefCounter(const RefCounter& other):_cnt(1)
69 {
70 }
71
72 RefCounter::~RefCounter()
73 {
74 #ifdef REFCNT
75   if(_cnt > 0)
76     {
77       DEBTRACE("Ref count > 0: " << this << " " << _cnt);
78       AttachDebugger();
79     }
80 #endif
81 }