| 1 | // |
|---|---|
| 2 | // Foundation.cpp |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Core |
| 6 | // Module: Foundation |
| 7 | // |
| 8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Foundation.h" |
| 16 | #include "Poco/MemoryPool.h" |
| 17 | #include "Poco/RefCountedObject.h" |
| 18 | #include "Poco/Logger.h" |
| 19 | #include "Poco/ThreadPool.h" |
| 20 | |
| 21 | |
| 22 | // Important note: |
| 23 | // |
| 24 | // This file contains definitions of static and/or global variables |
| 25 | // which depend on each other and require deterministic order of |
| 26 | // creation and destruction. Therefore, the definition order in |
| 27 | // this file should not be changed, nor should these definitions be |
| 28 | // moved elsewhere without a good reason *and* proper understanding |
| 29 | // of the consequences. |
| 30 | |
| 31 | |
| 32 | namespace Poco { |
| 33 | |
| 34 | |
| 35 | // |
| 36 | // FastMemoryPool |
| 37 | // |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | FastMemoryPool<WeakRefCounter> wrcFMP(POCO_FAST_MEMORY_POOL_PREALLOC); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | template <typename T> |
| 46 | FastMemoryPool<T>& getFastMemoryPool() |
| 47 | { |
| 48 | poco_assert(false); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | template <> |
| 53 | FastMemoryPool<WeakRefCounter>& getFastMemoryPool() |
| 54 | { |
| 55 | return wrcFMP; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | #ifdef ENABLE_REFCOUNT_DC |
| 60 | |
| 61 | |
| 62 | // |
| 63 | // RefCountDiagnosticContext |
| 64 | // |
| 65 | |
| 66 | // static RefCountDiagnosticContext members |
| 67 | RCDC::TraceMap RCDC::_traceMap; |
| 68 | SpinlockMutex RCDC::_mutex; |
| 69 | bool RCDC::_full = false; |
| 70 | |
| 71 | |
| 72 | namespace |
| 73 | { |
| 74 | RefCountDiagnosticContext rcdc; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | RefCountDiagnosticContext& RefCountDiagnosticContext::get() |
| 79 | { |
| 80 | return rcdc; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | #endif // ENABLE_REFCOUNT_DC |
| 85 | |
| 86 | |
| 87 | // |
| 88 | // Logger |
| 89 | // |
| 90 | |
| 91 | // static Logger members |
| 92 | Logger::LoggerMapPtr Logger::_pLoggerMap; |
| 93 | Mutex Logger::_mapMtx; |
| 94 | const std::string Logger::ROOT; |
| 95 | |
| 96 | |
| 97 | class AutoLoggerShutdown |
| 98 | /// Ensures proper Logger termination. |
| 99 | { |
| 100 | public: |
| 101 | AutoLoggerShutdown() { } |
| 102 | ~AutoLoggerShutdown() |
| 103 | { |
| 104 | try |
| 105 | { |
| 106 | Logger::shutdown(); |
| 107 | } |
| 108 | catch (...) |
| 109 | { |
| 110 | poco_unexpected(); |
| 111 | } |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | |
| 116 | namespace |
| 117 | { |
| 118 | static AutoLoggerShutdown als; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | // |
| 123 | // ThreadPool |
| 124 | // |
| 125 | |
| 126 | class ThreadPoolSingletonHolder |
| 127 | { |
| 128 | public: |
| 129 | ThreadPoolSingletonHolder() |
| 130 | { |
| 131 | _pPool = 0; |
| 132 | } |
| 133 | |
| 134 | ~ThreadPoolSingletonHolder() |
| 135 | { |
| 136 | delete _pPool; |
| 137 | } |
| 138 | |
| 139 | ThreadPool* pool(ThreadPool::ThreadAffinityPolicy affinityPolicy = ThreadPool::TAP_DEFAULT) |
| 140 | { |
| 141 | FastMutex::ScopedLock lock(_mutex); |
| 142 | |
| 143 | if (!_pPool) |
| 144 | { |
| 145 | _pPool = new ThreadPool("default"); |
| 146 | _pPool->setAffinityPolicy(affinityPolicy); |
| 147 | #if (POCO_THREAD_STACK_SIZE > 0) |
| 148 | _pPool->setStackSize(POCO_THREAD_STACK_SIZE); |
| 149 | #endif |
| 150 | } |
| 151 | return _pPool; |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | ThreadPool* _pPool; |
| 156 | FastMutex _mutex; |
| 157 | }; |
| 158 | |
| 159 | |
| 160 | namespace |
| 161 | { |
| 162 | static ThreadPoolSingletonHolder sh; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | ThreadPool& ThreadPool::defaultPool(ThreadAffinityPolicy affinityPolicy) |
| 167 | { |
| 168 | return *sh.pool(affinityPolicy); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | } // namespace Poco |
| 173 |