fix doc example typo
[boost.git] / boost / thread / pthread / mutex.hpp
blob51d62ae546eca607a497391671bdf77eb80497de
1 #ifndef BOOST_THREAD_PTHREAD_MUTEX_HPP
2 #define BOOST_THREAD_PTHREAD_MUTEX_HPP
3 // (C) Copyright 2007-8 Anthony Williams
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 #include <pthread.h>
9 #include <boost/utility.hpp>
10 #include <boost/thread/exceptions.hpp>
11 #include <boost/thread/locks.hpp>
12 #include <boost/thread/thread_time.hpp>
13 #include <boost/thread/xtime.hpp>
14 #include <boost/assert.hpp>
15 #include <errno.h>
16 #include "timespec.hpp"
17 #include "pthread_mutex_scoped_lock.hpp"
19 #ifdef _POSIX_TIMEOUTS
20 #if _POSIX_TIMEOUTS >= 0
21 #define BOOST_PTHREAD_HAS_TIMEDLOCK
22 #endif
23 #endif
25 #include <boost/config/abi_prefix.hpp>
27 namespace boost
29 class mutex:
30 boost::noncopyable
32 private:
33 pthread_mutex_t m;
34 public:
35 mutex()
37 int const res=pthread_mutex_init(&m,NULL);
38 if(res)
40 throw thread_resource_error();
43 ~mutex()
45 BOOST_VERIFY(!pthread_mutex_destroy(&m));
48 void lock()
50 BOOST_VERIFY(!pthread_mutex_lock(&m));
53 void unlock()
55 BOOST_VERIFY(!pthread_mutex_unlock(&m));
58 bool try_lock()
60 int const res=pthread_mutex_trylock(&m);
61 BOOST_ASSERT(!res || res==EBUSY);
62 return !res;
65 typedef pthread_mutex_t* native_handle_type;
66 native_handle_type native_handle()
68 return &m;
71 typedef unique_lock<mutex> scoped_lock;
72 typedef detail::try_lock_wrapper<mutex> scoped_try_lock;
75 typedef mutex try_mutex;
77 class timed_mutex:
78 boost::noncopyable
80 private:
81 pthread_mutex_t m;
82 #ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
83 pthread_cond_t cond;
84 bool is_locked;
85 #endif
86 public:
87 timed_mutex()
89 int const res=pthread_mutex_init(&m,NULL);
90 if(res)
92 throw thread_resource_error();
94 #ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
95 int const res2=pthread_cond_init(&cond,NULL);
96 if(res2)
98 BOOST_VERIFY(!pthread_mutex_destroy(&m));
99 throw thread_resource_error();
101 is_locked=false;
102 #endif
104 ~timed_mutex()
106 BOOST_VERIFY(!pthread_mutex_destroy(&m));
107 #ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
108 BOOST_VERIFY(!pthread_cond_destroy(&cond));
109 #endif
112 template<typename TimeDuration>
113 bool timed_lock(TimeDuration const & relative_time)
115 return timed_lock(get_system_time()+relative_time);
117 bool timed_lock(boost::xtime const & absolute_time)
119 return timed_lock(system_time(absolute_time));
122 #ifdef BOOST_PTHREAD_HAS_TIMEDLOCK
123 void lock()
125 BOOST_VERIFY(!pthread_mutex_lock(&m));
128 void unlock()
130 BOOST_VERIFY(!pthread_mutex_unlock(&m));
133 bool try_lock()
135 int const res=pthread_mutex_trylock(&m);
136 BOOST_ASSERT(!res || res==EBUSY);
137 return !res;
139 bool timed_lock(system_time const & abs_time)
141 struct timespec const timeout=detail::get_timespec(abs_time);
142 int const res=pthread_mutex_timedlock(&m,&timeout);
143 BOOST_ASSERT(!res || res==ETIMEDOUT);
144 return !res;
147 typedef pthread_mutex_t* native_handle_type;
148 native_handle_type native_handle()
150 return &m;
153 #else
154 void lock()
156 boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
157 while(is_locked)
159 BOOST_VERIFY(!pthread_cond_wait(&cond,&m));
161 is_locked=true;
164 void unlock()
166 boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
167 is_locked=false;
168 BOOST_VERIFY(!pthread_cond_signal(&cond));
171 bool try_lock()
173 boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
174 if(is_locked)
176 return false;
178 is_locked=true;
179 return true;
182 bool timed_lock(system_time const & abs_time)
184 struct timespec const timeout=detail::get_timespec(abs_time);
185 boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
186 while(is_locked)
188 int const cond_res=pthread_cond_timedwait(&cond,&m,&timeout);
189 if(cond_res==ETIMEDOUT)
191 return false;
193 BOOST_ASSERT(!cond_res);
195 is_locked=true;
196 return true;
198 #endif
200 typedef unique_lock<timed_mutex> scoped_timed_lock;
201 typedef detail::try_lock_wrapper<timed_mutex> scoped_try_lock;
202 typedef scoped_timed_lock scoped_lock;
207 #include <boost/config/abi_suffix.hpp>
210 #endif