2 // Copyright Oliver Kowalke 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // This test is based on the tests of Boost.Thread
15 #include <boost/chrono.hpp>
16 #include <boost/test/unit_test.hpp>
17 #include <boost/thread/barrier.hpp>
18 #include <boost/thread/thread.hpp>
20 #include <boost/fiber/all.hpp>
22 typedef boost::chrono::nanoseconds ns
;
23 typedef boost::chrono::milliseconds ms
;
28 template< typename Mtx
>
29 void g( boost::barrier
& b
, Mtx
& m
) {
36 template< typename Mtx
>
37 void f( boost::barrier
& b
, Mtx
& m
) {
44 template< typename Mtx
>
45 void fn1( boost::barrier
& b
, Mtx
& m
) {
46 boost::fibers::fiber( boost::fibers::launch::dispatch
, g
< Mtx
>, std::ref( b
), std::ref( m
) ).join();
49 template< typename Mtx
>
50 void fn2( boost::barrier
& b
, Mtx
& m
) {
51 boost::fibers::fiber( boost::fibers::launch::dispatch
, f
< Mtx
>, std::ref( b
), std::ref( m
) ).join();
55 for ( int i
= 0; i
< 10; ++i
) {
56 boost::fibers::mutex mtx
;
59 boost::thread
t1( fn1
< boost::fibers::mutex
>, std::ref( b
), std::ref( mtx
) );
60 boost::thread
t2( fn2
< boost::fibers::mutex
>, std::ref( b
), std::ref( mtx
) );
62 boost::this_thread::sleep_for( ms( 250) );
66 BOOST_CHECK( 3 == value1
);
67 BOOST_CHECK( 7 == value2
);
71 void test_recursive_mutex() {
72 for ( int i
= 0; i
< 10; ++i
) {
73 boost::fibers::recursive_mutex mtx
;
76 boost::thread
t1( fn1
< boost::fibers::recursive_mutex
>, std::ref( b
), std::ref( mtx
) );
77 boost::thread
t2( fn2
< boost::fibers::recursive_mutex
>, std::ref( b
), std::ref( mtx
) );
79 boost::this_thread::sleep_for( ms( 250) );
83 BOOST_CHECK( 3 == value1
);
84 BOOST_CHECK( 7 == value2
);
88 void test_timed_mutex() {
89 for ( int i
= 0; i
< 10; ++i
) {
90 boost::fibers::timed_mutex mtx
;
93 boost::thread
t1( fn1
< boost::fibers::timed_mutex
>, std::ref( b
), std::ref( mtx
) );
94 boost::thread
t2( fn2
< boost::fibers::timed_mutex
>, std::ref( b
), std::ref( mtx
) );
96 boost::this_thread::sleep_for( ms( 250) );
100 BOOST_CHECK( 3 == value1
);
101 BOOST_CHECK( 7 == value2
);
105 void test_recursive_timed_mutex() {
106 for ( int i
= 0; i
< 10; ++i
) {
107 boost::fibers::recursive_timed_mutex mtx
;
109 boost::barrier
b( 3);
110 boost::thread
t1( fn1
< boost::fibers::recursive_timed_mutex
>, std::ref( b
), std::ref( mtx
) );
111 boost::thread
t2( fn2
< boost::fibers::recursive_timed_mutex
>, std::ref( b
), std::ref( mtx
) );
113 boost::this_thread::sleep_for( ms( 250) );
117 BOOST_CHECK( 3 == value1
);
118 BOOST_CHECK( 7 == value2
);
125 boost::unit_test::test_suite
* init_unit_test_suite( int, char* []) {
126 boost::unit_test::test_suite
* test
=
127 BOOST_TEST_SUITE("Boost.Fiber: multithreaded mutex test suite");
129 #if ! defined(BOOST_FIBERS_NO_ATOMICS)
130 test
->add( BOOST_TEST_CASE( & test_mutex
) );
131 test
->add( BOOST_TEST_CASE( & test_recursive_mutex
) );
132 test
->add( BOOST_TEST_CASE( & test_timed_mutex
) );
133 test
->add( BOOST_TEST_CASE( & test_recursive_timed_mutex
) );
135 test
->add( BOOST_TEST_CASE( & test_dummy
) );