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)
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>
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
25 #include <boost/config/abi_prefix.hpp>
37 int const res
=pthread_mutex_init(&m
,NULL
);
40 throw thread_resource_error();
45 BOOST_VERIFY(!pthread_mutex_destroy(&m
));
50 BOOST_VERIFY(!pthread_mutex_lock(&m
));
55 BOOST_VERIFY(!pthread_mutex_unlock(&m
));
60 int const res
=pthread_mutex_trylock(&m
);
61 BOOST_ASSERT(!res
|| res
==EBUSY
);
65 typedef pthread_mutex_t
* native_handle_type
;
66 native_handle_type
native_handle()
71 typedef unique_lock
<mutex
> scoped_lock
;
72 typedef detail::try_lock_wrapper
<mutex
> scoped_try_lock
;
75 typedef mutex try_mutex
;
82 #ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
89 int const res
=pthread_mutex_init(&m
,NULL
);
92 throw thread_resource_error();
94 #ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
95 int const res2
=pthread_cond_init(&cond
,NULL
);
98 BOOST_VERIFY(!pthread_mutex_destroy(&m
));
99 throw thread_resource_error();
106 BOOST_VERIFY(!pthread_mutex_destroy(&m
));
107 #ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
108 BOOST_VERIFY(!pthread_cond_destroy(&cond
));
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
125 BOOST_VERIFY(!pthread_mutex_lock(&m
));
130 BOOST_VERIFY(!pthread_mutex_unlock(&m
));
135 int const res
=pthread_mutex_trylock(&m
);
136 BOOST_ASSERT(!res
|| res
==EBUSY
);
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
);
147 typedef pthread_mutex_t
* native_handle_type
;
148 native_handle_type
native_handle()
156 boost::pthread::pthread_mutex_scoped_lock
const local_lock(&m
);
159 BOOST_VERIFY(!pthread_cond_wait(&cond
,&m
));
166 boost::pthread::pthread_mutex_scoped_lock
const local_lock(&m
);
168 BOOST_VERIFY(!pthread_cond_signal(&cond
));
173 boost::pthread::pthread_mutex_scoped_lock
const local_lock(&m
);
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
);
188 int const cond_res
=pthread_cond_timedwait(&cond
,&m
,&timeout
);
189 if(cond_res
==ETIMEDOUT
)
193 BOOST_ASSERT(!cond_res
);
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>