]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/regex/test/static_mutex/static_mutex_test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / regex / test / static_mutex / static_mutex_test.cpp
1 /*
2 *
3 * Copyright (c) 2004
4 * John Maddock
5 *
6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 *
10 */
11
12 /*
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE static_mutex_test.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: test program for boost::static_mutex.
17 */
18
19 #include <iostream>
20 #include <iomanip>
21 #include <boost/regex/pending/static_mutex.hpp>
22 #include <boost/thread/thread.hpp>
23 #include <boost/timer.hpp>
24
25 //
26 // we cannot use the regular Boost.Test in here: it is not thread safe
27 // and calls to BOOST_CHECK will eventually crash on some compilers
28 // (Borland certainly) due to race conditions inside the Boost.Test lib.
29 //
30 #define BOOST_CHECK(pred) if(!(pred)) failed_test(__FILE__, __LINE__, BOOST_STRINGIZE(pred));
31
32 int total_failures = 0;
33 void failed_test(const char* file, int line, const char* pred)
34 {
35 static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
36 boost::static_mutex::scoped_lock guard(mut);
37 ++total_failures;
38 std::cout << "Failed test in \"" << file << "\" at line " << line << ": " << pred << std::endl;
39 }
40
41 void print_cycles(int c)
42 {
43 static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
44 boost::static_mutex::scoped_lock guard(mut);
45 std::cout << "Thread exited after " << c << " cycles." << std::endl;
46 }
47
48 bool sufficient_time()
49 {
50 // return true if enough time has passed since the tests began:
51 static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
52 boost::static_mutex::scoped_lock guard(mut);
53 static boost::timer t;
54 // is 10 seconds enough?
55 return t.elapsed() >= 10.0;
56 }
57
58 // define three trivial test proceedures:
59 bool t1()
60 {
61 static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
62 static int has_lock = 0;
63 static int data = 10000;
64
65 boost::static_mutex::scoped_lock guard(mut);
66 BOOST_CHECK(++has_lock == 1);
67 BOOST_CHECK(guard.locked());
68 BOOST_CHECK(guard);
69 bool result = (--data > 0) ? true : false;
70 BOOST_CHECK(--has_lock == 0);
71 return result;
72 }
73
74 bool t2()
75 {
76 static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
77 static int has_lock = 0;
78 static int data = 10000;
79
80 boost::static_mutex::scoped_lock guard(mut, false);
81 BOOST_CHECK(0 == guard.locked());
82 BOOST_CHECK(!guard);
83 guard.lock();
84 BOOST_CHECK(++has_lock == 1);
85 BOOST_CHECK(guard.locked());
86 BOOST_CHECK(guard);
87 bool result = (--data > 0) ? true : false;
88 BOOST_CHECK(--has_lock == 0);
89 guard.unlock();
90 BOOST_CHECK(0 == guard.locked());
91 BOOST_CHECK(!guard);
92 return result;
93 }
94
95 bool t3()
96 {
97 static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
98 static int has_lock = 0;
99 static int data = 10000;
100
101 boost::static_mutex::scoped_lock guard(mut);
102 BOOST_CHECK(++has_lock == 1);
103 BOOST_CHECK(guard.locked());
104 BOOST_CHECK(guard);
105 bool result = (--data > 0) ? true : false;
106 BOOST_CHECK(--has_lock == 0);
107 return result;
108 }
109
110 // define their thread procs:
111 void thread1_proc()
112 {
113 int cycles = 0;
114 while(!sufficient_time())
115 {
116 t1();
117 t2();
118 ++cycles;
119 }
120 print_cycles(cycles);
121 }
122
123 void thread2_proc()
124 {
125 int cycles = 0;
126 while(!sufficient_time())
127 {
128 t2();
129 t3();
130 ++cycles;
131 }
132 print_cycles(cycles);
133 }
134
135 void thread3_proc()
136 {
137 int cycles = 0;
138 while(!sufficient_time())
139 {
140 t1();
141 t3();
142 ++cycles;
143 }
144 print_cycles(cycles);
145 }
146
147 // make sure that at least one of our test proceedures
148 // is called during program startup:
149 struct startup1
150 {
151 startup1()
152 {
153 t1();
154 }
155 ~startup1()
156 {
157 t1();
158 }
159 };
160
161 startup1 up1;
162
163 int main()
164 {
165 (void)up1;
166
167 std::list<boost::shared_ptr<boost::thread> > threads;
168 for(int i = 0; i < 2; ++i)
169 {
170 try{
171 threads.push_back(boost::shared_ptr<boost::thread>(new boost::thread(&thread1_proc)));
172 }
173 catch(const std::exception& e)
174 {
175 std::cerr << "<note>Thread creation failed with message: " << e.what() << "</note>" << std::endl;
176 }
177 }
178 for(int i = 0; i < 2; ++i)
179 {
180 try{
181 threads.push_back(boost::shared_ptr<boost::thread>(new boost::thread(&thread2_proc)));
182 }
183 catch(const std::exception& e)
184 {
185 std::cerr << "<note>Thread creation failed with message: " << e.what() << "</note>" << std::endl;
186 }
187 }
188 for(int i = 0; i < 2; ++i)
189 {
190 try{
191 threads.push_back(boost::shared_ptr<boost::thread>(new boost::thread(&thread3_proc)));
192 }
193 catch(const std::exception& e)
194 {
195 std::cerr << "<note>Thread creation failed with message: " << e.what() << "</note>" << std::endl;
196 }
197 }
198
199 std::list<boost::shared_ptr<boost::thread> >::const_iterator a(threads.begin()), b(threads.end());
200 while(a != b)
201 {
202 (*a)->join();
203 ++a;
204 }
205
206 return total_failures;
207 }