forked from boostorg/interprocess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnamed_semaphore_test_helpers.hpp
150 lines (123 loc) · 4.73 KB
/
named_semaphore_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
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2024-2024. 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_SEMAPHORE_HELPERS_HEADER
#define BOOST_INTERPROCESS_TEST_NAMED_SEMAPHORE_HELPERS_HEADER
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/detail/interprocess_tester.hpp>
#include <boost/interprocess/exceptions.hpp>
#include "named_creation_template.hpp"
#include "mutex_test_template.hpp"
#include "get_process_id_name.hpp"
#include <exception>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <exception>
namespace boost { namespace interprocess { namespace test {
static const std::size_t RecSemCount = 100;
//This wrapper is necessary to plug this class
//in lock tests
template<class NamedSemaphore>
class lock_test_wrapper
: public NamedSemaphore
{
public:
template <class CharT>
lock_test_wrapper(create_only_t, const CharT *name, unsigned int count = 1)
: NamedSemaphore(create_only, name, count)
{}
template <class CharT>
lock_test_wrapper(open_only_t, const CharT *name)
: NamedSemaphore(open_only, name)
{}
template <class CharT>
lock_test_wrapper(open_or_create_t, const CharT *name, unsigned int count = 1)
: NamedSemaphore(open_or_create, name, count)
{}
~lock_test_wrapper()
{}
void lock()
{ this->wait(); }
bool try_lock()
{ return this->try_wait(); }
template<class TimePoint>
bool timed_lock(const TimePoint &pt)
{ return this->timed_wait(pt); }
template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
{ return this->timed_lock(abs_time); }
template<class Duration> bool try_lock_for(const Duration &dur)
{ return this->timed_lock(boost::interprocess::ipcdetail::duration_to_ustime(dur)); }
void unlock()
{ this->post(); }
};
//This wrapper is necessary to plug this class
//in recursive tests
template<class NamedSemaphore>
class recursive_test_wrapper
: public lock_test_wrapper<NamedSemaphore>
{
public:
recursive_test_wrapper(create_only_t, const char *name)
: lock_test_wrapper<NamedSemaphore>(create_only, name, RecSemCount)
{}
recursive_test_wrapper(open_only_t, const char *name)
: lock_test_wrapper<NamedSemaphore>(open_only, name)
{}
recursive_test_wrapper(open_or_create_t, const char *name)
: lock_test_wrapper<NamedSemaphore>(open_or_create, name, RecSemCount)
{}
};
template<class NamedSemaphore>
bool test_named_semaphore_specific()
{
NamedSemaphore::remove(test::get_process_id_name());
//Test persistance
{
NamedSemaphore sem(create_only, test::get_process_id_name(), 3);
}
{
NamedSemaphore sem(open_only, test::get_process_id_name());
BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
BOOST_INTERPROCESS_CHECK(sem.try_wait() == false);
sem.post();
}
{
NamedSemaphore sem(open_only, test::get_process_id_name());
BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
BOOST_INTERPROCESS_CHECK(sem.try_wait() == false);
}
NamedSemaphore::remove(test::get_process_id_name());
return true;
}
template<class NamedSemaphore>
int test_named_semaphore()
{
int ret = 0;
BOOST_TRY{
test::test_named_creation< test::named_sync_creation_test_wrapper<lock_test_wrapper<NamedSemaphore> > >();
#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)
test::test_named_creation< test::named_sync_creation_test_wrapper_w<lock_test_wrapper<NamedSemaphore> > >();
#endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)
test::test_all_lock< test::named_sync_wrapper<lock_test_wrapper<NamedSemaphore> > >();
test::test_all_mutex<test::named_sync_wrapper<lock_test_wrapper<NamedSemaphore> > >();
test::test_all_recursive_lock<test::named_sync_wrapper<recursive_test_wrapper<NamedSemaphore> > >();
test_named_semaphore_specific<NamedSemaphore>();
}
BOOST_CATCH(std::exception &ex){
std::cout << ex.what() << std::endl;
ret = 1;
} BOOST_CATCH_END
NamedSemaphore::remove(test::get_process_id_name());
return ret;
}
}}} //namespace boost { namespace interprocess { namespace test {
#include <boost/interprocess/detail/config_end.hpp>
#endif //BOOST_INTERPROCESS_TEST_NAMED_SEMAPHORE_HELPERS_HEADER