]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/regex/test/static_mutex/static_mutex_test.cpp
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)
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.
21 #include <boost/regex/pending/static_mutex.hpp>
22 #include <boost/thread/thread.hpp>
23 #include <boost/timer.hpp>
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.
30 #define BOOST_CHECK(pred) if(!(pred)) failed_test(__FILE__, __LINE__, BOOST_STRINGIZE(pred));
32 int total_failures
= 0;
33 void failed_test(const char* file
, int line
, const char* pred
)
35 static boost::static_mutex mut
= BOOST_STATIC_MUTEX_INIT
;
36 boost::static_mutex::scoped_lock
guard(mut
);
38 std::cout
<< "Failed test in \"" << file
<< "\" at line " << line
<< ": " << pred
<< std::endl
;
41 void print_cycles(int c
)
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
;
48 bool sufficient_time()
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;
58 // define three trivial test proceedures:
61 static boost::static_mutex mut
= BOOST_STATIC_MUTEX_INIT
;
62 static int has_lock
= 0;
63 static int data
= 10000;
65 boost::static_mutex::scoped_lock
guard(mut
);
66 BOOST_CHECK(++has_lock
== 1);
67 BOOST_CHECK(guard
.locked());
69 bool result
= (--data
> 0) ? true : false;
70 BOOST_CHECK(--has_lock
== 0);
76 static boost::static_mutex mut
= BOOST_STATIC_MUTEX_INIT
;
77 static int has_lock
= 0;
78 static int data
= 10000;
80 boost::static_mutex::scoped_lock
guard(mut
, false);
81 BOOST_CHECK(0 == guard
.locked());
84 BOOST_CHECK(++has_lock
== 1);
85 BOOST_CHECK(guard
.locked());
87 bool result
= (--data
> 0) ? true : false;
88 BOOST_CHECK(--has_lock
== 0);
90 BOOST_CHECK(0 == guard
.locked());
97 static boost::static_mutex mut
= BOOST_STATIC_MUTEX_INIT
;
98 static int has_lock
= 0;
99 static int data
= 10000;
101 boost::static_mutex::scoped_lock
guard(mut
);
102 BOOST_CHECK(++has_lock
== 1);
103 BOOST_CHECK(guard
.locked());
105 bool result
= (--data
> 0) ? true : false;
106 BOOST_CHECK(--has_lock
== 0);
110 // define their thread procs:
114 while(!sufficient_time())
120 print_cycles(cycles
);
126 while(!sufficient_time())
132 print_cycles(cycles
);
138 while(!sufficient_time())
144 print_cycles(cycles
);
147 // make sure that at least one of our test proceedures
148 // is called during program startup:
167 std::list
<boost::shared_ptr
<boost::thread
> > threads
;
168 for(int i
= 0; i
< 2; ++i
)
171 threads
.push_back(boost::shared_ptr
<boost::thread
>(new boost::thread(&thread1_proc
)));
173 catch(const std::exception
& e
)
175 std::cerr
<< "<note>Thread creation failed with message: " << e
.what() << "</note>" << std::endl
;
178 for(int i
= 0; i
< 2; ++i
)
181 threads
.push_back(boost::shared_ptr
<boost::thread
>(new boost::thread(&thread2_proc
)));
183 catch(const std::exception
& e
)
185 std::cerr
<< "<note>Thread creation failed with message: " << e
.what() << "</note>" << std::endl
;
188 for(int i
= 0; i
< 2; ++i
)
191 threads
.push_back(boost::shared_ptr
<boost::thread
>(new boost::thread(&thread3_proc
)));
193 catch(const std::exception
& e
)
195 std::cerr
<< "<note>Thread creation failed with message: " << e
.what() << "</note>" << std::endl
;
199 std::list
<boost::shared_ptr
<boost::thread
> >::const_iterator
a(threads
.begin()), b(threads
.end());
206 return total_failures
;