posix_mutex.hpp 1.6 KB

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