]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/test/named_semaphore_test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / interprocess / test / named_semaphore_test.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2004-2012. 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 #include <boost/interprocess/detail/config_begin.hpp>
12 #include <boost/interprocess/sync/named_semaphore.hpp>
13 #include <boost/interprocess/detail/interprocess_tester.hpp>
14 #include <boost/interprocess/exceptions.hpp>
15 #include <boost/date_time/posix_time/posix_time_types.hpp>
16 #include "named_creation_template.hpp"
17 #include "mutex_test_template.hpp"
18 #include <string>
19 #include "get_process_id_name.hpp"
20
21 using namespace boost::interprocess;
22
23 static const std::size_t RecSemCount = 100;
24 static const char * SemName = test::get_process_id_name();
25
26 //This wrapper is necessary to plug this class
27 //in lock tests
28 class lock_test_wrapper
29 : public named_semaphore
30 {
31 public:
32
33 lock_test_wrapper(create_only_t, const char *name, unsigned int count = 1)
34 : named_semaphore(create_only, name, count)
35 {}
36
37 lock_test_wrapper(open_only_t, const char *name)
38 : named_semaphore(open_only, name)
39 {}
40
41 lock_test_wrapper(open_or_create_t, const char *name, unsigned int count = 1)
42 : named_semaphore(open_or_create, name, count)
43 {}
44
45 ~lock_test_wrapper()
46 {}
47
48 void lock()
49 { this->wait(); }
50
51 bool try_lock()
52 { return this->try_wait(); }
53
54 bool timed_lock(const boost::posix_time::ptime &pt)
55 { return this->timed_wait(pt); }
56
57 void unlock()
58 { this->post(); }
59 };
60
61 //This wrapper is necessary to plug this class
62 //in recursive tests
63 class recursive_test_wrapper
64 : public lock_test_wrapper
65 {
66 public:
67 recursive_test_wrapper(create_only_t, const char *name)
68 : lock_test_wrapper(create_only, name, RecSemCount)
69 {}
70
71 recursive_test_wrapper(open_only_t, const char *name)
72 : lock_test_wrapper(open_only, name)
73 {}
74
75 recursive_test_wrapper(open_or_create_t, const char *name)
76 : lock_test_wrapper(open_or_create, name, RecSemCount)
77 {}
78 };
79
80 bool test_named_semaphore_specific()
81 {
82 //Test persistance
83 {
84 named_semaphore sem(create_only, SemName, 3);
85 }
86 {
87 named_semaphore sem(open_only, SemName);
88 BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
89 BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
90 BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
91 BOOST_INTERPROCESS_CHECK(sem.try_wait() == false);
92 sem.post();
93 }
94 {
95 named_semaphore sem(open_only, SemName);
96 BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
97 BOOST_INTERPROCESS_CHECK(sem.try_wait() == false);
98 }
99
100 named_semaphore::remove(SemName);
101 return true;
102 }
103
104 int main ()
105 {
106 try{
107 named_semaphore::remove(SemName);
108 test::test_named_creation< test::named_sync_creation_test_wrapper<lock_test_wrapper> >();
109 test::test_all_lock< test::named_sync_wrapper<lock_test_wrapper> >();
110 test::test_all_mutex<test::named_sync_wrapper<lock_test_wrapper> >();
111 test::test_all_recursive_lock<test::named_sync_wrapper<recursive_test_wrapper> >();
112 test_named_semaphore_specific();
113 }
114 catch(std::exception &ex){
115 named_semaphore::remove(SemName);
116 std::cout << ex.what() << std::endl;
117 return 1;
118 }
119 named_semaphore::remove(SemName);
120 return 0;
121 }
122
123 #include <boost/interprocess/detail/config_end.hpp>