1//////////////////////////////////////////////////////////////////////////////
2//
3// (C) Copyright Ion Gaztanaga 2024-2024. Distributed under the Boost
4// Software License, Version 1.0. (See accompanying file
5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7// See http://www.boost.org/libs/interprocess for documentation.
8//
9//////////////////////////////////////////////////////////////////////////////
10
11#ifndef BOOST_INTERPROCESS_TEST_NAMED_SEMAPHORE_HELPERS_HEADER
12#define BOOST_INTERPROCESS_TEST_NAMED_SEMAPHORE_HELPERS_HEADER
13
14#include <boost/interprocess/detail/config_begin.hpp>
15#include <boost/interprocess/detail/workaround.hpp>
16
17#include <boost/interprocess/detail/interprocess_tester.hpp>
18#include <boost/interprocess/exceptions.hpp>
19#include "named_creation_template.hpp"
20#include "mutex_test_template.hpp"
21#include "get_process_id_name.hpp"
22#include <exception>
23#include <boost/interprocess/timed_utils.hpp>
24
25#include <exception>
26
27namespace boost { namespace interprocess { namespace test {
28
29static const std::size_t RecSemCount = 100;
30
31//This wrapper is necessary to plug this class
32//in lock tests
33template<class NamedSemaphore>
34class lock_test_wrapper
35 : public NamedSemaphore
36{
37 public:
38
39 template <class CharT>
40 lock_test_wrapper(create_only_t, const CharT *name, unsigned int count = 1)
41 : NamedSemaphore(create_only, name, count)
42 {}
43
44 template <class CharT>
45 lock_test_wrapper(open_only_t, const CharT *name)
46 : NamedSemaphore(open_only, name)
47 {}
48
49 template <class CharT>
50 lock_test_wrapper(open_or_create_t, const CharT *name, unsigned int count = 1)
51 : NamedSemaphore(open_or_create, name, count)
52 {}
53
54 ~lock_test_wrapper()
55 {}
56
57 void lock()
58 { this->wait(); }
59
60 bool try_lock()
61 { return this->try_wait(); }
62
63 template<class TimePoint>
64 bool timed_lock(const TimePoint &pt)
65 { return this->timed_wait(pt); }
66
67 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
68 { return this->timed_lock(abs_time); }
69
70 template<class Duration> bool try_lock_for(const Duration &dur)
71 { return this->timed_lock(boost::interprocess::ipcdetail::duration_to_ustime(dur)); }
72
73 void unlock()
74 { this->post(); }
75};
76
77//This wrapper is necessary to plug this class
78//in recursive tests
79template<class NamedSemaphore>
80class recursive_test_wrapper
81 : public lock_test_wrapper<NamedSemaphore>
82{
83 public:
84 recursive_test_wrapper(create_only_t, const char *name)
85 : lock_test_wrapper<NamedSemaphore>(create_only, name, RecSemCount)
86 {}
87
88 recursive_test_wrapper(open_only_t, const char *name)
89 : lock_test_wrapper<NamedSemaphore>(open_only, name)
90 {}
91
92 recursive_test_wrapper(open_or_create_t, const char *name)
93 : lock_test_wrapper<NamedSemaphore>(open_or_create, name, RecSemCount)
94 {}
95};
96
97template<class NamedSemaphore>
98bool test_named_semaphore_specific()
99{
100 NamedSemaphore::remove(test::get_process_id_name());
101 //Test persistance
102 {
103 NamedSemaphore sem(create_only, test::get_process_id_name(), 3);
104 }
105 {
106 NamedSemaphore sem(open_only, test::get_process_id_name());
107 BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
108 BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
109 BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
110 BOOST_INTERPROCESS_CHECK(sem.try_wait() == false);
111 sem.post();
112 }
113 {
114 NamedSemaphore sem(open_only, test::get_process_id_name());
115 BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
116 BOOST_INTERPROCESS_CHECK(sem.try_wait() == false);
117 }
118
119 NamedSemaphore::remove(test::get_process_id_name());
120 return true;
121}
122
123template<class NamedSemaphore>
124int test_named_semaphore()
125{
126 int ret = 0;
127 BOOST_TRY{
128 test::test_named_creation< test::named_sync_creation_test_wrapper<lock_test_wrapper<NamedSemaphore> > >();
129 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)
130 test::test_named_creation< test::named_sync_creation_test_wrapper_w<lock_test_wrapper<NamedSemaphore> > >();
131 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)
132
133 test::test_all_lock< test::named_sync_wrapper<lock_test_wrapper<NamedSemaphore> > >();
134 test::test_all_mutex<test::named_sync_wrapper<lock_test_wrapper<NamedSemaphore> > >();
135 test::test_all_recursive_lock<test::named_sync_wrapper<recursive_test_wrapper<NamedSemaphore> > >();
136 test_named_semaphore_specific<NamedSemaphore>();
137 }
138 BOOST_CATCH(std::exception &ex){
139 std::cout << ex.what() << std::endl;
140 ret = 1;
141 } BOOST_CATCH_END
142 NamedSemaphore::remove(test::get_process_id_name());
143 return ret;
144}
145
146}}} //namespace boost { namespace interprocess { namespace test {
147
148#include <boost/interprocess/detail/config_end.hpp>
149
150#endif //BOOST_INTERPROCESS_TEST_NAMED_SEMAPHORE_HELPERS_HEADER
151

source code of boost/libs/interprocess/test/named_semaphore_test_helpers.hpp