| 1 | // |
|---|---|
| 2 | // detail/posix_mutex.hpp |
| 3 | // ~~~~~~~~~~~~~~~~~~~~~~ |
| 4 | // |
| 5 | // Copyright (c) 2003-2024 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 | // |
| 10 | |
| 11 | #ifndef BOOST_ASIO_DETAIL_POSIX_MUTEX_HPP |
| 12 | #define BOOST_ASIO_DETAIL_POSIX_MUTEX_HPP |
| 13 | |
| 14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
| 15 | # pragma once |
| 16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) |
| 17 | |
| 18 | #include <boost/asio/detail/config.hpp> |
| 19 | |
| 20 | #if defined(BOOST_ASIO_HAS_PTHREADS) |
| 21 | |
| 22 | #include <pthread.h> |
| 23 | #include <boost/asio/detail/noncopyable.hpp> |
| 24 | #include <boost/asio/detail/scoped_lock.hpp> |
| 25 | |
| 26 | #include <boost/asio/detail/push_options.hpp> |
| 27 | |
| 28 | namespace boost { |
| 29 | namespace asio { |
| 30 | namespace detail { |
| 31 | |
| 32 | class posix_event; |
| 33 | |
| 34 | class posix_mutex |
| 35 | : private noncopyable |
| 36 | { |
| 37 | public: |
| 38 | typedef boost::asio::detail::scoped_lock<posix_mutex> scoped_lock; |
| 39 | |
| 40 | // Constructor. |
| 41 | BOOST_ASIO_DECL posix_mutex(); |
| 42 | |
| 43 | // Destructor. |
| 44 | ~posix_mutex() |
| 45 | { |
| 46 | ::pthread_mutex_destroy(mutex: &mutex_); // Ignore EBUSY. |
| 47 | } |
| 48 | |
| 49 | // Lock the mutex. |
| 50 | void lock() |
| 51 | { |
| 52 | (void)::pthread_mutex_lock(mutex: &mutex_); // Ignore EINVAL. |
| 53 | } |
| 54 | |
| 55 | // Unlock the mutex. |
| 56 | void unlock() |
| 57 | { |
| 58 | (void)::pthread_mutex_unlock(mutex: &mutex_); // Ignore EINVAL. |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | friend class posix_event; |
| 63 | ::pthread_mutex_t mutex_; |
| 64 | }; |
| 65 | |
| 66 | } // namespace detail |
| 67 | } // namespace asio |
| 68 | } // namespace boost |
| 69 | |
| 70 | #include <boost/asio/detail/pop_options.hpp> |
| 71 | |
| 72 | #if defined(BOOST_ASIO_HEADER_ONLY) |
| 73 | # include <boost/asio/detail/impl/posix_mutex.ipp> |
| 74 | #endif // defined(BOOST_ASIO_HEADER_ONLY) |
| 75 | |
| 76 | #endif // defined(BOOST_ASIO_HAS_PTHREADS) |
| 77 | |
| 78 | #endif // BOOST_ASIO_DETAIL_POSIX_MUTEX_HPP |
| 79 |
