File: util.hpp

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (113 lines) | stat: -rw-r--r-- 3,493 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2004-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.
//
//////////////////////////////////////////////////////////////////////////////
// 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.

#ifndef BOOST_INTERPROCESS_TEST_UTIL_HEADER
#define BOOST_INTERPROCESS_TEST_UTIL_HEADER

#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>

#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <algorithm>
#include <iostream>

#ifndef DEFAULT_EXECUTION_MONITOR_TYPE
#   define DEFAULT_EXECUTION_MONITOR_TYPE execution_monitor::use_condition
#endif

namespace boost {
namespace interprocess {
namespace test {

inline void sleep(const boost::posix_time::ptime &xt)
{
   boost::interprocess::interprocess_mutex mx;
   boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex>
      lock(mx);
   boost::interprocess::interprocess_condition cond;
   cond.timed_wait(lock, xt);
}

inline boost::posix_time::ptime delay(int secs, int msecs=0, int nsecs = 0)
{
   (void)msecs;
   using namespace boost::posix_time;
   int count = static_cast<int>(double(nsecs)*
               (double(time_duration::ticks_per_second())/double(1000000000.0)));
   count += static_cast<int>(double(msecs)*
               (double(time_duration::ticks_per_second())/double(1000.0)));
   boost::posix_time::ptime cur = microsec_clock::universal_time();
   return cur +=  boost::posix_time::time_duration(0, 0, secs, count);
}

inline bool in_range(const boost::posix_time::ptime& xt, int secs=1)
{
    boost::posix_time::ptime min = delay(-secs);
    boost::posix_time::ptime max = delay(0);
    return (xt > min) && (max > xt);
}

boost::xtime xsecs(int secs)
{
   boost::xtime ret;
   boost::xtime_get(&ret, boost::TIME_UTC);
   ret.sec += secs;
   return ret;
}

template <typename P>
class thread_adapter
{
   public:
   thread_adapter(void (*func)(void*, P &), void* param1, P &param2)
      : _func(func), _param1(param1) ,_param2(param2){ }
   void operator()() const { _func(_param1, _param2); }

   private:
   void (*_func)(void*, P &);
   void* _param1;
   P& _param2;
};

template <typename P>
struct data
{
   data(int id, int secs=0)
      : m_id(id), m_value(-1), m_secs(secs)
   {}
   int m_id;
   int m_value;
   int m_secs;
};

static int shared_val = 0;
static const int BaseSeconds = 1;

}  //namespace test {
}  //namespace interprocess {
}  //namespace boost {

#include <boost/interprocess/detail/config_end.hpp>

#endif   //#ifndef BOOST_INTERPROCESS_TEST_UTIL_HEADER