123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- // Copyright Oliver Kowalke 2013.
- // Distributed under the Boost Software License, Version 1.0.
- // (See accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- //
- // This test is based on the tests of Boost.Thread
- #include <chrono>
- #include <cstdlib>
- #include <cstdio>
- #include <iostream>
- #include <map>
- #include <stdexcept>
- #include <vector>
- #include <boost/test/unit_test.hpp>
- #include <boost/fiber/all.hpp>
- typedef std::chrono::nanoseconds ns;
- typedef std::chrono::milliseconds ms;
- int value1 = 0;
- inline
- std::chrono::system_clock::time_point delay(int secs, int msecs = 0, int /*nsecs*/ = 0) {
- std::chrono::system_clock::time_point t = std::chrono::system_clock::now();
- t += std::chrono::seconds( secs);
- t += std::chrono::milliseconds( msecs);
- //t += std::chrono::nanoseconds( nsecs);
- return t;
- }
- struct condition_test_data {
- condition_test_data() : notified(0), awoken(0) { }
- boost::fibers::mutex mutex;
- boost::fibers::condition_variable_any cond;
- int notified;
- int awoken;
- };
- void condition_test_fiber(condition_test_data* data) {
- try {
- data->mutex.lock();
- while (!(data->notified > 0))
- data->cond.wait(data->mutex);
- data->awoken++;
- } catch ( ... ) {
- }
- data->mutex.unlock();
- }
- struct cond_predicate {
- cond_predicate(int& var, int val) : _var(var), _val(val) { }
- bool operator()() { return _var == _val; }
- int& _var;
- int _val;
- private:
- void operator=(cond_predicate&);
-
- };
- void notify_one_fn( boost::fibers::condition_variable_any & cond) {
- cond.notify_one();
- }
- void notify_all_fn( boost::fibers::condition_variable_any & cond) {
- cond.notify_all();
- }
- void wait_fn(
- boost::fibers::mutex & mtx,
- boost::fibers::condition_variable_any & cond) {
- mtx.lock();
- cond.wait( mtx);
- ++value1;
- mtx.unlock();
- }
- void test_one_waiter_notify_one() {
- value1 = 0;
- boost::fibers::mutex mtx;
- boost::fibers::condition_variable_any cond;
- boost::fibers::fiber f1(
- boost::fibers::launch::dispatch,
- wait_fn,
- std::ref( mtx),
- std::ref( cond) );
- BOOST_CHECK_EQUAL( 0, value1);
- boost::fibers::fiber f2(
- boost::fibers::launch::dispatch,
- notify_one_fn,
- std::ref( cond) );
- BOOST_CHECK_EQUAL( 0, value1);
- f1.join();
- f2.join();
- BOOST_CHECK_EQUAL( 1, value1);
- }
- void test_two_waiter_notify_one() {
- value1 = 0;
- boost::fibers::mutex mtx;
- boost::fibers::condition_variable_any cond;
- boost::fibers::fiber f1(
- boost::fibers::launch::dispatch,
- wait_fn,
- std::ref( mtx),
- std::ref( cond) );
- BOOST_CHECK_EQUAL( 0, value1);
- boost::fibers::fiber f2(
- boost::fibers::launch::dispatch,
- wait_fn,
- std::ref( mtx),
- std::ref( cond) );
- BOOST_CHECK_EQUAL( 0, value1);
- boost::fibers::fiber f3(
- boost::fibers::launch::dispatch,
- notify_one_fn,
- std::ref( cond) );
- BOOST_CHECK_EQUAL( 0, value1);
- boost::fibers::fiber f4(
- boost::fibers::launch::dispatch,
- notify_one_fn,
- std::ref( cond) );
- BOOST_CHECK_EQUAL( 1, value1);
- f1.join();
- f2.join();
- f3.join();
- f4.join();
- BOOST_CHECK_EQUAL( 2, value1);
- }
- void test_two_waiter_notify_all() {
- value1 = 0;
- boost::fibers::mutex mtx;
- boost::fibers::condition_variable_any cond;
- boost::fibers::fiber f1(
- boost::fibers::launch::dispatch,
- wait_fn,
- std::ref( mtx),
- std::ref( cond) );
- BOOST_CHECK_EQUAL( 0, value1);
- boost::fibers::fiber f2(
- boost::fibers::launch::dispatch,
- wait_fn,
- std::ref( mtx),
- std::ref( cond) );
- BOOST_CHECK_EQUAL( 0, value1);
- boost::fibers::fiber f3(
- boost::fibers::launch::dispatch,
- notify_all_fn,
- std::ref( cond) );
- BOOST_CHECK_EQUAL( 0, value1);
- boost::fibers::fiber f4(
- boost::fibers::launch::dispatch,
- wait_fn,
- std::ref( mtx),
- std::ref( cond) );
- BOOST_CHECK_EQUAL( 2, value1);
- boost::fibers::fiber f5(
- boost::fibers::launch::dispatch,
- notify_all_fn,
- std::ref( cond) );
- BOOST_CHECK_EQUAL( 2, value1);
- f1.join();
- f2.join();
- f3.join();
- f4.join();
- f5.join();
- BOOST_CHECK_EQUAL( 3, value1);
- }
- int test1 = 0;
- int test2 = 0;
- int runs = 0;
- void fn1( boost::fibers::mutex & m, boost::fibers::condition_variable_any & cv) {
- m.lock();
- BOOST_CHECK(test2 == 0);
- test1 = 1;
- cv.notify_one();
- while (test2 == 0) {
- cv.wait(m);
- }
- BOOST_CHECK(test2 != 0);
- m.unlock();
- }
- void fn2( boost::fibers::mutex & m, boost::fibers::condition_variable_any & cv) {
- m.lock();
- BOOST_CHECK(test2 == 0);
- test1 = 1;
- cv.notify_one();
- std::chrono::system_clock::time_point t0 = std::chrono::system_clock::now();
- std::chrono::system_clock::time_point t = t0 + ms(250);
- int count=0;
- while (test2 == 0 && cv.wait_until(m, t) == boost::fibers::cv_status::no_timeout)
- count++;
- std::chrono::system_clock::time_point t1 = std::chrono::system_clock::now();
- if (runs == 0) {
- BOOST_CHECK(t1 - t0 < ms(250));
- BOOST_CHECK(test2 != 0);
- } else {
- BOOST_CHECK(t1 - t0 - ms(250) < ms(count*250+100+1000));
- BOOST_CHECK(test2 == 0);
- }
- ++runs;
- m.unlock();
- }
- class Pred {
- int & i_;
- public:
- explicit Pred(int& i) :
- i_(i)
- {}
- bool operator()()
- { return i_ != 0; }
- };
- void fn3( boost::fibers::mutex & m, boost::fibers::condition_variable_any & cv) {
- m.lock();
- BOOST_CHECK(test2 == 0);
- test1 = 1;
- cv.notify_one();
- std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now();
- std::chrono::steady_clock::time_point t = t0 + ms(250);
- bool r = cv.wait_until(m, t, Pred(test2));
- std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
- if (runs == 0) {
- BOOST_CHECK(t1 - t0 < ms(250));
- BOOST_CHECK(test2 != 0);
- BOOST_CHECK(r);
- } else {
- BOOST_CHECK(t1 - t0 - ms(250) < ms(250+100));
- BOOST_CHECK(test2 == 0);
- BOOST_CHECK(!r);
- }
- ++runs;
- m.unlock();
- }
- void fn4( boost::fibers::mutex & m, boost::fibers::condition_variable_any & cv) {
- m.lock();
- BOOST_CHECK(test2 == 0);
- test1 = 1;
- cv.notify_one();
- std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now();
- int count=0;
- while (test2 == 0 && cv.wait_for(m, ms(250)) == boost::fibers::cv_status::no_timeout)
- count++;
- std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
- if (runs == 0) {
- BOOST_CHECK(t1 - t0 < ms(250));
- BOOST_CHECK(test2 != 0);
- } else {
- BOOST_CHECK(t1 - t0 - ms(250) < ms(count*250+100+1000));
- BOOST_CHECK(test2 == 0);
- }
- ++runs;
- m.unlock();
- }
- void fn5( boost::fibers::mutex & m, boost::fibers::condition_variable_any & cv) {
- m.lock();
- BOOST_CHECK(test2 == 0);
- test1 = 1;
- cv.notify_one();
- std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now();
- int count=0;
- cv.wait_for(m, ms(250), Pred(test2));
- count++;
- std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
- if (runs == 0) {
- BOOST_CHECK(t1 - t0 < ms(250+1000));
- BOOST_CHECK(test2 != 0);
- } else {
- BOOST_CHECK(t1 - t0 - ms(250) < ms(count*250+100));
- BOOST_CHECK(test2 == 0);
- }
- ++runs;
- m.unlock();
- }
- void do_test_condition_wait() {
- test1 = 0;
- test2 = 0;
- runs = 0;
- boost::fibers::mutex m;
- boost::fibers::condition_variable_any cv;
- m.lock();
- boost::fibers::fiber f( boost::fibers::launch::dispatch, & fn1, std::ref( m), std::ref( cv) );
- BOOST_CHECK(test1 == 0);
- while (test1 == 0)
- cv.wait(m);
- BOOST_CHECK(test1 != 0);
- test2 = 1;
- m.unlock();
- cv.notify_one();
- f.join();
- }
- void test_condition_wait() {
- boost::fibers::fiber( boost::fibers::launch::dispatch, & do_test_condition_wait).join();
- do_test_condition_wait();
- }
- void do_test_condition_wait_until() {
- test1 = 0;
- test2 = 0;
- runs = 0;
- boost::fibers::mutex m;
- boost::fibers::condition_variable_any cv;
- {
- m.lock();
- boost::fibers::fiber f( boost::fibers::launch::dispatch, & fn2, std::ref( m), std::ref( cv) );
- BOOST_CHECK(test1 == 0);
- while (test1 == 0)
- cv.wait(m);
- BOOST_CHECK(test1 != 0);
- test2 = 1;
- m.unlock();
- cv.notify_one();
- f.join();
- }
- test1 = 0;
- test2 = 0;
- {
- m.lock();
- boost::fibers::fiber f( boost::fibers::launch::dispatch, & fn2, std::ref( m), std::ref( cv) );
- BOOST_CHECK(test1 == 0);
- while (test1 == 0)
- cv.wait(m);
- BOOST_CHECK(test1 != 0);
- m.unlock();
- f.join();
- }
- }
- void test_condition_wait_until() {
- boost::fibers::fiber( boost::fibers::launch::dispatch, & do_test_condition_wait_until).join();
- do_test_condition_wait_until();
- }
- void do_test_condition_wait_until_pred() {
- test1 = 0;
- test2 = 0;
- runs = 0;
- boost::fibers::mutex m;
- boost::fibers::condition_variable_any cv;
- {
- m.lock();
- boost::fibers::fiber f( boost::fibers::launch::dispatch, & fn3, std::ref( m), std::ref( cv) );
- BOOST_CHECK(test1 == 0);
- while (test1 == 0)
- cv.wait(m);
- BOOST_CHECK(test1 != 0);
- test2 = 1;
- m.unlock();
- cv.notify_one();
- f.join();
- }
- test1 = 0;
- test2 = 0;
- {
- m.lock();
- boost::fibers::fiber f( boost::fibers::launch::dispatch, & fn3, std::ref( m), std::ref( cv) );
- BOOST_CHECK(test1 == 0);
- while (test1 == 0)
- cv.wait(m);
- BOOST_CHECK(test1 != 0);
- m.unlock();
- f.join();
- }
- }
- void test_condition_wait_until_pred() {
- boost::fibers::fiber( boost::fibers::launch::dispatch, & do_test_condition_wait_until_pred).join();
- do_test_condition_wait_until_pred();
- }
- void do_test_condition_wait_for() {
- test1 = 0;
- test2 = 0;
- runs = 0;
- boost::fibers::mutex m;
- boost::fibers::condition_variable_any cv;
- {
- m.lock();
- boost::fibers::fiber f( boost::fibers::launch::dispatch, & fn4, std::ref( m), std::ref( cv) );
- BOOST_CHECK(test1 == 0);
- while (test1 == 0)
- cv.wait(m);
- BOOST_CHECK(test1 != 0);
- test2 = 1;
- m.unlock();
- cv.notify_one();
- f.join();
- }
- test1 = 0;
- test2 = 0;
- {
- m.lock();
- boost::fibers::fiber f( boost::fibers::launch::dispatch, & fn4, std::ref( m), std::ref( cv) );
- BOOST_CHECK(test1 == 0);
- while (test1 == 0)
- cv.wait(m);
- BOOST_CHECK(test1 != 0);
- m.unlock();
- f.join();
- }
- }
- void test_condition_wait_for() {
- boost::fibers::fiber( boost::fibers::launch::dispatch, & do_test_condition_wait_for).join();
- do_test_condition_wait_for();
- }
- void do_test_condition_wait_for_pred() {
- test1 = 0;
- test2 = 0;
- runs = 0;
- boost::fibers::mutex m;
- boost::fibers::condition_variable_any cv;
- {
- m.lock();
- boost::fibers::fiber f( boost::fibers::launch::dispatch, & fn5, std::ref( m), std::ref( cv) );
- BOOST_CHECK(test1 == 0);
- while (test1 == 0)
- cv.wait(m);
- BOOST_CHECK(test1 != 0);
- test2 = 1;
- m.unlock();
- cv.notify_one();
- f.join();
- }
- test1 = 0;
- test2 = 0;
- {
- m.lock();
- boost::fibers::fiber f( boost::fibers::launch::dispatch, & fn5, std::ref( m), std::ref( cv) );
- BOOST_CHECK(test1 == 0);
- while (test1 == 0)
- cv.wait(m);
- BOOST_CHECK(test1 != 0);
- m.unlock();
- f.join();
- }
- }
- void test_condition_wait_for_pred() {
- boost::fibers::fiber( boost::fibers::launch::dispatch, & do_test_condition_wait_for_pred).join();
- do_test_condition_wait_for_pred();
- }
- boost::unit_test::test_suite * init_unit_test_suite( int, char* []) {
- boost::unit_test::test_suite * test =
- BOOST_TEST_SUITE("Boost.Fiber: condition_variable_any test suite");
- test->add( BOOST_TEST_CASE( & test_one_waiter_notify_one) );
- test->add( BOOST_TEST_CASE( & test_two_waiter_notify_one) );
- test->add( BOOST_TEST_CASE( & test_two_waiter_notify_all) );
- test->add( BOOST_TEST_CASE( & test_condition_wait) );
- test->add( BOOST_TEST_CASE( & test_condition_wait_until) );
- test->add( BOOST_TEST_CASE( & test_condition_wait_until_pred) );
- test->add( BOOST_TEST_CASE( & test_condition_wait_for) );
- test->add( BOOST_TEST_CASE( & test_condition_wait_for_pred) );
- return test;
- }
|