{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathintermodule_singleton_test.cpp
More file actions
402 lines (347 loc) · 11.8 KB
/
Copy pathintermodule_singleton_test.cpp
File metadata and controls
402 lines (347 loc) · 11.8 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/detail/intermodule_singleton.hpp>
#include <boost/interprocess/detail/portable_intermodule_singleton.hpp>
#include <boost/interprocess/detail/os_thread_functions.hpp>
#include <iostream>
#include <cstdlib> //for std::abort
#include <typeinfo>
#include "util.hpp"
using namespace boost::interprocess;
class MyClass
{
public:
MyClass()
{
std::cout << "MyClass()\n" << std::endl;
}
void shout() const
{
std::cout << "Shout\n" << std::endl;
}
~MyClass()
{
std::cout << "~MyClass()\n" << std::endl;
}
};
class MyDerivedClass
: public MyClass
{};
class MyThrowingClass
{
public:
MyThrowingClass()
{
throw int(0);
}
};
template < template<class T, bool LazyInit, bool Phoenix> class IntermoduleType >
int intermodule_singleton_test()
{
bool exception_thrown = false;
bool exception_2_thrown = false;
BOOST_INTERPROCESS_TRY{
IntermoduleType<MyThrowingClass, true, false>::get();
}
BOOST_INTERPROCESS_CATCH(int &){
exception_thrown = true;
//Second try
BOOST_INTERPROCESS_TRY{
IntermoduleType<MyThrowingClass, true, false>::get();
}
BOOST_INTERPROCESS_CATCH(interprocess_exception &){
exception_2_thrown = true;
} BOOST_INTERPROCESS_CATCH_END
} BOOST_INTERPROCESS_CATCH_END
if(!exception_thrown || !exception_2_thrown){
return 1;
}
MyClass & mc = IntermoduleType<MyClass, true, false>::get();
mc.shout();
IntermoduleType<MyClass, true, false>::get().shout();
IntermoduleType<MyDerivedClass, true, false>::get().shout();
//Second try
exception_2_thrown = false;
BOOST_INTERPROCESS_TRY{
IntermoduleType<MyThrowingClass, true, false>::get();
}
BOOST_INTERPROCESS_CATCH(interprocess_exception &){
exception_2_thrown = true;
} BOOST_INTERPROCESS_CATCH_END
if(!exception_2_thrown){
return 1;
}
return 0;
}
static const boost::uint32_t concurrent_thread_count = 4u;
//A class that throws in the constructor once all test threads have requested
//the singleton, so that the initializing thread fails while the other threads
//are waiting for it. Those threads must throw instead of obtaining a
//reference to a singleton that was never constructed.
template<class Tag>
class ConcurrentThrowingClass
{
public:
ConcurrentThrowingClass()
{
arrived.wait_at_least(concurrent_thread_count);
//Let the peer threads reach the singleton wait loop before failing
for(unsigned i = 0; i != 1000u; ++i){
ipcdetail::thread_yield();
}
throw int(0);
}
static test::test_counter arrived;
};
template<class Tag>
test::test_counter ConcurrentThrowingClass<Tag>::arrived;
template<class Tag, class ThrowingSingleton>
void concurrent_throwing_get(void *, volatile boost::uint32_t &returned_count)
{
ConcurrentThrowingClass<Tag>::arrived.increment();
BOOST_INTERPROCESS_TRY{
ThrowingSingleton::get();
//get() must never return when the singleton could not be constructed
ipcdetail::atomic_add32(&returned_count, 1u);
}
BOOST_INTERPROCESS_CATCH(...){
} BOOST_INTERPROCESS_CATCH_END
}
template < template<class T, bool LazyInit, bool Phoenix> class IntermoduleType >
int concurrent_throwing_singleton_test()
{
typedef IntermoduleType<char, true, false> Tag;
typedef ConcurrentThrowingClass<Tag> ThrowingType;
typedef IntermoduleType<ThrowingType, true, false> ThrowingSingleton;
volatile boost::uint32_t returned_count = 0u;
ipcdetail::OS_thread_t threads[concurrent_thread_count];
for(boost::uint32_t i = 0; i != concurrent_thread_count; ++i){
ipcdetail::thread_launch
( threads[i]
, test::thread_adapter<volatile boost::uint32_t>
(&concurrent_throwing_get<Tag, ThrowingSingleton>, 0, returned_count));
}
for(boost::uint32_t i = 0; i != concurrent_thread_count; ++i){
ipcdetail::thread_join(threads[i]);
}
return ipcdetail::atomic_read32(&returned_count) ? 1 : 0;
}
//A class simulating a logger
//We'll register constructor/destructor counts
//to test the singleton was correctly resurrected
//by LogUser singleton.
template<class Tag>
class Logger
{
public:
Logger()
{
++constructed_times;
std::cout << "Logger(),tag:" << typeid(Tag).name() << "(construct #" << constructed_times << ")\n" << std::endl;
}
void log_it()
{}
~Logger()
{
++destroyed_times;
std::cout << "~Logger(),tag:" << typeid(Tag).name() << "(destroy #" << destroyed_times << ")\n" << std::endl;
}
static unsigned int constructed_times;
static unsigned int destroyed_times;
};
template<class Tag>
unsigned int Logger<Tag>::constructed_times;
template<class Tag>
unsigned int Logger<Tag>::destroyed_times;
//A class simulating a logger user.
//The destructor uses the logger so that
//the logger is resurrected if it was
//already destroyed
template<class LogSingleton>
class LogUser
{
public:
LogUser()
{
std::cout << "LogUser(),tag:" << typeid(LogSingleton).name() << "\n" << std::endl;
}
void function_using_log()
{ LogSingleton::get().log_it(); }
~LogUser()
{
std::cout << "~LogUser(),tag:" << typeid(LogSingleton).name() << "\n" << std::endl;
LogSingleton::get().log_it();
}
};
//A class that tests the correct
//phoenix singleton behaviour.
//Logger should be resurrected by LogUser
template<class Tag>
class LogPhoenixTester
{
public:
LogPhoenixTester()
{
std::cout << "LogPhoenixTester(), tag: " << typeid(Tag).name() << "\n" << std::endl;
}
void dummy()
{}
~LogPhoenixTester()
{
//Test Phoenix singleton was correctly executed:
//created and destroyed two times
//This test will be executed after main ends
std::cout << "~LogPhoenixTester(), tag: " << typeid(Tag).name() << "\n" << std::endl;
if(Logger<Tag>::constructed_times != Logger<Tag>::destroyed_times ||
Logger<Tag>::constructed_times != 2)
{
std::stringstream sstr;
sstr << "LogPhoenixTester failed for tag ";
sstr << typeid(Tag).name();
sstr << "\n";
if(Logger<Tag>::constructed_times != 2){
sstr << "Logger<Tag>::constructed_times != 2\n";
sstr << "(";
sstr << Logger<Tag>::constructed_times << ")\n";
}
else{
sstr << "Logger<Tag>::constructed_times != Logger<Tag>::destroyed_times\n";
sstr << "(" << Logger<Tag>::constructed_times << " vs. " << Logger<Tag>::destroyed_times << ")\n";
}
std::cout << "~LogPhoenixTester(), error: " << sstr.str() << std::endl;
std::abort();
}
}
};
//A class simulating a logger user.
//The destructor uses the logger so that
//the logger is resurrected if it was
//already destroyed
template<class LogSingleton>
class LogDeadReferenceUser
{
public:
LogDeadReferenceUser()
{
std::cout << "LogDeadReferenceUser(), LogSingleton: " << typeid(LogSingleton).name() << "\n" << std::endl;
}
void function_using_log()
{ LogSingleton::get().log_it(); }
~LogDeadReferenceUser()
{
std::cout << "~LogDeadReferenceUser(), LogSingleton: " << typeid(LogSingleton).name() << "\n" << std::endl;
//Make sure the exception is thrown as we are
//trying to use a dead non-phoenix singleton
BOOST_INTERPROCESS_TRY{
LogSingleton::get().log_it();
std::string s("LogDeadReferenceUser failed for LogSingleton ");
s += typeid(LogSingleton).name();
std::cout << "~LogDeadReferenceUser(), error: " << s << std::endl;
std::abort();
}
BOOST_INTERPROCESS_CATCH(interprocess_exception &){
//Correct behaviour
} BOOST_INTERPROCESS_CATCH_END
}
};
template < template<class T, bool LazyInit, bool Phoenix> class IntermoduleType >
int phoenix_singleton_test()
{
typedef int DummyType;
typedef IntermoduleType<DummyType, true, true> Tag;
typedef Logger<Tag> LoggerType;
typedef IntermoduleType<LoggerType, true, true> LoggerSingleton;
typedef LogUser<LoggerSingleton> LogUserType;
typedef IntermoduleType<LogUserType, true, true> LogUserSingleton;
typedef IntermoduleType<LogPhoenixTester<Tag>, true, true> LogPhoenixTesterSingleton;
//Instantiate Phoenix tester singleton so that it will be destroyed the last
LogPhoenixTesterSingleton::get().dummy();
//Now instantitate a log user singleton
LogUserType &log_user = LogUserSingleton::get();
//Then force LoggerSingleton instantiation
//calling a function that will use it.
//After main ends, LoggerSingleton will be destroyed
//before LogUserSingleton due to LIFO
//singleton semantics
log_user.function_using_log();
//Next, LogUserSingleton destructor will resurrect
//LoggerSingleton.
//After that LoggerSingleton will be destroyed and
//lastly LogPhoenixTester will be destroyed checking
//LoggerSingleton was correctly destroyed.
return 0;
}
template < template<class T, bool LazyInit, bool Phoenix> class IntermoduleType >
int dead_reference_singleton_test()
{
typedef int DummyType;
typedef IntermoduleType<DummyType, true, false> Tag;
typedef Logger<Tag> LoggerType;
typedef IntermoduleType<LoggerType, true, false> LoggerSingleton;
typedef LogDeadReferenceUser<LoggerSingleton> LogDeadReferenceUserType;
typedef IntermoduleType<LogDeadReferenceUserType, true, false> LogDeadReferenceUserSingleton;
//Now instantitate a log user singleton
LogDeadReferenceUserType &log_user = LogDeadReferenceUserSingleton::get();
//Then force LoggerSingleton instantiation
//calling a function that will use it.
//After main ends, LoggerSingleton will be destroyed
//before LogDeadReferenceUserType due to LIFO
//singleton semantics
log_user.function_using_log();
//Next, LogDeadReferenceUserType destructor will try to use
//LoggerSingleton and an exception will be raised an catched.
return 0;
}
//reduce name length
template<typename C, bool LazyInit, bool Phoenix>
class port_singleton
: public ipcdetail::portable_intermodule_singleton<C, LazyInit, Phoenix>
{};
#ifdef BOOST_INTERPROCESS_WINDOWS
template<typename C, bool LazyInit, bool Phoenix>
class win_singleton
: public ipcdetail::windows_intermodule_singleton< C, LazyInit, Phoenix>
{};
#endif
int main ()
{
if(0 != intermodule_singleton_test<port_singleton>()){
return 1;
}
#ifdef BOOST_INTERPROCESS_WINDOWS
if(0 != intermodule_singleton_test<win_singleton>()){
return 1;
}
#endif
if(0 != concurrent_throwing_singleton_test<port_singleton>()){
return 1;
}
#ifdef BOOST_INTERPROCESS_WINDOWS
if(0 != concurrent_throwing_singleton_test<win_singleton>()){
return 1;
}
#endif
//Only few platforms support this
#ifdef BOOST_INTERPROCESS_ATEXIT_CALLABLE_FROM_ATEXIT
//Phoenix singletons are tested after main ends,
//LogPhoenixTester does the work
phoenix_singleton_test<port_singleton>();
#ifdef BOOST_INTERPROCESS_WINDOWS
phoenix_singleton_test<win_singleton>();
#endif
#endif
//Dead reference singletons are tested after main ends,
//LogDeadReferenceUser does the work
dead_reference_singleton_test<port_singleton>();
#ifdef BOOST_INTERPROCESS_WINDOWS
dead_reference_singleton_test<win_singleton>();
#endif
return 0;
}
You can’t perform that action at this time.
