Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/interprocess/sync/posix/condition.hpp @ 16:2665513ce2d3
Add boost headers
author | Chris Cannam |
---|---|
date | Tue, 05 Aug 2014 11:11:38 +0100 |
parents | |
children | c530137014c0 |
comparison
equal
deleted
inserted
replaced
15:663ca0da4350 | 16:2665513ce2d3 |
---|---|
1 ////////////////////////////////////////////////////////////////////////////// | |
2 // | |
3 // (C) Copyright Ion Gaztanaga 2005-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 | |
11 #ifndef BOOST_INTERPROCESS_POSIX_CONDITION_HPP | |
12 #define BOOST_INTERPROCESS_POSIX_CONDITION_HPP | |
13 | |
14 #if (defined _MSC_VER) && (_MSC_VER >= 1200) | |
15 # pragma once | |
16 #endif | |
17 | |
18 #include <boost/interprocess/detail/config_begin.hpp> | |
19 #include <boost/interprocess/detail/workaround.hpp> | |
20 | |
21 #include <pthread.h> | |
22 #include <errno.h> | |
23 #include <boost/interprocess/sync/posix/pthread_helpers.hpp> | |
24 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp> | |
25 #include <boost/interprocess/detail/posix_time_types_wrk.hpp> | |
26 #include <boost/interprocess/sync/posix/mutex.hpp> | |
27 #include <boost/assert.hpp> | |
28 | |
29 namespace boost { | |
30 namespace interprocess { | |
31 namespace ipcdetail { | |
32 | |
33 class posix_condition | |
34 { | |
35 //Non-copyable | |
36 posix_condition(const posix_condition &); | |
37 posix_condition &operator=(const posix_condition &); | |
38 | |
39 public: | |
40 //!Constructs a posix_condition. On error throws interprocess_exception. | |
41 posix_condition(); | |
42 | |
43 //!Destroys *this | |
44 //!liberating system resources. | |
45 ~posix_condition(); | |
46 | |
47 //!If there is a thread waiting on *this, change that | |
48 //!thread's state to ready. Otherwise there is no effect. | |
49 void notify_one(); | |
50 | |
51 //!Change the state of all threads waiting on *this to ready. | |
52 //!If there are no waiting threads, notify_all() has no effect. | |
53 void notify_all(); | |
54 | |
55 //!Releases the lock on the posix_mutex object associated with lock, blocks | |
56 //!the current thread of execution until readied by a call to | |
57 //!this->notify_one() or this->notify_all(), and then reacquires the lock. | |
58 template <typename L> | |
59 void wait(L& lock) | |
60 { | |
61 if (!lock) | |
62 throw lock_exception(); | |
63 this->do_wait(*lock.mutex()); | |
64 } | |
65 | |
66 //!The same as: | |
67 //!while (!pred()) wait(lock) | |
68 template <typename L, typename Pr> | |
69 void wait(L& lock, Pr pred) | |
70 { | |
71 if (!lock) | |
72 throw lock_exception(); | |
73 | |
74 while (!pred()) | |
75 this->do_wait(*lock.mutex()); | |
76 } | |
77 | |
78 //!Releases the lock on the posix_mutex object associated with lock, blocks | |
79 //!the current thread of execution until readied by a call to | |
80 //!this->notify_one() or this->notify_all(), or until time abs_time is reached, | |
81 //!and then reacquires the lock. | |
82 //!Returns: false if time abs_time is reached, otherwise true. | |
83 template <typename L> | |
84 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time) | |
85 { | |
86 if(abs_time == boost::posix_time::pos_infin){ | |
87 this->wait(lock); | |
88 return true; | |
89 } | |
90 if (!lock) | |
91 throw lock_exception(); | |
92 return this->do_timed_wait(abs_time, *lock.mutex()); | |
93 } | |
94 | |
95 //!The same as: while (!pred()) { | |
96 //! if (!timed_wait(lock, abs_time)) return pred(); | |
97 //! } return true; | |
98 template <typename L, typename Pr> | |
99 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred) | |
100 { | |
101 if(abs_time == boost::posix_time::pos_infin){ | |
102 this->wait(lock, pred); | |
103 return true; | |
104 } | |
105 if (!lock) | |
106 throw lock_exception(); | |
107 while (!pred()){ | |
108 if (!this->do_timed_wait(abs_time, *lock.mutex())) | |
109 return pred(); | |
110 } | |
111 | |
112 return true; | |
113 } | |
114 | |
115 | |
116 void do_wait(posix_mutex &mut); | |
117 | |
118 bool do_timed_wait(const boost::posix_time::ptime &abs_time, posix_mutex &mut); | |
119 | |
120 private: | |
121 pthread_cond_t m_condition; | |
122 }; | |
123 | |
124 inline posix_condition::posix_condition() | |
125 { | |
126 int res; | |
127 pthread_condattr_t cond_attr; | |
128 res = pthread_condattr_init(&cond_attr); | |
129 if(res != 0){ | |
130 throw interprocess_exception("pthread_condattr_init failed"); | |
131 } | |
132 res = pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED); | |
133 if(res != 0){ | |
134 pthread_condattr_destroy(&cond_attr); | |
135 throw interprocess_exception(res); | |
136 } | |
137 res = pthread_cond_init(&m_condition, &cond_attr); | |
138 pthread_condattr_destroy(&cond_attr); | |
139 if(res != 0){ | |
140 throw interprocess_exception(res); | |
141 } | |
142 } | |
143 | |
144 inline posix_condition::~posix_condition() | |
145 { | |
146 int res = 0; | |
147 res = pthread_cond_destroy(&m_condition); | |
148 BOOST_ASSERT(res == 0); (void)res; | |
149 } | |
150 | |
151 inline void posix_condition::notify_one() | |
152 { | |
153 int res = 0; | |
154 res = pthread_cond_signal(&m_condition); | |
155 BOOST_ASSERT(res == 0); (void)res; | |
156 } | |
157 | |
158 inline void posix_condition::notify_all() | |
159 { | |
160 int res = 0; | |
161 res = pthread_cond_broadcast(&m_condition); | |
162 BOOST_ASSERT(res == 0); (void)res; | |
163 } | |
164 | |
165 inline void posix_condition::do_wait(posix_mutex &mut) | |
166 { | |
167 pthread_mutex_t* pmutex = &mut.m_mut; | |
168 int res = 0; | |
169 res = pthread_cond_wait(&m_condition, pmutex); | |
170 BOOST_ASSERT(res == 0); (void)res; | |
171 } | |
172 | |
173 inline bool posix_condition::do_timed_wait | |
174 (const boost::posix_time::ptime &abs_time, posix_mutex &mut) | |
175 { | |
176 timespec ts = ptime_to_timespec(abs_time); | |
177 pthread_mutex_t* pmutex = &mut.m_mut; | |
178 int res = 0; | |
179 res = pthread_cond_timedwait(&m_condition, pmutex, &ts); | |
180 BOOST_ASSERT(res == 0 || res == ETIMEDOUT); | |
181 | |
182 return res != ETIMEDOUT; | |
183 } | |
184 | |
185 } //namespace ipcdetail | |
186 } //namespace interprocess | |
187 } //namespace boost | |
188 | |
189 #include <boost/interprocess/detail/config_end.hpp> | |
190 | |
191 #endif //#ifndef BOOST_INTERPROCESS_POSIX_CONDITION_HPP |