2
0
mirror of https://github.com/boostorg/thread.git synced 2026-01-24 18:32:32 +00:00
Files
thread/test/test_condition.cpp
Eric Niebler 2a1e4496ba Merged revisions 41399-41442 via svnmerge from
https://svn.boost.org/svn/boost/trunk

........
  r41400 | igaztanaga | 2007-11-26 08:34:13 -0800 (Mon, 26 Nov 2007) | 1 line
  
  Added missing #include <typeinfo>
........
  r41401 | anthonyw | 2007-11-26 09:01:08 -0800 (Mon, 26 Nov 2007) | 1 line
  
  once_flag uses zero-initialization on POSIX as well as windows
........
  r41402 | niels_dekker | 2007-11-26 09:36:52 -0800 (Mon, 26 Nov 2007) | 1 line
  
  Marked value_init_test failures on Borland C++ as "expected failures", as discussed with Fernando Cacciola.
........
  r41404 | rwgk | 2007-11-26 12:46:28 -0800 (Mon, 26 Nov 2007) | 1 line
  
  g++ 4.3.0 compatibility (4.3.0 20071125 (experimental))
........
  r41405 | anthonyw | 2007-11-26 13:15:04 -0800 (Mon, 26 Nov 2007) | 1 line
  
  reverted accidental checkin of new timed_wait functions on condition_variable
........
  r41406 | nasonov | 2007-11-26 13:29:04 -0800 (Mon, 26 Nov 2007) | 3 lines
  
  Remove redundant BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION.
........
  r41407 | chris_kohlhoff | 2007-11-26 13:29:38 -0800 (Mon, 26 Nov 2007) | 3 lines
  
  WinCE doesn't work with all multicast addresses, and even though it doesn't
  support the multicast::enable_loopback option you can still get the value.
........
  r41408 | dave | 2007-11-26 14:01:50 -0800 (Mon, 26 Nov 2007) | 2 lines
  
  Try to extend the workaround to SunPro 5.9, since we're marked as not working on 5.8
........
  r41409 | dave | 2007-11-26 17:40:01 -0800 (Mon, 26 Nov 2007) | 3 lines
  
  Allow Sun-5.7 and Sun-5.8 to fail loudly for Boost.Python so I can see
  what's really wrong.
........
  r41413 | anthonyw | 2007-11-27 06:24:29 -0800 (Tue, 27 Nov 2007) | 1 line
  
  add support for relative timeouts to condition timed_wait
........
  r41414 | grafik | 2007-11-27 09:53:56 -0800 (Tue, 27 Nov 2007) | 1 line
  
  Remove non-existent option info.
........
  r41415 | grafik | 2007-11-27 09:55:13 -0800 (Tue, 27 Nov 2007) | 1 line
  
  Add some utility output formatting functions.
........
  r41416 | grafik | 2007-11-27 09:57:15 -0800 (Tue, 27 Nov 2007) | 1 line
  
  Check empty string invariants, instead of assuming all strings are allocated. And reset strings when they are freed.
........
  r41417 | grafik | 2007-11-27 09:58:50 -0800 (Tue, 27 Nov 2007) | 1 line
  
  Fix buffer overrun bug in expanding @() subexpressions.
........
  r41418 | hkaiser | 2007-11-27 10:18:10 -0800 (Tue, 27 Nov 2007) | 1 line
  
  Wave: Fixed gcc warning, bumped version number.
........
  r41419 | djenkins | 2007-11-27 10:57:48 -0800 (Tue, 27 Nov 2007) | 1 line
  
  Changes for msvc-9.0 /clr flag
........
  r41420 | djenkins | 2007-11-27 10:59:06 -0800 (Tue, 27 Nov 2007) | 1 line
  
  Changes for msvc-9.0 /clr flag
........
  r41423 | niels_dekker | 2007-11-27 13:34:08 -0800 (Tue, 27 Nov 2007) | 1 line
  
  Added value_init test for struct as used in MSVC bug report regarding value-initialization.
........
  r41429 | noel_belcourt | 2007-11-27 18:27:13 -0800 (Tue, 27 Nov 2007) | 3 lines
  
  Fix pathscale rpath issue per Alain Minussi's suggestion.
........
  r41430 | grafik | 2007-11-27 23:08:13 -0800 (Tue, 27 Nov 2007) | 1 line
  
  Add test for result status values of simple actions, i.e. empty actions.
........
  r41431 | grafik | 2007-11-27 23:21:49 -0800 (Tue, 27 Nov 2007) | 19 lines
  
  build-system.jam
  * Reflect added start/end timestamps for actions in xml output. And update action rules for new args.
  
  execcmd.h
  * Add start/end timestamps to action timing info.
  
  execnt.c
  * Fix filetime_seconds calculation when time is larger than low 32 bit value.
  * Add calc of C time_t from Windows FILETIME.
  * Add start/end timestamps recording to action timing info.
  
  execunix.c
  * Add start/end timestamps recording to action timing info.
  
  jam.c
  * Change JAMDATE to use common ISO date format.
  
  make1.c
  * Redo __TIMING_RULE__ and __ACTION__RULE__ invocations to new argument ordering and added end/result timestamp values.
........
  r41432 | chris_kohlhoff | 2007-11-28 05:26:33 -0800 (Wed, 28 Nov 2007) | 3 lines
  
  Make async operations fail with an error if the socket descriptor doesn't
  fit into the select call's fd_set.
........
  r41433 | bgubenko | 2007-11-28 07:33:16 -0800 (Wed, 28 Nov 2007) | 1 line
  
  add "gcc*hpux*" toolset for serialization library bug on big endian platforms
........
  r41436 | niels_dekker | 2007-11-28 09:19:37 -0800 (Wed, 28 Nov 2007) | 1 line
  
  Added tests for two more struct types to value_init_test -- discussed with Fernando Cacciola
........
  r41439 | bgubenko | 2007-11-28 11:04:53 -0800 (Wed, 28 Nov 2007) | 1 line
  
  add "<linkflags>-lrt" for acc* toolsets
........
  r41440 | grafik | 2007-11-28 12:24:17 -0800 (Wed, 28 Nov 2007) | 1 line
  
  Fix for latest Doxygen namespace file names. And support for method groups. From Samuel Debionne.
........


[SVN r41444]
2007-11-28 22:03:59 +00:00

278 lines
7.2 KiB
C++

// Copyright (C) 2001-2003
// William E. Kempf
// Copyright (C) 2007 Anthony Williams
//
// 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)
#include <boost/thread/detail/config.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/test/unit_test.hpp>
#include <libs/thread/test/util.inl>
struct condition_test_data
{
condition_test_data() : notified(0), awoken(0) { }
boost::mutex mutex;
boost::condition_variable condition;
int notified;
int awoken;
};
void condition_test_thread(condition_test_data* data)
{
boost::mutex::scoped_lock lock(data->mutex);
BOOST_CHECK(lock ? true : false);
while (!(data->notified > 0))
data->condition.wait(lock);
BOOST_CHECK(lock ? true : false);
data->awoken++;
}
struct cond_predicate
{
cond_predicate(int& var, int val) : _var(var), _val(val) { }
bool operator()() { return _var == _val; }
int& _var;
int _val;
};
void condition_test_waits(condition_test_data* data)
{
boost::mutex::scoped_lock lock(data->mutex);
BOOST_CHECK(lock ? true : false);
// Test wait.
while (data->notified != 1)
data->condition.wait(lock);
BOOST_CHECK(lock ? true : false);
BOOST_CHECK_EQUAL(data->notified, 1);
data->awoken++;
data->condition.notify_one();
// Test predicate wait.
data->condition.wait(lock, cond_predicate(data->notified, 2));
BOOST_CHECK(lock ? true : false);
BOOST_CHECK_EQUAL(data->notified, 2);
data->awoken++;
data->condition.notify_one();
// Test timed_wait.
boost::xtime xt = delay(10);
while (data->notified != 3)
data->condition.timed_wait(lock, xt);
BOOST_CHECK(lock ? true : false);
BOOST_CHECK_EQUAL(data->notified, 3);
data->awoken++;
data->condition.notify_one();
// Test predicate timed_wait.
xt = delay(10);
cond_predicate pred(data->notified, 4);
BOOST_CHECK(data->condition.timed_wait(lock, xt, pred));
BOOST_CHECK(lock ? true : false);
BOOST_CHECK(pred());
BOOST_CHECK_EQUAL(data->notified, 4);
data->awoken++;
data->condition.notify_one();
// Test predicate timed_wait with relative timeout
cond_predicate pred_rel(data->notified, 5);
BOOST_CHECK(data->condition.timed_wait(lock, boost::posix_time::seconds(10), pred_rel));
BOOST_CHECK(lock ? true : false);
BOOST_CHECK(pred_rel());
BOOST_CHECK_EQUAL(data->notified, 5);
data->awoken++;
data->condition.notify_one();
}
void do_test_condition_notify_one()
{
condition_test_data data;
boost::thread thread(bind(&condition_test_thread, &data));
{
boost::mutex::scoped_lock lock(data.mutex);
BOOST_CHECK(lock ? true : false);
data.notified++;
data.condition.notify_one();
}
thread.join();
BOOST_CHECK_EQUAL(data.awoken, 1);
}
void test_condition_notify_one()
{
timed_test(&do_test_condition_notify_one, 100, execution_monitor::use_mutex);
}
void do_test_condition_notify_all()
{
const int NUMTHREADS = 5;
boost::thread_group threads;
condition_test_data data;
try
{
for (int i = 0; i < NUMTHREADS; ++i)
threads.create_thread(bind(&condition_test_thread, &data));
{
boost::mutex::scoped_lock lock(data.mutex);
BOOST_CHECK(lock ? true : false);
data.notified++;
data.condition.notify_all();
}
threads.join_all();
}
catch(...)
{
threads.interrupt_all();
threads.join_all();
throw;
}
BOOST_CHECK_EQUAL(data.awoken, NUMTHREADS);
}
void test_condition_notify_all()
{
// We should have already tested notify_one here, so
// a timed test with the default execution_monitor::use_condition
// should be OK, and gives the fastest performance
timed_test(&do_test_condition_notify_all, 100);
}
void do_test_condition_waits()
{
condition_test_data data;
boost::thread thread(bind(&condition_test_waits, &data));
{
boost::mutex::scoped_lock lock(data.mutex);
BOOST_CHECK(lock ? true : false);
boost::thread::sleep(delay(1));
data.notified++;
data.condition.notify_one();
while (data.awoken != 1)
data.condition.wait(lock);
BOOST_CHECK(lock ? true : false);
BOOST_CHECK_EQUAL(data.awoken, 1);
boost::thread::sleep(delay(1));
data.notified++;
data.condition.notify_one();
while (data.awoken != 2)
data.condition.wait(lock);
BOOST_CHECK(lock ? true : false);
BOOST_CHECK_EQUAL(data.awoken, 2);
boost::thread::sleep(delay(1));
data.notified++;
data.condition.notify_one();
while (data.awoken != 3)
data.condition.wait(lock);
BOOST_CHECK(lock ? true : false);
BOOST_CHECK_EQUAL(data.awoken, 3);
boost::thread::sleep(delay(1));
data.notified++;
data.condition.notify_one();
while (data.awoken != 4)
data.condition.wait(lock);
BOOST_CHECK(lock ? true : false);
BOOST_CHECK_EQUAL(data.awoken, 4);
boost::thread::sleep(delay(1));
data.notified++;
data.condition.notify_one();
while (data.awoken != 5)
data.condition.wait(lock);
BOOST_CHECK(lock ? true : false);
BOOST_CHECK_EQUAL(data.awoken, 5);
}
thread.join();
BOOST_CHECK_EQUAL(data.awoken, 5);
}
void test_condition_waits()
{
// We should have already tested notify_one here, so
// a timed test with the default execution_monitor::use_condition
// should be OK, and gives the fastest performance
timed_test(&do_test_condition_waits, 12);
}
void do_test_condition_wait_is_a_interruption_point()
{
condition_test_data data;
boost::thread thread(bind(&condition_test_thread, &data));
thread.interrupt();
thread.join();
BOOST_CHECK_EQUAL(data.awoken,0);
}
void test_condition_wait_is_a_interruption_point()
{
timed_test(&do_test_condition_wait_is_a_interruption_point, 1);
}
bool fake_predicate()
{
return false;
}
void do_test_timed_wait_times_out()
{
boost::condition_variable cond;
boost::mutex m;
boost::posix_time::seconds const delay(5);
boost::mutex::scoped_lock lock(m);
boost::system_time const start=boost::get_system_time();
bool const res=cond.timed_wait(lock,delay,fake_predicate);
boost::system_time const end=boost::get_system_time();
BOOST_CHECK(!res);
BOOST_CHECK((delay-boost::posix_time::milliseconds(10))<=(end-start));
}
void test_timed_wait_times_out()
{
timed_test(&do_test_timed_wait_times_out, 15);
}
boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
{
boost::unit_test_framework::test_suite* test =
BOOST_TEST_SUITE("Boost.Threads: condition test suite");
test->add(BOOST_TEST_CASE(&test_condition_notify_one));
test->add(BOOST_TEST_CASE(&test_condition_notify_all));
test->add(BOOST_TEST_CASE(&test_condition_waits));
test->add(BOOST_TEST_CASE(&test_condition_wait_is_a_interruption_point));
test->add(BOOST_TEST_CASE(&test_timed_wait_times_out));
return test;
}