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 |
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 | #include <boost/interprocess/timed_utils.hpp> |
28 | |
29 | #if defined(BOOST_CLANG) || (defined(BOOST_GCC) && (BOOST_GCC >= 40600)) |
30 | #pragma GCC diagnostic push |
31 | #pragma GCC diagnostic ignored "-Wsign-conversion" |
32 | #pragma GCC diagnostic ignored "-Wconversion" |
33 | #pragma GCC diagnostic ignored "-Wcast-align" |
34 | #pragma GCC diagnostic ignored "-Wshadow" |
35 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
36 | # if (BOOST_GCC >= 100000) |
37 | #pragma GCC diagnostic ignored "-Warith-conversion" |
38 | # endif |
39 | #endif |
40 | |
41 | #if BOOST_CXX_VERSION >= 201103L |
42 | #define |
43 | #include <boost/chrono/system_clocks.hpp> |
44 | #include <boost/date_time/posix_time/posix_time_types.hpp> |
45 | #endif |
46 | |
47 | #include <boost/version.hpp> |
48 | |
49 | #if !defined(BOOST_NO_CXX11_HDR_CHRONO) |
50 | #include <chrono> |
51 | #endif |
52 | |
53 | #if defined(BOOST_GCC) && (BOOST_GCC >= 40600) |
54 | #pragma GCC diagnostic pop |
55 | #endif |
56 | |
57 | namespace boost { |
58 | namespace interprocess { |
59 | namespace test { |
60 | |
61 | // ptime_delay_ms + ptime_ms |
62 | |
63 | #if BOOST_CXX_VERSION >= 201103L |
64 | inline boost::posix_time::ptime ptime_delay_ms(unsigned msecs) |
65 | { |
66 | using namespace boost::posix_time; |
67 | int count = static_cast<int>(double(msecs) * |
68 | (double(time_duration::ticks_per_second()) / double(1000.0))); |
69 | return microsec_clock::universal_time() + time_duration(0, 0, 0, count); |
70 | } |
71 | |
72 | inline boost::posix_time::time_duration ptime_ms(unsigned msecs) |
73 | { |
74 | using namespace boost::posix_time; |
75 | int count = static_cast<int>(double(msecs) * |
76 | (double(time_duration::ticks_per_second()) / double(1000.0))); |
77 | return time_duration(0, 0, 0, count); |
78 | } |
79 | #else |
80 | inline ustime ptime_delay_ms(unsigned msecs) |
81 | { return ustime_delay_milliseconds(msecs); } |
82 | |
83 | inline usduration ptime_ms(unsigned msecs) |
84 | { return usduration_from_milliseconds(msecs); } |
85 | #endif |
86 | |
87 | // boost_systemclock_delay_ms + boost_systemclock_ms |
88 | |
89 | #if BOOST_CXX_VERSION >= 201103L |
90 | inline boost::chrono::system_clock::time_point boost_systemclock_delay_ms(unsigned msecs) |
91 | { return boost::chrono::system_clock::now() + boost::chrono::milliseconds(msecs); } |
92 | |
93 | inline boost::chrono::milliseconds boost_systemclock_ms(unsigned msecs) |
94 | { return boost::chrono::milliseconds(msecs); } |
95 | #else |
96 | inline ustime boost_systemclock_delay_ms(unsigned msecs) |
97 | { return ustime_delay_milliseconds(msecs); } |
98 | |
99 | inline usduration boost_systemclock_ms(unsigned msecs) |
100 | { return usduration_from_milliseconds(msecs); } |
101 | #endif |
102 | |
103 | // std_systemclock_delay_ms + std_systemclock_ms |
104 | |
105 | #if !defined(BOOST_NO_CXX11_HDR_CHRONO) |
106 | //Use std chrono if available |
107 | inline std::chrono::system_clock::time_point std_systemclock_delay_ms(unsigned msecs) |
108 | { return std::chrono::system_clock::now() + std::chrono::milliseconds(msecs); } |
109 | |
110 | inline std::chrono::milliseconds std_systemclock_ms(unsigned msecs) |
111 | { return std::chrono::milliseconds(msecs); } |
112 | |
113 | #elif BOOST_CXX_VERSION >= 201103L |
114 | //Otherwise use boost chrono |
115 | inline boost::chrono::system_clock::time_point std_systemclock_delay_ms(unsigned msecs) |
116 | { return boost_systemclock_delay_ms(msecs); } |
117 | |
118 | inline boost::chrono::milliseconds std_systemclock_ms(unsigned msecs) |
119 | { return boost_systemclock_ms(msecs); } |
120 | #else |
121 | inline ustime std_systemclock_delay_ms(unsigned msecs) |
122 | { return ustime_delay_milliseconds(msecs); } |
123 | |
124 | inline usduration std_systemclock_ms(unsigned msecs) |
125 | { return usduration_from_milliseconds(msecs); } |
126 | #endif |
127 | |
128 | // thread_adapter + data |
129 | |
130 | template <typename P> |
131 | class thread_adapter |
132 | { |
133 | public: |
134 | thread_adapter(void (*func)(void*, P &), void* param1, P ¶m2) |
135 | : func_(func), param1_(param1) ,param2_(param2){ } |
136 | void operator()() const { func_(param1_, param2_); } |
137 | |
138 | private: |
139 | void (*func_)(void*, P &); |
140 | void* param1_; |
141 | P& param2_; |
142 | }; |
143 | |
144 | template <typename P> |
145 | struct data |
146 | { |
147 | explicit data(int id, int msecs=0, int flags = 0, bool block = false) |
148 | : m_id(id), m_value(-1), m_msecs(msecs), m_error(no_error), m_flags(flags), m_block(block) |
149 | {} |
150 | |
151 | int m_id; |
152 | int m_value; |
153 | int m_msecs; |
154 | error_code_t m_error; |
155 | int m_flags; |
156 | bool m_block; |
157 | }; |
158 | |
159 | int shared_val = 0; |
160 | static const unsigned BaseMs = 1000; |
161 | |
162 | } //namespace test { |
163 | } //namespace interprocess { |
164 | } //namespace boost { |
165 | |
166 | #include <boost/interprocess/detail/config_end.hpp> |
167 | |
168 | #endif //#ifndef BOOST_INTERPROCESS_TEST_UTIL_HEADER |
169 | |