forked from boostorg/interprocess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnamed_condition_test_helpers.hpp
280 lines (232 loc) · 9.07 KB
/
named_condition_test_helpers.hpp
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
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2021-2021. 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.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_INTERPROCESS_TEST_NAMED_CONDITION_HELPERS_HEADER
#define BOOST_INTERPROCESS_TEST_NAMED_CONDITION_HELPERS_HEADER
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/sync/cv_status.hpp>
#include <boost/interprocess/detail/interprocess_tester.hpp>
#include <boost/interprocess/sync/detail/locks.hpp>
#include "condition_test_template.hpp"
#include "named_creation_template.hpp"
#include "get_process_id_name.hpp"
namespace boost { namespace interprocess { namespace test {
template<class NamedCondition>
struct condition_deleter
{
std::string name;
~condition_deleter()
{
if(name.empty())
NamedCondition::remove(test::add_to_process_id_name("named_condition"));
else
NamedCondition::remove(name.c_str());
}
};
#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)
template<class NamedCondition>
struct condition_deleter_w
{
std::string name;
~condition_deleter_w()
{
if(name.empty())
NamedCondition::remove(test::add_to_process_id_name(L"named_condition"));
else
NamedCondition::remove(name.c_str());
}
};
#endif //#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)
inline std::string num_to_string(int n)
{ std::stringstream s; s << n; return s.str(); }
//This wrapper is necessary to have a default constructor
//in generic mutex_test_template functions
template<class NamedCondition>
class named_condition_test_wrapper
: public condition_deleter<NamedCondition>, public NamedCondition
{
public:
named_condition_test_wrapper()
: NamedCondition(open_or_create,
(test::add_to_process_id_name("test_cond") + num_to_string(count)).c_str())
{
condition_deleter<NamedCondition>::name += test::add_to_process_id_name("test_cond");
condition_deleter<NamedCondition>::name += num_to_string(count);
++count;
}
~named_condition_test_wrapper()
{ --count; }
template <typename L>
void wait(L& lock)
{
ipcdetail::internal_mutex_lock<L> internal_lock(lock);
NamedCondition::wait(internal_lock);
}
template <typename L, typename Pr>
void wait(L& lock, Pr pred)
{
ipcdetail::internal_mutex_lock<L> internal_lock(lock);
NamedCondition::wait(internal_lock, pred);
}
template <typename L, typename TimePoint>
bool timed_wait(L& lock, const TimePoint &abs_time)
{
ipcdetail::internal_mutex_lock<L> internal_lock(lock);
return NamedCondition::timed_wait(internal_lock, abs_time);
}
template <typename L, typename TimePoint, typename Pr>
bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
{
ipcdetail::internal_mutex_lock<L> internal_lock(lock);
return NamedCondition::timed_wait(internal_lock, abs_time, pred);
}
template <typename L, class TimePoint>
cv_status wait_until(L& lock, const TimePoint &abs_time)
{
ipcdetail::internal_mutex_lock<L> internal_lock(lock);
return NamedCondition::wait_until(internal_lock, abs_time);
}
template <typename L, class TimePoint, typename Pr>
bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
{
ipcdetail::internal_mutex_lock<L> internal_lock(lock);
return NamedCondition::wait_until(internal_lock, abs_time, pred);
}
static int count;
};
template<class NamedCondition>
int named_condition_test_wrapper<NamedCondition>::count = 0;
//This wrapper is necessary to have a common constructor
//in generic named_creation_template functions
template<class NamedCondition>
class named_condition_creation_test_wrapper
: public condition_deleter<NamedCondition>, public NamedCondition
{
public:
named_condition_creation_test_wrapper(create_only_t)
: NamedCondition(create_only, test::add_to_process_id_name("named_condition"))
{ ++count_; }
named_condition_creation_test_wrapper(open_only_t)
: NamedCondition(open_only, test::add_to_process_id_name("named_condition"))
{ ++count_; }
named_condition_creation_test_wrapper(open_or_create_t)
: NamedCondition(open_or_create, test::add_to_process_id_name("named_condition"))
{ ++count_; }
~named_condition_creation_test_wrapper() {
if(--count_){
ipcdetail::interprocess_tester::
dont_close_on_destruction(static_cast<NamedCondition&>(*this));
}
}
static int count_;
};
template<class NamedCondition>
int named_condition_creation_test_wrapper<NamedCondition>::count_ = 0;
#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)
//This wrapper is necessary to have a common constructor
//in generic named_creation_template functions
template<class NamedCondition>
class named_condition_creation_test_wrapper_w
: public condition_deleter_w<NamedCondition>, public NamedCondition
{
public:
named_condition_creation_test_wrapper_w(create_only_t)
: NamedCondition(create_only, test::add_to_process_id_name(L"named_condition"))
{ ++count_; }
named_condition_creation_test_wrapper_w(open_only_t)
: NamedCondition(open_only, test::add_to_process_id_name(L"named_condition"))
{ ++count_; }
named_condition_creation_test_wrapper_w(open_or_create_t)
: NamedCondition(open_or_create, test::add_to_process_id_name(L"named_condition"))
{ ++count_; }
~named_condition_creation_test_wrapper_w() {
if(--count_){
ipcdetail::interprocess_tester::
dont_close_on_destruction(static_cast<NamedCondition&>(*this));
}
}
static int count_;
};
template<class NamedCondition>
int named_condition_creation_test_wrapper_w<NamedCondition>::count_ = 0;
#endif //#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)
template<class NamedMutex>
struct mutex_deleter
{
std::string name;
~mutex_deleter()
{
if(name.empty())
NamedMutex::remove(test::add_to_process_id_name("named_mutex"));
else
NamedMutex::remove(name.c_str());
}
};
//This wrapper is necessary to have a default constructor
//in generic mutex_test_template functions
template<class NamedMutex>
class named_mutex_test_wrapper
: public mutex_deleter<NamedMutex>, public NamedMutex
{
public:
named_mutex_test_wrapper()
: NamedMutex(open_or_create,
(test::add_to_process_id_name("test_mutex") + num_to_string(count)).c_str())
{
mutex_deleter<NamedMutex>::name += test::add_to_process_id_name("test_mutex");
mutex_deleter<NamedMutex>::name += num_to_string(count);
++count;
}
typedef NamedMutex internal_mutex_type;
internal_mutex_type &internal_mutex()
{ return *this; }
~named_mutex_test_wrapper()
{ --count; }
static int count;
};
template<class NamedMutex>
int named_mutex_test_wrapper<NamedMutex>::count = 0;
template<class NamedCondition, class NamedMutex>
int test_named_condition()
{
int ret = 0;
BOOST_TRY{
//Remove previous mutexes and conditions
NamedMutex::remove(test::add_to_process_id_name("test_mutex0"));
NamedCondition::remove(test::add_to_process_id_name("test_cond0"));
NamedCondition::remove(test::add_to_process_id_name("test_cond1"));
NamedCondition::remove(test::add_to_process_id_name("named_condition"));
NamedMutex::remove(test::add_to_process_id_name("named_mutex"));
test::test_named_creation<test::named_condition_creation_test_wrapper<NamedCondition> >();
#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)
//Remove previous mutexes and conditions
NamedMutex::remove(test::add_to_process_id_name("test_mutex0"));
NamedCondition::remove(test::add_to_process_id_name("test_cond0"));
NamedCondition::remove(test::add_to_process_id_name("test_cond1"));
NamedCondition::remove(test::add_to_process_id_name("named_condition"));
NamedMutex::remove(test::add_to_process_id_name("named_mutex"));
test::test_named_creation<test::named_condition_creation_test_wrapper_w<NamedCondition> >();
#endif
test::do_test_condition<test::named_condition_test_wrapper<NamedCondition>
,test::named_mutex_test_wrapper<NamedMutex> >();
}
BOOST_CATCH(std::exception &ex){
std::cout << ex.what() << std::endl;
ret = 1;
} BOOST_CATCH_END
NamedMutex::remove(test::add_to_process_id_name("test_mutex0"));
NamedCondition::remove(test::add_to_process_id_name("test_cond0"));
NamedCondition::remove(test::add_to_process_id_name("test_cond1"));
NamedCondition::remove(test::add_to_process_id_name("named_condition"));
NamedMutex::remove(test::add_to_process_id_name("named_mutex"));
return ret;
}
}}} //namespace boost { namespace interprocess { namespace test {
#include <boost/interprocess/detail/config_end.hpp>
#endif //BOOST_INTERPROCESS_TEST_NAMED_CONDITION_HELPERS_HEADER