fix doc example typo
[boost.git] / boost / asio / detail / posix_mutex.hpp
blob219d6d0be7204ad7938ab9206ec8ba865566db05
1 //
2 // posix_mutex.hpp
3 // ~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
11 #ifndef BOOST_ASIO_DETAIL_POSIX_MUTEX_HPP
12 #define BOOST_ASIO_DETAIL_POSIX_MUTEX_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
21 #include <boost/config.hpp>
22 #include <boost/system/system_error.hpp>
23 #include <boost/asio/detail/pop_options.hpp>
25 #if defined(BOOST_HAS_PTHREADS)
27 #include <boost/asio/detail/push_options.hpp>
28 #include <boost/throw_exception.hpp>
29 #include <pthread.h>
30 #include <boost/asio/detail/pop_options.hpp>
32 #include <boost/asio/error.hpp>
33 #include <boost/asio/detail/noncopyable.hpp>
34 #include <boost/asio/detail/scoped_lock.hpp>
36 namespace boost {
37 namespace asio {
38 namespace detail {
40 class posix_event;
42 class posix_mutex
43 : private noncopyable
45 public:
46 typedef boost::asio::detail::scoped_lock<posix_mutex> scoped_lock;
48 // Constructor.
49 posix_mutex()
51 int error = ::pthread_mutex_init(&mutex_, 0);
52 if (error != 0)
54 boost::system::system_error e(
55 boost::system::error_code(error,
56 boost::asio::error::get_system_category()),
57 "mutex");
58 boost::throw_exception(e);
62 // Destructor.
63 ~posix_mutex()
65 ::pthread_mutex_destroy(&mutex_);
68 // Lock the mutex.
69 void lock()
71 int error = ::pthread_mutex_lock(&mutex_);
72 if (error != 0)
74 boost::system::system_error e(
75 boost::system::error_code(error,
76 boost::asio::error::get_system_category()),
77 "mutex");
78 boost::throw_exception(e);
82 // Unlock the mutex.
83 void unlock()
85 int error = ::pthread_mutex_unlock(&mutex_);
86 if (error != 0)
88 boost::system::system_error e(
89 boost::system::error_code(error,
90 boost::asio::error::get_system_category()),
91 "mutex");
92 boost::throw_exception(e);
96 private:
97 friend class posix_event;
98 ::pthread_mutex_t mutex_;
101 } // namespace detail
102 } // namespace asio
103 } // namespace boost
105 #endif // defined(BOOST_HAS_PTHREADS)
107 #include <boost/asio/detail/pop_options.hpp>
109 #endif // BOOST_ASIO_DETAIL_POSIX_MUTEX_HPP