1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost 4 // Software License, Version 1.0. (See accompanying file 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 // 7 // See http://www.boost.org/libs/interprocess for documentation. 8 // 9 ////////////////////////////////////////////////////////////////////////////// 10 // Copyright (C) 2001-2003 11 // William E. Kempf 12 // 13 // Permission to use, copy, modify, distribute and sell this software 14 // and its documentation for any purpose is hereby granted without fee, 15 // provided that the above copyright notice appear in all copies and 16 // that both that copyright notice and this permission notice appear 17 // in supporting documentation. William E. Kempf makes no representations 18 // about the suitability of this software for any purpose. 19 // It is provided "as is" without express or implied warranty. 20 21 #ifndef BOOST_INTERPROCESS_TEST_UTIL_HEADER 22 #define BOOST_INTERPROCESS_TEST_UTIL_HEADER 23 24 #include <boost/interprocess/detail/config_begin.hpp> 25 #include <boost/interprocess/sync/scoped_lock.hpp> 26 #include <boost/interprocess/detail/os_thread_functions.hpp> 27 28 #include <boost/date_time/posix_time/posix_time_types.hpp> 29 #include <boost/version.hpp> 30 31 namespace boost { 32 namespace interprocess { 33 namespace test { 34 sleep(const boost::posix_time::ptime & xt)35 inline void sleep(const boost::posix_time::ptime &xt) 36 { 37 boost::interprocess::ipcdetail::thread_sleep 38 ((xt - microsec_clock::universal_time()).total_milliseconds()); 39 } 40 delay(int secs,int msecs=0,int nsecs=0)41 inline boost::posix_time::ptime delay(int secs, int msecs=0, int nsecs = 0) 42 { 43 (void)msecs; 44 using namespace boost::posix_time; 45 int count = static_cast<int>(double(nsecs)* 46 (double(time_duration::ticks_per_second())/double(1000000000.0))); 47 count += static_cast<int>(double(msecs)* 48 (double(time_duration::ticks_per_second())/double(1000.0))); 49 boost::posix_time::ptime cur = microsec_clock::universal_time(); 50 return cur += boost::posix_time::time_duration(0, 0, secs, count); 51 } 52 in_range(const boost::posix_time::ptime & xt,int secs=1)53 inline bool in_range(const boost::posix_time::ptime& xt, int secs=1) 54 { 55 boost::posix_time::ptime min = delay(-secs); 56 boost::posix_time::ptime max = delay(0); 57 return (xt > min) && (max > xt); 58 } 59 60 template <typename P> 61 class thread_adapter 62 { 63 public: thread_adapter(void (* func)(void *,P &),void * param1,P & param2)64 thread_adapter(void (*func)(void*, P &), void* param1, P ¶m2) 65 : func_(func), param1_(param1) ,param2_(param2){ } operator ()() const66 void operator()() const { func_(param1_, param2_); } 67 68 private: 69 void (*func_)(void*, P &); 70 void* param1_; 71 P& param2_; 72 }; 73 74 template <typename P> 75 struct data 76 { databoost::interprocess::test::data77 explicit data(int id, int secs=0) 78 : m_id(id), m_value(-1), m_secs(secs), m_error(no_error) 79 {} 80 int m_id; 81 int m_value; 82 int m_secs; 83 error_code_t m_error; 84 }; 85 86 static int shared_val = 0; 87 static const int BaseSeconds = 1; 88 89 } //namespace test { 90 } //namespace interprocess { 91 } //namespace boost { 92 93 #include <boost/interprocess/detail/config_end.hpp> 94 95 #endif //#ifndef BOOST_INTERPROCESS_TEST_UTIL_HEADER 96