1 // SALOME Basics : general SALOME definitions and tools (C++ part - no CORBA)
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : BasicGenericDestructor.cxx
25 // Author : Antoine YESSAYAN, Paul RASCLE, EDF
33 #include "BasicsGenericDestructor.hxx"
39 std::list<PROTECTED_DELETE*> PROTECTED_DELETE::_objList;
40 pthread_mutex_t PROTECTED_DELETE::_listMutex;
42 std::list<GENERIC_DESTRUCTOR*> *GENERIC_DESTRUCTOR::Destructors = 0;
43 static bool atExitSingletonDone = false ;
45 // ============================================================================
47 * deleteInstance deletes only once the object. Only object present on the
48 * static list of PROTECTED_DELETE* are deleted, then removed of the list.
49 * The operation is protected by a mutex.
51 // ============================================================================
53 void PROTECTED_DELETE::deleteInstance(PROTECTED_DELETE *anObject)
55 if (std::find(_objList.begin(), _objList.end(),anObject) == _objList.end())
60 ret = pthread_mutex_lock(&_listMutex); // acquire lock, an check again
61 if (std::find(_objList.begin(), _objList.end(), anObject)
64 DEVTRACE("PROTECTED_DELETE::deleteInstance1 " << anObject);
66 DEVTRACE("PROTECTED_DELETE::deleteInstance2 " << &_objList);
67 _objList.remove(anObject);
69 ret = pthread_mutex_unlock(&_listMutex); // release lock
73 // ============================================================================
75 * To allow a further destruction of a PRTECTED_DELETE object, it must be added
76 * to the static list of PROTECTED_DELETE*
78 // ============================================================================
80 void PROTECTED_DELETE::addObj(PROTECTED_DELETE *anObject)
82 DEVTRACE("PROTECTED_DELETE::addObj " << anObject);
83 _objList.push_back(anObject);
86 // ============================================================================
88 * Herited classes have there own destructors
90 // ============================================================================
92 PROTECTED_DELETE::~PROTECTED_DELETE()
94 DEVTRACE("PROTECTED_DELETE::~PROTECTED_DELETE()");
97 // ============================================================================
99 * To execute only once GENERIC_DESTRUCTOR::HouseKeeping et the end of process,
100 * a dedicated object is created, as a singleton: atExitSingleton.
101 * When the singleton is created, the HouseKeeping() function is registered in
103 * Destructors is a list created on heap, and deleted by HouseKeeping(), with
106 // ============================================================================
108 class atExitSingleton
111 atExitSingleton(bool Make_ATEXIT)
113 if (Make_ATEXIT && !atExitSingletonDone)
115 DEVTRACE("atExitSingleton(" << Make_ATEXIT << ")");
116 assert(GENERIC_DESTRUCTOR::Destructors == 0);
117 GENERIC_DESTRUCTOR::Destructors = new std::list<GENERIC_DESTRUCTOR*>;
118 int cr = atexit(HouseKeeping);
120 atExitSingletonDone = true;
126 DEVTRACE("atExitSingleton::~atExitSingleton()");
130 //! static singleton for atExitSingleton class
132 static atExitSingleton HouseKeeper = atExitSingleton(false);
134 // ============================================================================
136 * Executes all objects of type DESTRUCTOR_OF in the Destructors list.
137 * Deletes all objects of type DESTRUCTOR_OF in the Destructors list.
140 // ============================================================================
142 void HouseKeeping( void )
144 DEVTRACE("HouseKeeping()");
145 assert(GENERIC_DESTRUCTOR::Destructors);
146 if(GENERIC_DESTRUCTOR::Destructors->size())
148 std::list<GENERIC_DESTRUCTOR*>::iterator it =
149 GENERIC_DESTRUCTOR::Destructors->end();
154 GENERIC_DESTRUCTOR* ptr = *it ;
155 DEVTRACE("HouseKeeping() " << typeid(ptr).name());
159 while (it != GENERIC_DESTRUCTOR::Destructors->begin()) ;
161 DEVTRACE("HouseKeeping() end list ");
162 GENERIC_DESTRUCTOR::Destructors->clear() ;
163 assert(GENERIC_DESTRUCTOR::Destructors->size() == 0);
164 assert(GENERIC_DESTRUCTOR::Destructors->empty());
165 DEVTRACE("HouseKeeping()after clear ");
168 delete GENERIC_DESTRUCTOR::Destructors;
169 GENERIC_DESTRUCTOR::Destructors = 0;
170 DEVTRACE("HouseKeeping() very end ");
174 // ============================================================================
176 * Adds a destruction object to the list of actions to be performed at the end
179 // ============================================================================
181 const int GENERIC_DESTRUCTOR::Add(GENERIC_DESTRUCTOR &anObject)
183 DEVTRACE("GENERIC_DESTRUCTOR::Add("<<typeid(anObject).name()<<") "
185 if (!atExitSingletonDone)
187 HouseKeeper = atExitSingleton(true);
190 Destructors->push_back(&anObject);
191 return Destructors->size();