Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / container / test / util.hpp
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/container 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_CONTAINER_TEST_UTIL_HEADER
22 #define BOOST_CONTAINER_TEST_UTIL_HEADER
23
24 #include <boost/container/detail/config_begin.hpp>
25 #include <boost/container/sync/container_mutex.hpp>
26 #include <boost/container/sync/container_condition.hpp>
27 #include <boost/container/sync/scoped_lock.hpp>
28 #include <boost/thread/thread.hpp>
29 #include <boost/thread/xtime.hpp>
30
31 #include <boost/date_time/posix_time/posix_time_types.hpp>
32 #include <algorithm>
33 #include <iostream>
34
35 #ifndef DEFAULT_EXECUTION_MONITOR_TYPE
36 #   define DEFAULT_EXECUTION_MONITOR_TYPE execution_monitor::use_condition
37 #endif
38
39 namespace boost {
40 namespace container {
41 namespace test {
42
43 inline void sleep(const boost::posix_time::ptime &xt)
44 {
45    boost::container::container_mutex mx;
46    boost::container::scoped_lock<boost::container::container_mutex>
47       lock(mx);
48    boost::container::container_condition cond;
49    cond.timed_wait(lock, xt);
50 }
51
52 inline boost::posix_time::ptime delay(int secs, int msecs=0, int nsecs = 0)
53 {
54    (void)msecs;
55    using namespace boost::posix_time;
56    int count = static_cast<int>(double(nsecs)*
57                (double(time_duration::ticks_per_second())/double(1000000000.0)));
58    count += static_cast<int>(double(msecs)*
59                (double(time_duration::ticks_per_second())/double(1000.0)));
60    boost::posix_time::ptime cur = microsec_clock::universal_time();
61    return cur +=  boost::posix_time::time_duration(0, 0, secs, count);
62 }
63
64 inline bool in_range(const boost::posix_time::ptime& xt, int secs=1)
65 {
66     boost::posix_time::ptime min = delay(-secs);
67     boost::posix_time::ptime max = delay(0);
68     return (xt > min) && (max > xt);
69 }
70
71 boost::xtime xsecs(int secs)
72 {
73    boost::xtime ret;
74    boost::xtime_get(&ret, boost::TIME_UTC);
75    ret.sec += secs;
76    return ret;
77 }
78
79 template <typename P>
80 class thread_adapter
81 {
82    public:
83    thread_adapter(void (*func)(void*, P &), void* param1, P &param2)
84       : _func(func), _param1(param1) ,_param2(param2){ }
85    void operator()() const { _func(_param1, _param2); }
86
87    private:
88    void (*_func)(void*, P &);
89    void* _param1;
90    P& _param2;
91 };
92
93 template <typename P>
94 struct data
95 {
96    data(int id, int secs=0)
97       : m_id(id), m_value(-1), m_secs(secs)
98    {}
99    int m_id;
100    int m_value;
101    int m_secs;
102 };
103
104 static int shared_val = 0;
105 static const int BaseSeconds = 1;
106
107 }  //namespace test {
108 }  //namespace container {
109 }  //namespace boost {
110
111 #include <boost/container/detail/config_end.hpp>
112
113 #endif   //#ifndef BOOST_CONTAINER_TEST_UTIL_HEADER