2
0
mirror of https://github.com/boostorg/thread.git synced 2026-02-04 22:02:10 +00:00

Compare commits

..

3 Commits

Author SHA1 Message Date
William E. Kempf
c09b4110d7 Fixed several bugs in the new unit test code
[SVN r14788]
2002-08-12 14:27:00 +00:00
William E. Kempf
b75a32b913 Fixed xtime bugs
[SVN r13865]
2002-05-14 21:11:36 +00:00
nobody
c6b8f21bde This commit was manufactured by cvs2svn to create branch 'RC_1_28_0'.
[SVN r13795]
2002-05-10 04:34:27 +00:00
13 changed files with 587 additions and 678 deletions

View File

@@ -38,7 +38,7 @@ if $(NT) && ! $(PTW32)
{
dll boost_threadmon
: ../src/threadmon.cpp
: <sysinclude>$(BOOST_ROOT)
: <include>$(BOOST_ROOT)
<threading>multi
: debug release <runtime-link>static/dynamic
;
@@ -53,7 +53,7 @@ CPP_SOURCES =
lib boost_thread
: ../src/$(CPP_SOURCES).cpp
: <sysinclude>$(BOOST_ROOT)
: <include>$(BOOST_ROOT)
<threading>multi
$(pthreads-win32)
: debug release <runtime-link>static/dynamic

View File

@@ -35,7 +35,7 @@ exe monitor
: monitor/monitor.cpp
<lib>../build/boost_thread
$(threadmon)
: <sysinclude>$(BOOST_ROOT)
: <include>$(BOOST_ROOT)
$(pthreads-win32)
<threading>multi
: debug release <runtime-link>static/dynamic
@@ -48,7 +48,7 @@ exe starvephil
: starvephil/starvephil.cpp
<lib>../build/boost_thread
$(threadmon)
: <sysinclude>$(BOOST_ROOT)
: <include>$(BOOST_ROOT)
$(pthreads-win32)
<threading>multi
: debug release <runtime-link>static/dynamic
@@ -61,7 +61,7 @@ exe tennis
: tennis/tennis.cpp
<lib>../build/boost_thread
$(threadmon)
: <sysinclude>$(BOOST_ROOT)
: <include>$(BOOST_ROOT)
$(pthreads-win32)
<threading>multi
: debug release <runtime-link>static/dynamic

View File

@@ -39,12 +39,11 @@ struct xtime
int_fast32_t nsec;
};
int xtime_get(struct xtime* xtp, int clock_type);
inline int xtime_cmp(const xtime& xt1, const xtime& xt2)
int xtime_get(xtime* xtp, int clock_type);
inline int xtime_cmp(const xtime& xtp1, const xtime& xtp2)
{
int res = (int)(xt1.sec - xt2.sec);
if (res == 0)
res = (int)(xt1.nsec - xt2.nsec);
int res = xtp1.sec - xtp2.sec;
if (res == 0) res = xtp1.nsec - xtp2.nsec;
return res;
}

View File

@@ -17,6 +17,8 @@
#include <cassert>
#include "timeconv.inl"
#include <iostream>
#if defined(BOOST_HAS_WINTHREADS)
# ifndef NOMINMAX
# define NOMINMAX
@@ -242,71 +244,103 @@ void condition::do_wait()
}
}
//#define DOFIX
bool condition::do_timed_wait(const xtime& xt)
{
int milliseconds;
to_duration(xt, milliseconds);
bool ret = false;
unsigned int res = 0;
unsigned int res = 0;
res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_queue), milliseconds);
assert(res != WAIT_FAILED && res != WAIT_ABANDONED);
#if defined(DOFIX)
for (;;)
{
#endif
int milliseconds;
to_duration(xt, milliseconds);
if (milliseconds < 0)
std::cout << "milliseconds < 0" << std::endl;
if (milliseconds > 10000)
std::cout << "milliseconds > 10000" << std::endl;
// assert(milliseconds > 0);
bool ret = (res == WAIT_OBJECT_0);
res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_queue), milliseconds);
assert(res != WAIT_FAILED && res != WAIT_ABANDONED);
unsigned was_waiting=0;
unsigned was_gone=0;
ret = (res == WAIT_OBJECT_0);
res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_mutex), INFINITE);
assert(res == WAIT_OBJECT_0);
was_waiting = m_waiting;
was_gone = m_gone;
if (was_waiting != 0)
{
if (!ret) // timeout
{
if (m_blocked != 0)
--m_blocked;
else
++m_gone; // count spurious wakeups
}
if (--m_waiting == 0)
{
if (m_blocked != 0)
{
res = ReleaseSemaphore(reinterpret_cast<HANDLE>(m_gate), 1, 0); // open m_gate
assert(res);
was_waiting = 0;
}
else if (m_gone != 0)
m_gone = 0;
}
}
else if (++m_gone == (std::numeric_limits<unsigned>::max() / 2))
{
// timeout occured, normalize the m_gone count
// this may occur if many calls to wait with a timeout are made and
// no call to notify_* is made
res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_gate), INFINITE);
assert(res == WAIT_OBJECT_0);
m_blocked -= m_gone;
res = ReleaseSemaphore(reinterpret_cast<HANDLE>(m_gate), 1, 0);
assert(res);
m_gone = 0;
}
res = ReleaseMutex(reinterpret_cast<HANDLE>(m_mutex));
assert(res);
#if defined(DOFIX)
if (ret)
break;
if (was_waiting == 1)
{
for (/**/ ; was_gone; --was_gone)
{
// better now than spurious later
res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_queue), INFINITE);
assert(res == WAIT_OBJECT_0);
}
res = ReleaseSemaphore(reinterpret_cast<HANDLE>(m_gate), 1, 0);
assert(res);
}
xtime now;
xtime_get(&now, TIME_UTC);
if (xtime_cmp(xt, now) >= 0)
break;
}
#endif
unsigned was_waiting=0;
unsigned was_gone=0;
res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_mutex), INFINITE);
assert(res == WAIT_OBJECT_0);
was_waiting = m_waiting;
was_gone = m_gone;
if (was_waiting != 0)
{
if (!ret) // timeout?
{
if (m_blocked != 0)
--m_blocked;
else
++m_gone; // count spurious wakeups
}
if (--m_waiting == 0)
{
if (m_blocked != 0)
{
res = ReleaseSemaphore(reinterpret_cast<HANDLE>(m_gate), 1, 0); // open m_gate
assert(res);
was_waiting = 0;
}
else if (m_gone != 0)
m_gone = 0;
}
}
else if (++m_gone == (std::numeric_limits<unsigned>::max() / 2))
{
// timeout occured, normalize the m_gone count
// this may occur if many calls to wait with a timeout are made and
// no call to notify_* is made
res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_gate), INFINITE);
assert(res == WAIT_OBJECT_0);
m_blocked -= m_gone;
res = ReleaseSemaphore(reinterpret_cast<HANDLE>(m_gate), 1, 0);
assert(res);
m_gone = 0;
}
res = ReleaseMutex(reinterpret_cast<HANDLE>(m_mutex));
assert(res);
if (was_waiting == 1)
{
for (/**/ ; was_gone; --was_gone)
{
// better now than spurious later
res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_queue), INFINITE);
assert(res == WAIT_OBJECT_0);
}
res = ReleaseSemaphore(reinterpret_cast<HANDLE>(m_gate), 1, 0);
assert(res);
}
// if (!ret)
// {
// xtime now;
// xtime_get(&now, TIME_UTC);
// if (xtime_cmp(xt, now) >= 0)
// break;
// }
return ret;
}

View File

@@ -12,6 +12,8 @@
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/thread/condition.hpp>
#include <exception>
#include <cassert>
#if defined(BOOST_HAS_WINTHREADS)
@@ -72,6 +74,8 @@ static OSStatus thread_proxy(void* param)
}
catch (...)
{
using namespace std;
terminate();
}
#if defined(BOOST_HAS_MPTASKS)
::boost::detail::thread_cleanup();
@@ -191,44 +195,37 @@ void thread::join()
void thread::sleep(const xtime& xt)
{
for (;;)
{
#if defined(BOOST_HAS_WINTHREADS)
int milliseconds;
to_duration(xt, milliseconds);
Sleep(milliseconds);
xtime xt2;
xtime_get(&xt2, TIME_UTC);
int milliseconds;
to_duration(xt, milliseconds);
Sleep(milliseconds);
#elif defined(BOOST_HAS_PTHREADS)
# if defined(BOOST_HAS_PTHREAD_DELAY_NP)
timespec ts;
to_timespec_duration(xt, ts);
int res = 0;
res = pthread_delay_np(&ts);
assert(res == 0);
timespec ts;
to_timespec_duration(xt, ts);
int res = 0;
res = pthread_delay_np(&ts);
assert(res == 0);
# elif defined(BOOST_HAS_NANOSLEEP)
timespec ts;
to_timespec_duration(xt, ts);
timespec ts;
to_timespec_duration(xt, ts);
// nanosleep takes a timespec that is an offset, not
// an absolute time.
nanosleep(&ts, 0);
// nanosleep takes a timespec that is an offset, not
// an absolute time.
nanosleep(&ts, 0);
# else
mutex mx;
mutex::scoped_lock lock(mx);
condition cond;
cond.timed_wait(lock, xt);
mutex mx;
mutex::scoped_lock lock(mx);
condition cond;
cond.timed_wait(lock, xt);
# endif
#elif defined(BOOST_HAS_MPTASKS)
int microseconds;
to_microduration(xt, microseconds);
Duration lMicroseconds(kDurationMicrosecond * microseconds);
AbsoluteTime sWakeTime(DurationToAbsolute(lMicroseconds));
threads::mac::detail::safe_delay_until(&sWakeTime);
int microseconds;
to_microduration(xt, microseconds);
Duration lMicroseconds(kDurationMicrosecond * microseconds);
AbsoluteTime sWakeTime(DurationToAbsolute(lMicroseconds));
threads::mac::detail::safe_delay_until(&sWakeTime);
#endif
if (xtime_cmp(xt, xt2) >= 0)
return;
}
}
void thread::yield()

View File

@@ -59,7 +59,7 @@ namespace {
res = boost::xtime_get(&cur, boost::TIME_UTC);
assert(res == boost::TIME_UTC);
if (xt.sec < cur.sec || (xt.sec == cur.sec && xt.nsec < cur.nsec))
if (boost::xtime_cmp(xt, cur) < 0)
{
ts.tv_sec = 0;
ts.tv_nsec = 0;
@@ -90,7 +90,7 @@ namespace {
res = boost::xtime_get(&cur, boost::TIME_UTC);
assert(res == boost::TIME_UTC);
if (xt.sec < cur.sec || (xt.sec == cur.sec && xt.nsec < cur.nsec))
if (boost::xtime_cmp(xt, cur) <= 0)
milliseconds = 0;
else
{
@@ -107,7 +107,7 @@ namespace {
res = boost::xtime_get(&cur, boost::TIME_UTC);
assert(res == boost::TIME_UTC);
if (xt.sec < cur.sec || (xt.sec == cur.sec && xt.nsec < cur.nsec))
if (boost::xtime_cmp(xt, cur) <= 0)
microseconds = 0;
else
{

View File

@@ -26,17 +26,14 @@ subproject libs/thread/test ;
SEARCH on <module@>threads.jam = $(BOOST_ROOT)/libs/thread/build ;
include <module@>threads.jam ;
sources = test.cpp test_thread.cpp test_mutex.cpp test_condition.cpp test_tss.cpp test_once.cpp ;
#######################
# Declare the Boost.Threads unit test program test_thread.
unit-test test_thread
: $(sources)
: test_thread.cpp
<lib>../build/boost_thread
<lib>../../test/build/unit_test_framework
$(threadmon)
: <sysinclude>$(BOOST_ROOT)
: <include>$(BOOST_ROOT)
$(pthreads-win32)
<threading>multi
: debug release <runtime-link>static/dynamic

View File

@@ -1,20 +0,0 @@
#include <boost/test/unit_test.hpp>
extern boost::unit_test_framework::test_suite* thread_tests();
extern boost::unit_test_framework::test_suite* mutex_tests();
extern boost::unit_test_framework::test_suite* condition_tests();
extern boost::unit_test_framework::test_suite* tss_tests();
extern boost::unit_test_framework::test_suite* once_tests();
boost::unit_test_framework::test_suite* init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads test suite");
test->add(thread_tests());
test->add(mutex_tests());
test->add(condition_tests());
test->add(tss_tests());
test->add(once_tests());
return test;
}

View File

@@ -1,191 +0,0 @@
#include <boost/thread/condition.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/test/unit_test.hpp>
namespace
{
struct condition_test_data
{
condition_test_data() : notified(0), awoken(0) { }
boost::mutex mutex;
boost::condition condition;
int notified;
int awoken;
};
void condition_test_thread(void* param)
{
condition_test_data* data = static_cast<condition_test_data*>(param);
boost::mutex::scoped_lock lock(data->mutex);
BOOST_CHECK(lock);
while (!(data->notified > 0))
data->condition.wait(lock);
BOOST_CHECK(lock);
data->awoken++;
}
class thread_adapter
{
public:
thread_adapter(void (*func)(void*), void* param) : _func(func), _param(param) { }
void operator()() const { _func(_param); }
private:
void (*_func)(void*);
void* _param;
};
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(void* param)
{
condition_test_data* data = static_cast<condition_test_data*>(param);
boost::mutex::scoped_lock lock(data->mutex);
BOOST_CHECK(lock);
// Test wait.
while (data->notified != 1)
data->condition.wait(lock);
BOOST_CHECK(lock);
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);
BOOST_CHECK_EQUAL(data->notified, 2);
data->awoken++;
data->condition.notify_one();
// Test timed_wait.
boost::xtime xt;
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.nsec += 100000000;
while (data->notified != 3)
data->condition.timed_wait(lock, xt);
BOOST_CHECK(lock);
BOOST_CHECK_EQUAL(data->notified, 3);
data->awoken++;
data->condition.notify_one();
// Test predicate timed_wait.
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.sec += 2;
BOOST_CHECK(data->condition.timed_wait(lock, xt, cond_predicate(data->notified, 4)));
BOOST_CHECK(lock);
BOOST_CHECK_EQUAL(data->notified, 4);
data->awoken++;
}
}
void test_condition_notify_one()
{
condition_test_data data;
boost::thread thread(thread_adapter(&condition_test_thread, &data));
{
boost::mutex::scoped_lock lock(data.mutex);
BOOST_CHECK(lock);
data.notified++;
data.condition.notify_one();
}
thread.join();
BOOST_CHECK_EQUAL(data.awoken, 1);
}
void test_condition_notify_all()
{
const int NUMTHREADS = 5;
boost::thread_group threads;
condition_test_data data;
for (int i = 0; i < NUMTHREADS; ++i)
threads.create_thread(thread_adapter(&condition_test_thread, &data));
{
boost::mutex::scoped_lock lock(data.mutex);
BOOST_CHECK(lock);
data.notified++;
data.condition.notify_all();
}
threads.join_all();
BOOST_CHECK_EQUAL(data.awoken, NUMTHREADS);
}
void test_condition_waits()
{
condition_test_data data;
boost::thread thread(thread_adapter(&condition_test_waits, &data));
boost::xtime xt;
{
boost::mutex::scoped_lock lock(data.mutex);
BOOST_CHECK(lock);
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
data.notified++;
data.condition.notify_one();
while (data.awoken != 1)
data.condition.wait(lock);
BOOST_CHECK_EQUAL(data.awoken, 1);
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
data.notified++;
data.condition.notify_one();
while (data.awoken != 2)
data.condition.wait(lock);
BOOST_CHECK_EQUAL(data.awoken, 2);
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
data.notified++;
data.condition.notify_one();
while (data.awoken != 3)
data.condition.wait(lock);
BOOST_CHECK_EQUAL(data.awoken, 3);
}
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
data.notified++;
data.condition.notify_one();
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
thread.join();
BOOST_CHECK_EQUAL(data.awoken, 4);
}
boost::unit_test_framework::test_suite* condition_tests()
{
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));
return test;
}

View File

@@ -1,184 +0,0 @@
#include <boost/thread/mutex.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/thread/condition.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_suite_ex.hpp>
template <typename M>
struct test_lock
{
typedef M mutex_type;
typedef typename M::scoped_lock lock_type;
void operator()()
{
mutex_type mutex;
boost::condition condition;
// Test the lock's constructors.
{
lock_type lock(mutex, false);
BOOST_CHECK(!lock);
}
lock_type lock(mutex);
BOOST_CHECK(lock);
// Construct and initialize an xtime for a fast time out.
boost::xtime xt;
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.nsec += 100000000;
// Test the lock and the mutex with condition variables.
// No one is going to notify this condition variable. We expect to
// time out.
BOOST_CHECK(!condition.timed_wait(lock, xt));
BOOST_CHECK(lock);
// Test the lock and unlock methods.
lock.unlock();
BOOST_CHECK(!lock);
lock.lock();
BOOST_CHECK(lock);
}
};
template <typename M>
struct test_trylock
{
typedef M mutex_type;
typedef typename M::scoped_try_lock try_lock_type;
void operator()()
{
mutex_type mutex;
boost::condition condition;
// Test the lock's constructors.
{
try_lock_type lock(mutex);
BOOST_CHECK(lock);
}
{
try_lock_type lock(mutex, false);
BOOST_CHECK(!lock);
}
try_lock_type lock(mutex, true);
BOOST_CHECK(lock);
// Construct and initialize an xtime for a fast time out.
boost::xtime xt;
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.nsec += 100000000;
// Test the lock and the mutex with condition variables.
// No one is going to notify this condition variable. We expect to
// time out.
BOOST_CHECK(!condition.timed_wait(lock, xt));
BOOST_CHECK(lock);
// Test the lock, unlock and trylock methods.
lock.unlock();
BOOST_CHECK(!lock);
lock.lock();
BOOST_CHECK(lock);
lock.unlock();
BOOST_CHECK(!lock);
BOOST_CHECK(lock.try_lock());
BOOST_CHECK(lock);
}
};
template <typename M>
struct test_timedlock
{
typedef M mutex_type;
typedef typename M::scoped_timed_lock timed_lock_type;
void operator()()
{
mutex_type mutex;
boost::condition condition;
// Test the lock's constructors.
{
// Construct and initialize an xtime for a fast time out.
boost::xtime xt;
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.nsec += 100000000;
timed_lock_type lock(mutex, xt);
BOOST_CHECK(lock);
}
{
timed_lock_type lock(mutex, false);
BOOST_CHECK(!lock);
}
timed_lock_type lock(mutex, true);
BOOST_CHECK(lock);
// Construct and initialize an xtime for a fast time out.
boost::xtime xt;
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.nsec += 100000000;
// Test the lock and the mutex with condition variables.
// No one is going to notify this condition variable. We expect to
// time out.
BOOST_CHECK(!condition.timed_wait(lock, xt));
BOOST_CHECK(lock);
// Test the lock, unlock and timedlock methods.
lock.unlock();
BOOST_CHECK(!lock);
lock.lock();
BOOST_CHECK(lock);
lock.unlock();
BOOST_CHECK(!lock);
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.nsec += 100000000;
BOOST_CHECK(lock.timed_lock(xt));
}
};
template <typename M>
struct test_recursive_lock
{
typedef M mutex;
void operator()()
{
mutex mx;
mutex::scoped_lock lock1(mx);
mutex::scoped_lock lock2(mx);
}
};
boost::unit_test_framework::test_suite* mutex_tests()
{
boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads: mutex test suite");
test->add(BOOST_TEST_CASE(test_lock<boost::mutex>()));
test->add(BOOST_TEST_CASE(test_lock<boost::try_mutex>()));
test->add(BOOST_TEST_CASE(test_trylock<boost::try_mutex>()));
test->add(BOOST_TEST_CASE(test_lock<boost::timed_mutex>()));
test->add(BOOST_TEST_CASE(test_trylock<boost::timed_mutex>()));
test->add(BOOST_TEST_CASE(test_timedlock<boost::timed_mutex>()));
test->add(BOOST_TEST_CASE(test_lock<boost::recursive_mutex>()));
test->add(BOOST_TEST_CASE(test_recursive_lock<boost::recursive_mutex>()));
test->add(BOOST_TEST_CASE(test_lock<boost::recursive_try_mutex>()));
test->add(BOOST_TEST_CASE(test_trylock<boost::recursive_try_mutex>()));
test->add(BOOST_TEST_CASE(test_recursive_lock<boost::recursive_try_mutex>()));
test->add(BOOST_TEST_CASE(test_lock<boost::recursive_timed_mutex>()));
test->add(BOOST_TEST_CASE(test_trylock<boost::recursive_timed_mutex>()));
test->add(BOOST_TEST_CASE(test_timedlock<boost::recursive_timed_mutex>()));
test->add(BOOST_TEST_CASE(test_recursive_lock<boost::recursive_timed_mutex>()));
return test;
}

View File

@@ -1,39 +0,0 @@
#include <boost/thread/once.hpp>
#include <boost/thread/thread.hpp>
#include <boost/test/unit_test.hpp>
namespace
{
int once_value = 0;
boost::once_flag once = BOOST_ONCE_INIT;
void init_once_value()
{
once_value++;
}
void test_once_thread()
{
boost::call_once(&init_once_value, once);
}
}
void test_once()
{
const int NUMTHREADS=5;
boost::thread_group threads;
for (int i=0; i<NUMTHREADS; ++i)
threads.create_thread(&test_once_thread);
threads.join_all();
BOOST_CHECK_EQUAL(once_value, 1);
}
boost::unit_test_framework::test_suite* once_tests()
{
boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads: once test suite");
test->add(BOOST_TEST_CASE(&test_once));
return test;
}

View File

@@ -1,89 +1,464 @@
#include <list>
#include <boost/thread/mutex.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/tss.hpp>
#include <boost/thread/once.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/test/unit_test.hpp>
#define BOOST_INCLUDE_MAIN
#include <boost/test/test_tools.hpp>
namespace
#if defined(BOOST_HAS_WINTHREADS)
# include <windows.h>
#endif
template <typename M>
void test_lock(M* dummy=0)
{
inline bool xtime_in_range(boost::xtime& xt, int less_seconds, int greater_seconds)
typedef M mutex_type;
typedef typename M::scoped_lock lock_type;
mutex_type mutex;
boost::condition condition;
// Test the lock's constructors.
{
lock_type lock(mutex, false);
BOOST_TEST(!lock);
}
lock_type lock(mutex);
BOOST_TEST(lock);
// Construct and initialize an xtime for a fast time out.
boost::xtime xt;
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.nsec += 100000000;
// Test the lock and the mutex with condition variables.
// No one is going to notify this condition variable. We expect to
// time out.
BOOST_TEST(condition.timed_wait(lock, xt) == false);
BOOST_TEST(lock);
// boost::xtime now;
// BOOST_TEST(boost::xtime_get(&now, boost::TIME_UTC) == boost::TIME_UTC);
// BOOST_TEST(boost::xtime_cmp(xt, now) >= 0);
// Test the lock and unlock methods.
lock.unlock();
BOOST_TEST(!lock);
lock.lock();
BOOST_TEST(lock);
}
template <typename M>
void test_trylock(M* dummy=0)
{
typedef M mutex_type;
typedef typename M::scoped_try_lock try_lock_type;
mutex_type mutex;
boost::condition condition;
// Test the lock's constructors.
{
try_lock_type lock(mutex);
BOOST_TEST(lock);
}
{
try_lock_type lock(mutex, false);
BOOST_TEST(!lock);
}
try_lock_type lock(mutex, true);
BOOST_TEST(lock);
// Construct and initialize an xtime for a fast time out.
boost::xtime xt;
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.nsec += 100000000;
// Test the lock and the mutex with condition variables.
// No one is going to notify this condition variable. We expect to
// time out.
BOOST_TEST(condition.timed_wait(lock, xt) == false);
BOOST_TEST(lock);
// Test the lock, unlock and trylock methods.
lock.unlock();
BOOST_TEST(!lock);
lock.lock();
BOOST_TEST(lock);
lock.unlock();
BOOST_TEST(!lock);
BOOST_TEST(lock.try_lock());
BOOST_TEST(lock);
}
template <typename M>
void test_timedlock(M* dummy=0)
{
typedef M mutex_type;
typedef typename M::scoped_timed_lock timed_lock_type;
mutex_type mutex;
boost::condition condition;
// Test the lock's constructors.
{
// Construct and initialize an xtime for a fast time out.
boost::xtime xt;
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.nsec += 100000000;
timed_lock_type lock(mutex, xt);
BOOST_TEST(lock);
}
{
timed_lock_type lock(mutex, false);
BOOST_TEST(!lock);
}
timed_lock_type lock(mutex, true);
BOOST_TEST(lock);
// Construct and initialize an xtime for a fast time out.
boost::xtime xt;
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.nsec += 100000000;
// Test the lock and the mutex with condition variables.
// No one is going to notify this condition variable. We expect to
// time out.
BOOST_TEST(condition.timed_wait(lock, xt) == false);
BOOST_TEST(lock);
// Test the lock, unlock and timedlock methods.
lock.unlock();
BOOST_TEST(!lock);
lock.lock();
BOOST_TEST(lock);
lock.unlock();
BOOST_TEST(!lock);
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.nsec += 100000000;
BOOST_TEST(lock.timed_lock(xt));
}
void test_mutex()
{
typedef boost::mutex mutex;
test_lock<mutex>();
}
void test_try_mutex()
{
typedef boost::try_mutex mutex;
test_lock<mutex>();
test_trylock<mutex>();
}
void test_timed_mutex()
{
typedef boost::timed_mutex mutex;
test_lock<mutex>();
test_trylock<mutex>();
test_timedlock<mutex>();
}
void test_recursive_mutex()
{
typedef boost::recursive_mutex mutex;
test_lock<mutex>();
mutex mx;
mutex::scoped_lock lock1(mx);
mutex::scoped_lock lock2(mx);
}
void test_recursive_try_mutex()
{
typedef boost::recursive_try_mutex mutex;
test_lock<mutex>();
test_trylock<mutex>();
mutex mx;
mutex::scoped_lock lock1(mx);
mutex::scoped_lock lock2(mx);
}
void test_recursive_timed_mutex()
{
typedef boost::recursive_timed_mutex mutex;
test_lock<mutex>();
test_trylock<mutex>();
test_timedlock<mutex>();
mutex mx;
mutex::scoped_lock lock1(mx);
mutex::scoped_lock lock2(mx);
}
struct condition_test_data
{
condition_test_data() : notified(0), awoken(0) { }
boost::mutex mutex;
boost::condition condition;
int notified;
int awoken;
};
void condition_test_thread(void* param)
{
condition_test_data* data = static_cast<condition_test_data*>(param);
boost::mutex::scoped_lock lock(data->mutex);
BOOST_TEST(lock);
while (!(data->notified > 0))
data->condition.wait(lock);
BOOST_TEST(lock);
data->awoken++;
}
class thread_adapter
{
public:
thread_adapter(void (*func)(void*), void* param) : _func(func), _param(param) { }
void operator()() const { _func(_param); }
private:
void (*_func)(void*);
void* _param;
};
void test_condition_notify_one()
{
condition_test_data data;
boost::thread thread(thread_adapter(&condition_test_thread, &data));
{
boost::mutex::scoped_lock lock(data.mutex);
BOOST_TEST(lock);
data.notified++;
data.condition.notify_one();
}
thread.join();
BOOST_TEST(data.awoken == 1);
}
void test_condition_notify_all()
{
const int NUMTHREADS = 5;
boost::thread_group threads;
condition_test_data data;
for (int i = 0; i < NUMTHREADS; ++i)
threads.create_thread(thread_adapter(&condition_test_thread, &data));
{
boost::mutex::scoped_lock lock(data.mutex);
BOOST_TEST(lock);
data.notified++;
data.condition.notify_all();
}
threads.join_all();
BOOST_TEST(data.awoken == NUMTHREADS);
}
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(void* param)
{
condition_test_data* data = static_cast<condition_test_data*>(param);
boost::mutex::scoped_lock lock(data->mutex);
BOOST_TEST(lock);
// Test wait.
while (data->notified != 1)
data->condition.wait(lock);
BOOST_TEST(lock);
BOOST_TEST(data->notified == 1);
data->awoken++;
data->condition.notify_one();
// Test predicate wait.
data->condition.wait(lock, cond_predicate(data->notified, 2));
BOOST_TEST(lock);
BOOST_TEST(data->notified == 2);
data->awoken++;
data->condition.notify_one();
// Test timed_wait.
boost::xtime xt;
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.nsec += 100000000;
while (data->notified != 3)
data->condition.timed_wait(lock, xt);
BOOST_TEST(lock);
BOOST_TEST(data->notified == 3);
data->awoken++;
data->condition.notify_one();
// Test predicate timed_wait.
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.sec += 2;
BOOST_TEST(data->condition.timed_wait(lock, xt, cond_predicate(data->notified, 4)));
BOOST_TEST(lock);
BOOST_TEST(data->notified == 4);
data->awoken++;
}
void test_condition_waits()
{
condition_test_data data;
boost::thread thread(thread_adapter(&condition_test_waits, &data));
boost::xtime xt;
{
boost::mutex::scoped_lock lock(data.mutex);
BOOST_TEST(lock);
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
data.notified++;
data.condition.notify_one();
while (data.awoken != 1)
data.condition.wait(lock);
BOOST_TEST(data.awoken == 1);
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
data.notified++;
data.condition.notify_one();
while (data.awoken != 2)
data.condition.wait(lock);
BOOST_TEST(data.awoken == 2);
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
data.notified++;
data.condition.notify_one();
while (data.awoken != 3)
data.condition.wait(lock);
BOOST_TEST(data.awoken == 3);
}
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
data.notified++;
data.condition.notify_one();
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
thread.join();
BOOST_TEST(data.awoken == 4);
}
void test_condition()
{
test_condition_notify_one();
test_condition_notify_all();
test_condition_waits();
}
boost::mutex tss_mutex;
int tss_instances = 0;
struct tss_value_t
{
tss_value_t()
{
boost::mutex::scoped_lock lock(tss_mutex);
++tss_instances;
value = 0;
}
~tss_value_t()
{
boost::mutex::scoped_lock lock(tss_mutex);
--tss_instances;
}
int value;
};
boost::thread_specific_ptr<tss_value_t> tss_value;
void test_tss_thread()
{
tss_value.reset(new tss_value_t());
for (int i=0; i<1000; ++i)
{
int& n = tss_value->value;
BOOST_TEST(n == i);
++n;
}
}
void test_tss()
{
const int NUMTHREADS=5;
boost::thread_group threads;
for (int i=0; i<NUMTHREADS; ++i)
threads.create_thread(&test_tss_thread);
threads.join_all();
BOOST_TEST(tss_instances == 0);
}
int once_value = 0;
boost::once_flag once = BOOST_ONCE_INIT;
void init_once_value()
{
once_value++;
}
void test_once_thread()
{
boost::call_once(&init_once_value, once);
}
void test_once()
{
const int NUMTHREADS=5;
boost::thread_group threads;
for (int i=0; i<NUMTHREADS; ++i)
threads.create_thread(&test_once_thread);
threads.join_all();
BOOST_TEST(once_value == 1);
}
int test_main(int, char*[])
{
for (int i = 0; i < 100; ++i)
{
boost::xtime cur;
BOOST_CHECK_EQUAL(boost::xtime_get(&cur, boost::TIME_UTC), boost::TIME_UTC);
boost::xtime less = cur;
less.sec += less_seconds;
boost::xtime greater = cur;
greater.sec += greater_seconds;
return (boost::xtime_cmp(xt, less) >= 0) && (boost::xtime_cmp(xt, greater) <= 0);
boost::xtime xt1, xt2;
BOOST_TEST(boost::xtime_get(&xt1, boost::TIME_UTC) == boost::TIME_UTC);
// for (int j = 0; j < 1000000000; ++j) ;
BOOST_TEST(boost::xtime_get(&xt2, boost::TIME_UTC) == boost::TIME_UTC);
BOOST_TEST(boost::xtime_cmp(xt1, xt2) <= 0);
}
int test_value;
void simple_thread()
{
test_value = 999;
}
struct thread_adapter
{
thread_adapter(void (*func)(boost::thread& parent), boost::thread& parent)
: func(func), parent(parent)
{
}
void operator()()
{
(*func)(parent);
}
void (*func)(boost::thread& parent);
boost::thread& parent;
};
void comparison_thread(boost::thread& parent)
{
boost::thread thrd;
BOOST_TEST(thrd != parent);
BOOST_TEST(thrd == boost::thread());
}
}
void test_sleep()
{
boost::xtime xt;
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
xt.sec += 5;
boost::thread::sleep(xt);
// Insure it's in a range instead of checking actual equality due to time lapse
BOOST_CHECK(xtime_in_range(xt, -1, 1));
}
void test_creation()
{
test_value = 0;
boost::thread thrd(&simple_thread);
thrd.join();
BOOST_CHECK_EQUAL(test_value, 999);
}
void test_comparison()
{
boost::thread self;
boost::thread thrd(thread_adapter(&comparison_thread, self));
thrd.join();
}
boost::unit_test_framework::test_suite* thread_tests()
{
boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads: thread test suite");
test->add(BOOST_TEST_CASE(&test_sleep));
test->add(BOOST_TEST_CASE(&test_creation));
test->add(BOOST_TEST_CASE(&test_comparison));
return test;
test_mutex();
// test_try_mutex();
// test_timed_mutex();
// test_recursive_mutex();
// test_recursive_try_mutex();
// test_recursive_timed_mutex();
// test_condition();
// test_tss();
// test_once();
return 0;
}

View File

@@ -1,59 +0,0 @@
#include <boost/thread/tss.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <boost/test/unit_test.hpp>
namespace
{
boost::mutex tss_mutex;
int tss_instances = 0;
struct tss_value_t
{
tss_value_t()
{
boost::mutex::scoped_lock lock(tss_mutex);
++tss_instances;
value = 0;
}
~tss_value_t()
{
boost::mutex::scoped_lock lock(tss_mutex);
--tss_instances;
}
int value;
};
boost::thread_specific_ptr<tss_value_t> tss_value;
void test_tss_thread()
{
tss_value.reset(new tss_value_t());
for (int i=0; i<1000; ++i)
{
int& n = tss_value->value;
BOOST_CHECK_EQUAL(n, i);
++n;
}
}
}
void test_tss()
{
const int NUMTHREADS=5;
boost::thread_group threads;
for (int i=0; i<NUMTHREADS; ++i)
threads.create_thread(&test_tss_thread);
threads.join_all();
BOOST_CHECK_EQUAL(tss_instances, 0);
}
boost::unit_test_framework::test_suite* tss_tests()
{
boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads: tss test suite");
test->add(BOOST_TEST_CASE(&test_tss));
return test;
}