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