1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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, or (at your option) any later version.
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
23 // SALOME Basics : general SALOME definitions and tools (C++ part - no CORBA)
24 // File : BasicGenericDestructor.cxx
25 // Author : Antoine YESSAYAN, Paul RASCLE, EDF
33 #include "BasicsGenericDestructor.hxx"
37 std::list<PROTECTED_DELETE*> PROTECTED_DELETE::_objList;
39 pthread_mutex_t PROTECTED_DELETE::_listMutex;
41 pthread_mutex_t PROTECTED_DELETE::_listMutex =
42 PTHREAD_MUTEX_INITIALIZER;
45 std::list<GENERIC_DESTRUCTOR*> *GENERIC_DESTRUCTOR::Destructors = 0;
46 static bool atExitSingletonDone = false ;
48 // ============================================================================
50 * deleteInstance deletes only once the object. Only object present on the
51 * static list of PROTECTED_DELETE* are deleted, then removed of the list.
52 * The operation is protected by a mutex.
54 // ============================================================================
56 void PROTECTED_DELETE::deleteInstance(PROTECTED_DELETE *anObject)
58 if (std::find(_objList.begin(), _objList.end(),anObject) == _objList.end())
62 pthread_mutex_lock(&_listMutex); // acquire lock, an check again
63 if (std::find(_objList.begin(), _objList.end(), anObject)
66 DEVTRACE("PROTECTED_DELETE::deleteInstance1 " << anObject);
68 DEVTRACE("PROTECTED_DELETE::deleteInstance2 " << &_objList);
69 _objList.remove(anObject);
71 pthread_mutex_unlock(&_listMutex); // release lock
75 // ============================================================================
77 * To allow a further destruction of a PRTECTED_DELETE object, it must be added
78 * to the static list of PROTECTED_DELETE*
80 // ============================================================================
82 void PROTECTED_DELETE::addObj(PROTECTED_DELETE *anObject)
84 DEVTRACE("PROTECTED_DELETE::addObj " << anObject);
85 _objList.push_back(anObject);
88 // ============================================================================
90 * Herited classes have there own destructors
92 // ============================================================================
94 PROTECTED_DELETE::~PROTECTED_DELETE()
96 DEVTRACE("PROTECTED_DELETE::~PROTECTED_DELETE()");
99 // ============================================================================
101 * To execute only once GENERIC_DESTRUCTOR::HouseKeeping et the end of process,
102 * a dedicated object is created, as a singleton: atExitSingleton.
103 * When the singleton is created, the HouseKeeping() function is registered in
105 * Destructors is a list created on heap, and deleted by HouseKeeping(), with
108 // ============================================================================
110 class atExitSingleton
113 atExitSingleton(bool Make_ATEXIT)
115 if (Make_ATEXIT && !atExitSingletonDone)
117 DEVTRACE("atExitSingleton(" << Make_ATEXIT << ")");
118 assert(GENERIC_DESTRUCTOR::Destructors == 0);
119 GENERIC_DESTRUCTOR::Destructors = new std::list<GENERIC_DESTRUCTOR*>;
121 atexit(HouseKeeping);
123 int cr = atexit(HouseKeeping);
126 atExitSingletonDone = true;
132 DEVTRACE("atExitSingleton::~atExitSingleton()");
136 //! static singleton for atExitSingleton class
138 static atExitSingleton HouseKeeper = atExitSingleton(false);
140 // ============================================================================
142 * Executes all objects of type DESTRUCTOR_OF in the Destructors list.
143 * Deletes all objects of type DESTRUCTOR_OF in the Destructors list.
146 // ============================================================================
148 void HouseKeeping( void )
150 DEVTRACE("HouseKeeping()");
151 assert(GENERIC_DESTRUCTOR::Destructors);
152 if(GENERIC_DESTRUCTOR::Destructors->size())
154 std::list<GENERIC_DESTRUCTOR*>::iterator it =
155 GENERIC_DESTRUCTOR::Destructors->end();
160 GENERIC_DESTRUCTOR* ptr = *it ;
161 DEVTRACE("HouseKeeping() " << typeid(ptr).name());
165 while (it != GENERIC_DESTRUCTOR::Destructors->begin()) ;
167 DEVTRACE("HouseKeeping() end list ");
168 GENERIC_DESTRUCTOR::Destructors->clear() ;
169 assert(GENERIC_DESTRUCTOR::Destructors->size() == 0);
170 assert(GENERIC_DESTRUCTOR::Destructors->empty());
171 DEVTRACE("HouseKeeping()after clear ");
174 delete GENERIC_DESTRUCTOR::Destructors;
175 GENERIC_DESTRUCTOR::Destructors = 0;
176 atExitSingletonDone = false ;
177 DEVTRACE("HouseKeeping() very end ");
181 // ============================================================================
183 * Adds a destruction object to the list of actions to be performed at the end
186 // ============================================================================
188 int GENERIC_DESTRUCTOR::Add(GENERIC_DESTRUCTOR &anObject)
190 DEVTRACE("GENERIC_DESTRUCTOR::Add("<<typeid(anObject).name()<<") "
192 if (!atExitSingletonDone)
194 HouseKeeper = atExitSingleton(true);
197 Destructors->push_back(&anObject);
198 return (int)Destructors->size();