1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
// Copyright (C) 2001-2003
// William E. Kempf
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation. William E. Kempf makes no representations
// about the suitability of this software for any purpose.
// It is provided "as is" without express or implied warranty.
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2007. 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)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_INTERPROCESS_CONDITION_TEST_TEMPLATE_HPP
#define BOOST_INTERPROCESS_CONDITION_TEST_TEMPLATE_HPP
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/thread/detail/config.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
namespace boost{
namespace interprocess{
namespace test {
boost::posix_time::ptime ptime_delay(int secs)
{
return microsec_clock::universal_time() +
boost::posix_time::time_duration(0, 0, secs);
}
inline boost::xtime delay(int secs, int msecs=0, int nsecs=0)
{
const int MILLISECONDS_PER_SECOND = 1000;
const int NANOSECONDS_PER_SECOND = 1000000000;
const int NANOSECONDS_PER_MILLISECOND = 1000000;
boost::xtime xt;
int ret = boost::xtime_get(&xt, boost::TIME_UTC);
assert(ret == static_cast<int>(boost::TIME_UTC));(void)ret;
nsecs += xt.nsec;
msecs += nsecs / NANOSECONDS_PER_MILLISECOND;
secs += msecs / MILLISECONDS_PER_SECOND;
nsecs += (msecs % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND;
xt.nsec = nsecs % NANOSECONDS_PER_SECOND;
xt.sec += secs + (nsecs / NANOSECONDS_PER_SECOND);
return xt;
}
template <typename F, typename T>
class binder
{
public:
binder(const F& func, const T& param)
: func(func), param(param) { }
void operator()() const { func(param); }
private:
F func;
T param;
};
template <typename F, typename T>
binder<F, T> bind_function(F func, T param)
{
return binder<F, T>(func, param);
}
template <class Condition, class Mutex>
struct condition_test_data
{
condition_test_data() : notified(0), awoken(0) { }
~condition_test_data()
{}
Mutex mutex;
Condition condition;
int notified;
int awoken;
};
template <class Condition, class Mutex>
void condition_test_thread(condition_test_data<Condition, Mutex>* data)
{
boost::interprocess::scoped_lock<Mutex>
lock(data->mutex);
assert(lock ? true : false);
while (!(data->notified > 0))
data->condition.wait(lock);
assert(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;
};
template <class Condition, class Mutex>
void condition_test_waits(condition_test_data<Condition, Mutex>* data)
{
boost::interprocess::scoped_lock<Mutex>
lock(data->mutex);
assert(lock ? true : false);
// Test wait.
while (data->notified != 1)
data->condition.wait(lock);
assert(lock ? true : false);
assert(data->notified == 1);
data->awoken++;
data->condition.notify_one();
// Test predicate wait.
data->condition.wait(lock, cond_predicate(data->notified, 2));
assert(lock ? true : false);
assert(data->notified == 2);
data->awoken++;
data->condition.notify_one();
// Test timed_wait.
while (data->notified != 3)
data->condition.timed_wait(lock, ptime_delay(5));
assert(lock ? true : false);
assert(data->notified == 3);
data->awoken++;
data->condition.notify_one();
// Test predicate timed_wait.
cond_predicate pred(data->notified, 4);
bool ret = data->condition.timed_wait(lock, ptime_delay(5), pred);
assert(ret);(void)ret;
assert(lock ? true : false);
assert(pred());
assert(data->notified == 4);
data->awoken++;
data->condition.notify_one();
}
template <class Condition, class Mutex>
void do_test_condition_notify_one()
{
condition_test_data<Condition, Mutex> data;
boost::thread thread(bind_function(&condition_test_thread<Condition, Mutex>, &data));
{
boost::interprocess::scoped_lock<Mutex>
lock(data.mutex);
assert(lock ? true : false);
data.notified++;
data.condition.notify_one();
}
thread.join();
assert(data.awoken == 1);
}
template <class Condition, class Mutex>
void do_test_condition_notify_all()
{
const int NUMTHREADS = 3;
boost::thread_group threads;
condition_test_data<Condition, Mutex> data;
for (int i = 0; i < NUMTHREADS; ++i)
threads.create_thread(bind_function(&condition_test_thread<Condition, Mutex>, &data));
{
boost::interprocess::scoped_lock<Mutex>
lock(data.mutex);
assert(lock ? true : false);
data.notified++;
data.condition.notify_all();
}
threads.join_all();
assert(data.awoken == NUMTHREADS);
}
template <class Condition, class Mutex>
void do_test_condition_waits()
{
condition_test_data<Condition, Mutex> data;
boost::thread thread(bind_function(&condition_test_waits<Condition, Mutex>, &data));
{
boost::interprocess::scoped_lock<Mutex>
lock(data.mutex);
assert(lock ? true : false);
boost::thread::sleep(delay(1));
data.notified++;
data.condition.notify_one();
while (data.awoken != 1)
data.condition.wait(lock);
assert(lock ? true : false);
assert(data.awoken == 1);
boost::thread::sleep(delay(1));
data.notified++;
data.condition.notify_one();
while (data.awoken != 2)
data.condition.wait(lock);
assert(lock ? true : false);
assert(data.awoken == 2);
boost::thread::sleep(delay(1));
data.notified++;
data.condition.notify_one();
while (data.awoken != 3)
data.condition.wait(lock);
assert(lock ? true : false);
assert(data.awoken == 3);
boost::thread::sleep(delay(1));
data.notified++;
data.condition.notify_one();
while (data.awoken != 4)
data.condition.wait(lock);
assert(lock ? true : false);
assert(data.awoken == 4);
}
thread.join();
assert(data.awoken == 4);
}
/*
//Message queue simulation test
template <class Condition>
inline Condition &cond_empty()
{
static Condition cond_empty;
return cond_empty;
}
template <class Condition>
inline Condition &cond_full()
{
static Condition cond_full;
return cond_full;
}
template <class Mutex>
inline Mutex &mutex()
{
static Mutex mut;
return mut;
}
*/
static volatile int count = 0;
static volatile int waiting_readers = 0;
static volatile int waiting_writer = 0;
const int queue_size = 3;
const int thread_factor = 10;
const int NumThreads = thread_factor*queue_size;
//Function that removes items from queue
template <class Condition, class Mutex>
struct condition_func
{
condition_func(Condition &cond_full, Condition &cond_empty, Mutex &mutex)
: cond_full_(cond_full), cond_empty_(cond_empty), mutex_(mutex)
{}
void operator()()
{
boost::interprocess::scoped_lock<Mutex>lock(mutex_);
while(count == 0){
++waiting_readers;
cond_empty_.wait(lock);
--waiting_readers;
}
--count;
if(waiting_writer)
cond_full_.notify_one();
}
Condition &cond_full_;
Condition &cond_empty_;
Mutex &mutex_;
};
//Queue functions
template <class Condition, class Mutex>
void do_test_condition_queue_notify_one(void)
{
//Force mutex and condition creation
Condition cond_empty;
Condition cond_full;
Mutex mutex;
//Create threads that will decrease count
{
//Initialize counters
count = 0;
waiting_readers = 0;
waiting_writer = 0;
boost::thread_group thgroup;
int i;
for(i = 0; i< NumThreads; ++i){
condition_func<Condition, Mutex> func(cond_full, cond_empty, mutex);
thgroup.create_thread(func);
}
//Add 20 elements one by one in the queue simulation
//The sender will block if it fills the queue
for(i = 0; i < NumThreads; ++i){
boost::interprocess::scoped_lock<Mutex> lock(mutex);
while(count == queue_size){
++waiting_writer;
cond_full.wait(lock);
--waiting_writer;
}
count++;
if(waiting_readers)
cond_empty.notify_one();
}
thgroup.join_all();
assert(count == 0);
assert(waiting_readers == 0);
assert(waiting_writer == 0);
}
}
//Queue functions
template <class Condition, class Mutex>
void do_test_condition_queue_notify_all(void)
{
//Force mutex and condition creation
Condition cond_empty;
Condition cond_full;
Mutex mutex;
//Create threads that will decrease count
{
//Initialize counters
count = 0;
waiting_readers = 0;
waiting_writer = 0;
boost::thread_group thgroup;
int i;
for(i = 0; i< NumThreads; ++i){
condition_func<Condition, Mutex> func(cond_full, cond_empty, mutex);
thgroup.create_thread(func);
}
//Fill queue to the max size and notify all several times
for(i = 0; i < NumThreads; ++i){
boost::interprocess::scoped_lock<Mutex>lock(mutex);
while(count == queue_size){
++waiting_writer;
cond_full.wait(lock);
--waiting_writer;
}
count++;
if(waiting_readers)
cond_empty.notify_all();
}
thgroup.join_all();
assert(count == 0);
assert(waiting_readers == 0);
assert(waiting_writer == 0);
}
}
template <class Condition, class Mutex>
bool do_test_condition()
{
std::cout << "do_test_condition_notify_one<" << typeid(Condition).name() << "," << typeid(Mutex).name() << std::endl;
do_test_condition_notify_one<Condition, Mutex>();
std::cout << "do_test_condition_notify_all<" << typeid(Condition).name() << "," << typeid(Mutex).name() << std::endl;
do_test_condition_notify_all<Condition, Mutex>();
std::cout << "do_test_condition_waits<" << typeid(Condition).name() << "," << typeid(Mutex).name() << std::endl;
do_test_condition_waits<Condition, Mutex>();
//std::cout << "do_test_condition_queue_notify_one<" << typeid(Condition).name() << "," << typeid(Mutex).name() << std::endl;
//do_test_condition_queue_notify_one<Condition, Mutex>();
//std::cout << "do_test_condition_queue_notify_all<" << typeid(Condition).name() << "," << typeid(Mutex).name() << std::endl;
//do_test_condition_queue_notify_all<Condition, Mutex>();
return true;
}
} //namespace test
} //namespace interprocess{
} //namespace boost{
#include <boost/interprocess/detail/config_end.hpp>
#endif //#ifndef BOOST_INTERPROCESS_CONDITION_TEST_TEMPLATE_HPP
|