util.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #ifndef BOOST_INTERPROCESS_TEST_UTIL_HEADER
  21. #define BOOST_INTERPROCESS_TEST_UTIL_HEADER
  22. #include <boost/interprocess/detail/config_begin.hpp>
  23. #include <boost/interprocess/sync/scoped_lock.hpp>
  24. #include <boost/interprocess/detail/os_thread_functions.hpp>
  25. #include <boost/date_time/posix_time/posix_time_types.hpp>
  26. #include <boost/version.hpp>
  27. namespace boost {
  28. namespace interprocess {
  29. namespace test {
  30. inline void sleep(const boost::posix_time::ptime &xt)
  31. {
  32. boost::interprocess::ipcdetail::thread_sleep
  33. ((xt - microsec_clock::universal_time()).total_milliseconds());
  34. }
  35. inline boost::posix_time::ptime delay(int secs, int msecs=0, int nsecs = 0)
  36. {
  37. (void)msecs;
  38. using namespace boost::posix_time;
  39. int count = static_cast<int>(double(nsecs)*
  40. (double(time_duration::ticks_per_second())/double(1000000000.0)));
  41. count += static_cast<int>(double(msecs)*
  42. (double(time_duration::ticks_per_second())/double(1000.0)));
  43. boost::posix_time::ptime cur = microsec_clock::universal_time();
  44. return cur += boost::posix_time::time_duration(0, 0, secs, count);
  45. }
  46. inline bool in_range(const boost::posix_time::ptime& xt, int secs=1)
  47. {
  48. boost::posix_time::ptime min = delay(-secs);
  49. boost::posix_time::ptime max = delay(0);
  50. return (xt > min) && (max > xt);
  51. }
  52. template <typename P>
  53. class thread_adapter
  54. {
  55. public:
  56. thread_adapter(void (*func)(void*, P &), void* param1, P &param2)
  57. : func_(func), param1_(param1) ,param2_(param2){ }
  58. void operator()() const { func_(param1_, param2_); }
  59. private:
  60. void (*func_)(void*, P &);
  61. void* param1_;
  62. P& param2_;
  63. };
  64. template <typename P>
  65. struct data
  66. {
  67. explicit data(int id, int secs=0)
  68. : m_id(id), m_value(-1), m_secs(secs), m_error(no_error)
  69. {}
  70. int m_id;
  71. int m_value;
  72. int m_secs;
  73. error_code_t m_error;
  74. };
  75. static int shared_val = 0;
  76. static const int BaseSeconds = 1;
  77. } //namespace test {
  78. } //namespace interprocess {
  79. } //namespace boost {
  80. #include <boost/interprocess/detail/config_end.hpp>
  81. #endif //#ifndef BOOST_INTERPROCESS_TEST_UTIL_HEADER