/*MT* MediaTomb - http://www.mediatomb.cc/ singleton.cc - this file is part of MediaTomb. Copyright (C) 2005 Gena Batyan , Sergey 'Jin' Bostandzhyan Copyright (C) 2006-2007 Gena Batyan , Sergey 'Jin' Bostandzhyan , Leonhard Wimmer MediaTomb is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. MediaTomb is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License version 2 along with MediaTomb; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. $Id: singleton.cc 1124 2007-02-17 21:49:15Z lww $ */ /// \file singleton.cc #ifdef HAVE_CONFIG_H #include "autoconfig.h" #endif #include "singleton.h" using namespace zmm; Ref SingletonManager::instance = nil; Ref SingletonManager::mutex = Ref(new Mutex()); Ref SingletonManager::getInstance() { if (instance == nil) { AUTOLOCK(mutex); if (instance == nil) // check again, because there is a very small chance // that 2 threads tried to lock() concurrently { instance = zmm::Ref(new SingletonManager()); } } return instance; } SingletonManager::SingletonManager() : Object() { singletonStack = Ref > >(new ObjectStack >(SINGLETON_CUR_MAX)); } void SingletonManager::registerSingleton(Ref > object) { AUTOLOCK(mutex); #ifdef LOG_TOMBDEBUG if (singletonStack->size() >= SINGLETON_CUR_MAX) { printf("%d singletons are active (SINGLETON_CUR_MAX=%d) and tried to add another singleton - check this!\n", singletonStack->size(), SINGLETON_CUR_MAX); print_backtrace(); abort(); } #endif log_debug("registering new singleton... - %d -> %d\n", singletonStack->size(), singletonStack->size() + 1); singletonStack->push(object); } void SingletonManager::shutdown(bool complete) { log_debug("start (%d objects)\n", singletonStack->size()); AUTOLOCK(mutex); Ref > > singletonStackReactivate(new ObjectStack >(SINGLETON_CUR_MAX)); Ref > object; while((object = singletonStack->pop()) != nil) { //log_debug("destoying... \n"); //_print_backtrace(stdout); object->shutdown(); object->inactivateSingleton(); singletonStackReactivate->push(object); //object->destroyMutex(); } while((object = singletonStackReactivate->pop()) != nil) object->reactivateSingleton(); if (complete && instance != nil) instance = nil; log_debug("end\n"); }