1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2004, 2005, 2007 StatPro Italia srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program 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 license for more details.
*/
/*! \file singleton.hpp
\brief basic support for the singleton pattern
*/
#ifndef quantlib_singleton_hpp
#define quantlib_singleton_hpp
#include <ql/qldefines.hpp>
#ifdef QL_ENABLE_SINGLETON_THREAD_SAFE_INIT
#if defined(QL_ENABLE_SESSIONS)
#ifdef BOOST_MSVC
#pragma message(\
"Thread-safe singleton initialization not supported " \
"when sessions are enabled.")
#else
#warning \
Thread-safe singleton initialization not supported \
when sessions are enabled.
#endif
#else
#include <boost/atomic.hpp>
#include <boost/thread/mutex.hpp>
#if !defined(BOOST_ATOMIC_ADDRESS_LOCK_FREE)
#ifdef BOOST_MSVC
#pragma message(\
"Thread-safe singleton initialization " \
"may degrade performances.")
#else
#warning \
Thread-safe singleton initialization \
may degrade performances.
#endif
#endif
#define QL_SINGLETON_THREAD_SAFE_INIT
#endif
#endif
#include <ql/types.hpp>
#include <ql/shared_ptr.hpp>
#if defined(QL_PATCH_MSVC)
#pragma managed(push, off)
#endif
#include <boost/noncopyable.hpp>
#if defined(QL_PATCH_MSVC)
#pragma managed(pop)
#endif
#include <map>
#if (_MANAGED == 1) || (_M_CEE == 1)
// One of the Visual C++ /clr modes. In this case, the global instance
// map must be declared as a static data member of the class.
#define QL_MANAGED 1
#else
// Every other configuration. The map can be declared as a static
// variable inside the creation method.
#define QL_MANAGED 0
#endif
namespace QuantLib {
// This allows to define a different type if needed, while keeping
// backwards compatibility with the current implementation.
// For instance, one might create a file threadkey.hpp with:
//
// #include <pthread.h>
// #define QL_THREAD_KEY pthread_t
//
// and then compile QuantLib with the option -DQL_INCLUDE_FIRST=threadkey.hpp
// to have that file included by qldefines.hpp and thus this one.
#if defined(QL_THREAD_KEY)
typedef QL_THREAD_KEY ThreadKey;
#else
typedef Integer ThreadKey;
#endif
#if defined(QL_ENABLE_SESSIONS)
// definition must be provided by the user
ThreadKey sessionId();
#endif
// this is required on VC++ when CLR support is enabled
#if defined(QL_PATCH_MSVC)
#pragma managed(push, off)
#endif
//! Basic support for the singleton pattern.
/*! The typical use of this class is:
\code
class Foo : public Singleton<Foo> {
friend class Singleton<Foo>;
private:
Foo() {}
public:
...
};
\endcode
which, albeit sub-optimal, frees one from the concerns of
creating and managing the unique instance and can serve later
as a single implemementation point should synchronization
features be added.
\ingroup patterns
*/
template <class T>
class Singleton : private boost::noncopyable {
#if (QL_MANAGED == 1) && !defined(QL_SINGLETON_THREAD_SAFE_INIT)
private:
static std::map<ThreadKey, ext::shared_ptr<T> > instances_;
#endif
#if defined(QL_SINGLETON_THREAD_SAFE_INIT)
private:
static boost::atomic<T*> instance_;
static boost::mutex mutex_;
#endif
public:
//! access to the unique instance
static T& instance();
protected:
Singleton() {}
};
// static member definitions
#if (QL_MANAGED == 1) && !defined(QL_SINGLETON_THREAD_SAFE_INIT)
template <class T>
std::map<ThreadKey, ext::shared_ptr<T> > Singleton<T>::instances_;
#endif
#if defined(QL_SINGLETON_THREAD_SAFE_INIT)
template <class T> boost::atomic<T*> Singleton<T>::instance_;
template <class T> boost::mutex Singleton<T>::mutex_;
#endif
// template definitions
template <class T>
T& Singleton<T>::instance() {
#if (QL_MANAGED == 0) && !defined(QL_SINGLETON_THREAD_SAFE_INIT)
static std::map<ThreadKey, ext::shared_ptr<T> > instances_;
#endif
// thread safe double checked locking pattern with atomic memory calls
#if defined(QL_SINGLETON_THREAD_SAFE_INIT)
T* instance = instance_.load(boost::memory_order_consume);
if (!instance) {
boost::mutex::scoped_lock guard(mutex_);
instance = instance_.load(boost::memory_order_consume);
if (!instance) {
instance = new T();
instance_.store(instance, boost::memory_order_release);
}
}
#else //this is not thread safe
#if defined(QL_ENABLE_SESSIONS)
ThreadKey id = sessionId();
#else
ThreadKey id = 0;
#endif
ext::shared_ptr<T>& instance = instances_[id];
if (!instance)
instance = ext::shared_ptr<T>(new T);
#endif
return *instance;
}
// reverts the change above
#if defined(QL_PATCH_MSVC)
#pragma managed(pop)
#endif
}
#undef QL_MANAGED
#endif
|