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
32namespace Poco {
33
34
35//
36// FastMemoryPool
37//
38
39namespace
40{
41 FastMemoryPool<WeakRefCounter> wrcFMP(POCO_FAST_MEMORY_POOL_PREALLOC);
42}
43
44
45template <typename T>
46FastMemoryPool<T>& getFastMemoryPool()
47{
48 poco_assert(false);
49}
50
51
52template <>
53FastMemoryPool<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
67RCDC::TraceMap RCDC::_traceMap;
68SpinlockMutex RCDC::_mutex;
69bool RCDC::_full = false;
70
71
72namespace
73{
74 RefCountDiagnosticContext rcdc;
75}
76
77
78RefCountDiagnosticContext& 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
92Logger::LoggerMapPtr Logger::_pLoggerMap;
93Mutex Logger::_mapMtx;
94const std::string Logger::ROOT;
95
96
97class AutoLoggerShutdown
98 /// Ensures proper Logger termination.
99{
100public:
101 AutoLoggerShutdown() { }
102 ~AutoLoggerShutdown()
103 {
104 try
105 {
106 Logger::shutdown();
107 }
108 catch (...)
109 {
110 poco_unexpected();
111 }
112 }
113};
114
115
116namespace
117{
118 static AutoLoggerShutdown als;
119}
120
121
122//
123// ThreadPool
124//
125
126class ThreadPoolSingletonHolder
127{
128public:
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
154private:
155 ThreadPool* _pPool;
156 FastMutex _mutex;
157};
158
159
160namespace
161{
162 static ThreadPoolSingletonHolder sh;
163}
164
165
166ThreadPool& ThreadPool::defaultPool(ThreadAffinityPolicy affinityPolicy)
167{
168 return *sh.pool(affinityPolicy);
169}
170
171
172} // namespace Poco
173