00001 /*MT* 00002 00003 MediaTomb - http://www.mediatomb.cc/ 00004 00005 singleton.cc - this file is part of MediaTomb. 00006 00007 Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>, 00008 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc> 00009 00010 Copyright (C) 2006-2010 Gena Batyan <bgeradz@mediatomb.cc>, 00011 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>, 00012 Leonhard Wimmer <leo@mediatomb.cc> 00013 00014 MediaTomb is free software; you can redistribute it and/or modify 00015 it under the terms of the GNU General Public License version 2 00016 as published by the Free Software Foundation. 00017 00018 MediaTomb is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 GNU General Public License for more details. 00022 00023 You should have received a copy of the GNU General Public License 00024 version 2 along with MediaTomb; if not, write to the Free Software 00025 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00026 00027 $Id: singleton.cc 2081 2010-03-23 20:18:00Z lww $ 00028 */ 00029 00031 00032 #ifdef HAVE_CONFIG_H 00033 #include "autoconfig.h" 00034 #endif 00035 00036 #include "singleton.h" 00037 00038 using namespace zmm; 00039 00040 Ref<SingletonManager> SingletonManager::instance = nil; 00041 Ref<Mutex> SingletonManager::mutex = Ref<Mutex>(new Mutex()); 00042 00043 Ref<SingletonManager> SingletonManager::getInstance() 00044 { 00045 if (instance == nil) 00046 { 00047 AUTOLOCK(mutex); 00048 if (instance == nil) // check again, because there is a very small chance 00049 // that 2 threads tried to lock() concurrently 00050 { 00051 instance = zmm::Ref<SingletonManager>(new SingletonManager()); 00052 } 00053 } 00054 return instance; 00055 } 00056 00057 SingletonManager::SingletonManager() : Object() 00058 { 00059 singletonStack = Ref<ObjectStack<Singleton<Object> > >(new ObjectStack<Singleton<Object> >(SINGLETON_CUR_MAX)); 00060 } 00061 00062 void SingletonManager::registerSingleton(Ref<Singleton<Object> > object) 00063 { 00064 AUTOLOCK(mutex); 00065 #ifdef TOMBDEBUG 00066 if (singletonStack->size() >= SINGLETON_CUR_MAX) 00067 { 00068 printf("%d singletons are active (SINGLETON_CUR_MAX=%d) and tried to add another singleton - check this!\n", singletonStack->size(), SINGLETON_CUR_MAX); 00069 print_backtrace(); 00070 abort(); 00071 } 00072 #endif 00073 log_debug("registering new singleton... - %d -> %d\n", singletonStack->size(), singletonStack->size() + 1); 00074 singletonStack->push(object); 00075 } 00076 00077 void SingletonManager::shutdown(bool complete) 00078 { 00079 log_debug("start (%d objects)\n", singletonStack->size()); 00080 AUTOLOCK(mutex); 00081 00082 Ref<ObjectStack<Singleton<Object> > > singletonStackReactivate(new ObjectStack<Singleton<Object> >(SINGLETON_CUR_MAX)); 00083 00084 Ref<Singleton<Object> > object; 00085 while((object = singletonStack->pop()) != nil) 00086 { 00087 //log_debug("destoying... \n"); 00088 //_print_backtrace(stdout); 00089 object->shutdown(); 00090 object->inactivateSingleton(); 00091 singletonStackReactivate->push(object); 00092 //object->destroyMutex(); 00093 } 00094 while((object = singletonStackReactivate->pop()) != nil) 00095 object->reactivateSingleton(); 00096 if (complete && instance != nil) 00097 instance = nil; 00098 log_debug("end\n"); 00099 }
1.6.1