First public contribution.
1 // Copyright (C) 2001-2003
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_CONDITION_WEK070601_HPP
8 #define BOOST_CONDITION_WEK070601_HPP
10 #include <boost/thread/detail/config.hpp>
12 #include <boost/thread/exceptions.hpp>
13 #include <boost/utility.hpp>
14 #include <boost/thread/detail/lock.hpp>
16 #if defined(BOOST_HAS_PTHREADS)
18 #elif defined(BOOST_HAS_MPTASKS)
19 # include "scoped_critical_region.hpp"
25 // disable warnings about non dll import
26 // see: http://www.boost.org/more/separate_compilation.html#dlls
28 # pragma warning(push)
29 # pragma warning(disable: 4251 4231 4660 4275)
34 class BOOST_THREAD_DECL condition_impl : private noncopyable
36 friend class condition;
45 #if (defined(BOOST_HAS_WINTHREADS) || defined(BOOST_HAS_MPTASKS))
48 bool do_timed_wait(const xtime& xt);
49 #elif defined(BOOST_HAS_PTHREADS)
50 void do_wait(pthread_mutex_t* pmutex);
51 bool do_timed_wait(const xtime& xt, pthread_mutex_t* pmutex);
54 #if defined(BOOST_HAS_WINTHREADS)
58 unsigned m_gone; // # threads that timed out and never made it to m_queue
59 unsigned long m_blocked; // # threads blocked on the condition
60 unsigned m_waiting; // # threads no longer waiting for the condition but
61 // still waiting to be removed from m_queue
62 #elif defined(BOOST_HAS_PTHREADS)
63 pthread_cond_t m_condition;
64 #elif defined(BOOST_HAS_MPTASKS)
66 MPSemaphoreID m_queue;
67 threads::mac::detail::scoped_critical_region m_mutex;
68 threads::mac::detail::scoped_critical_region m_mutex_mutex;
69 unsigned m_gone; // # threads that timed out and never made it to m_queue
70 unsigned long m_blocked; // # threads blocked on the condition
71 unsigned m_waiting; // # threads no longer waiting for the condition but
72 // still waiting to be removed from m_queue
78 class condition : private noncopyable
84 void notify_one() { m_impl.notify_one(); }
85 void notify_all() { m_impl.notify_all(); }
93 do_wait(lock.m_mutex);
96 template <typename L, typename Pr>
97 void wait(L& lock, Pr pred)
103 do_wait(lock.m_mutex);
106 template <typename L>
107 bool timed_wait(L& lock, const xtime& xt)
112 return do_timed_wait(lock.m_mutex, xt);
115 template <typename L, typename Pr>
116 bool timed_wait(L& lock, const xtime& xt, Pr pred)
123 if (!do_timed_wait(lock.m_mutex, xt))
131 detail::condition_impl m_impl;
133 template <typename M>
134 void do_wait(M& mutex)
136 #if (defined(BOOST_HAS_WINTHREADS) || defined(BOOST_HAS_MPTASKS))
140 typedef detail::thread::lock_ops<M>
141 #if defined(__HP_aCC) && __HP_aCC <= 33900 && !defined(BOOST_STRICT_CONFIG)
142 # define lock_ops lock_ops_ // HP confuses lock_ops witht the template
146 typename lock_ops::lock_state state;
147 lock_ops::unlock(mutex, state);
149 #if defined(BOOST_HAS_PTHREADS)
150 m_impl.do_wait(state.pmutex);
151 #elif (defined(BOOST_HAS_WINTHREADS) || defined(BOOST_HAS_MPTASKS))
155 lock_ops::lock(mutex, state);
159 template <typename M>
160 bool do_timed_wait(M& mutex, const xtime& xt)
162 #if (defined(BOOST_HAS_WINTHREADS) || defined(BOOST_HAS_MPTASKS))
166 typedef detail::thread::lock_ops<M>
167 #if defined(__HP_aCC) && __HP_aCC <= 33900 && !defined(BOOST_STRICT_CONFIG)
168 # define lock_ops lock_ops_ // HP confuses lock_ops witht the template
172 typename lock_ops::lock_state state;
173 lock_ops::unlock(mutex, state);
177 #if defined(BOOST_HAS_PTHREADS)
178 ret = m_impl.do_timed_wait(xt, state.pmutex);
179 #elif (defined(BOOST_HAS_WINTHREADS) || defined(BOOST_HAS_MPTASKS))
180 ret = m_impl.do_timed_wait(xt);
183 lock_ops::lock(mutex, state);
190 # pragma warning(pop)
195 // 8 Feb 01 WEKEMPF Initial version.
196 // 22 May 01 WEKEMPF Modified to use xtime for time outs.
197 // 23 May 01 WEKEMPF Removed "duration" timed_waits, as they are too
198 // difficult to use with spurious wakeups.
199 // 3 Jan 03 WEKEMPF Modified for DLL implementation.
201 #endif // BOOST_CONDITION_WEK070601_HPP