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