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.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : BasicGenericDestructor.cxx
25 // Author : Antoine YESSAYAN, Paul RASCLE, EDF
33 #include "BasicsGenericDestructor.hxx"
39 std::list<PROTECTED_DELETE*> PROTECTED_DELETE::_objList;
41 pthread_mutex_t PROTECTED_DELETE::_listMutex;
43 pthread_mutex_t PROTECTED_DELETE::_listMutex =
44 PTHREAD_MUTEX_INITIALIZER;
47 std::list<GENERIC_DESTRUCTOR*> *GENERIC_DESTRUCTOR::Destructors = 0;
48 static bool atExitSingletonDone = false ;
50 // ============================================================================
52 * deleteInstance deletes only once the object. Only object present on the
53 * static list of PROTECTED_DELETE* are deleted, then removed of the list.
54 * The operation is protected by a mutex.
56 // ============================================================================
58 void PROTECTED_DELETE::deleteInstance(PROTECTED_DELETE *anObject)
60 if (std::find(_objList.begin(), _objList.end(),anObject) == _objList.end())
65 ret = pthread_mutex_lock(&_listMutex); // acquire lock, an check again
66 if (std::find(_objList.begin(), _objList.end(), anObject)
69 DEVTRACE("PROTECTED_DELETE::deleteInstance1 " << anObject);
71 DEVTRACE("PROTECTED_DELETE::deleteInstance2 " << &_objList);
72 _objList.remove(anObject);
74 ret = pthread_mutex_unlock(&_listMutex); // release lock
78 // ============================================================================
80 * To allow a further destruction of a PRTECTED_DELETE object, it must be added
81 * to the static list of PROTECTED_DELETE*
83 // ============================================================================
85 void PROTECTED_DELETE::addObj(PROTECTED_DELETE *anObject)
87 DEVTRACE("PROTECTED_DELETE::addObj " << anObject);
88 _objList.push_back(anObject);
91 // ============================================================================
93 * Herited classes have there own destructors
95 // ============================================================================
97 PROTECTED_DELETE::~PROTECTED_DELETE()
99 DEVTRACE("PROTECTED_DELETE::~PROTECTED_DELETE()");
102 // ============================================================================
104 * To execute only once GENERIC_DESTRUCTOR::HouseKeeping et the end of process,
105 * a dedicated object is created, as a singleton: atExitSingleton.
106 * When the singleton is created, the HouseKeeping() function is registered in
108 * Destructors is a list created on heap, and deleted by HouseKeeping(), with
111 // ============================================================================
113 class atExitSingleton
116 atExitSingleton(bool Make_ATEXIT)
118 if (Make_ATEXIT && !atExitSingletonDone)
120 DEVTRACE("atExitSingleton(" << Make_ATEXIT << ")");
121 assert(GENERIC_DESTRUCTOR::Destructors == 0);
122 GENERIC_DESTRUCTOR::Destructors = new std::list<GENERIC_DESTRUCTOR*>;
123 int cr = atexit(HouseKeeping);
125 atExitSingletonDone = true;
131 DEVTRACE("atExitSingleton::~atExitSingleton()");
135 //! static singleton for atExitSingleton class
137 static atExitSingleton HouseKeeper = atExitSingleton(false);
139 // ============================================================================
141 * Executes all objects of type DESTRUCTOR_OF in the Destructors list.
142 * Deletes all objects of type DESTRUCTOR_OF in the Destructors list.
145 // ============================================================================
147 void HouseKeeping( void )
149 DEVTRACE("HouseKeeping()");
150 assert(GENERIC_DESTRUCTOR::Destructors);
151 if(GENERIC_DESTRUCTOR::Destructors->size())
153 std::list<GENERIC_DESTRUCTOR*>::iterator it =
154 GENERIC_DESTRUCTOR::Destructors->end();
159 GENERIC_DESTRUCTOR* ptr = *it ;
160 DEVTRACE("HouseKeeping() " << typeid(ptr).name());
164 while (it != GENERIC_DESTRUCTOR::Destructors->begin()) ;
166 DEVTRACE("HouseKeeping() end list ");
167 GENERIC_DESTRUCTOR::Destructors->clear() ;
168 assert(GENERIC_DESTRUCTOR::Destructors->size() == 0);
169 assert(GENERIC_DESTRUCTOR::Destructors->empty());
170 DEVTRACE("HouseKeeping()after clear ");
173 delete GENERIC_DESTRUCTOR::Destructors;
174 GENERIC_DESTRUCTOR::Destructors = 0;
175 DEVTRACE("HouseKeeping() very end ");
179 // ============================================================================
181 * Adds a destruction object to the list of actions to be performed at the end
184 // ============================================================================
186 const int GENERIC_DESTRUCTOR::Add(GENERIC_DESTRUCTOR &anObject)
188 DEVTRACE("GENERIC_DESTRUCTOR::Add("<<typeid(anObject).name()<<") "
190 if (!atExitSingletonDone)
192 HouseKeeper = atExitSingleton(true);
195 Destructors->push_back(&anObject);
196 return Destructors->size();