posix_mutex.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. //
  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/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/config.hpp>
  18. #include <boost/system/system_error.hpp>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. #if defined(BOOST_HAS_PTHREADS)
  21. #include <boost/asio/detail/push_options.hpp>
  22. #include <boost/throw_exception.hpp>
  23. #include <pthread.h>
  24. #include <boost/asio/detail/pop_options.hpp>
  25. #include <boost/asio/error.hpp>
  26. #include <boost/asio/detail/noncopyable.hpp>
  27. #include <boost/asio/detail/scoped_lock.hpp>
  28. namespace boost {
  29. namespace asio {
  30. namespace detail {
  31. class posix_event;
  32. class posix_mutex
  33. : private noncopyable
  34. {
  35. public:
  36. typedef boost::asio::detail::scoped_lock<posix_mutex> scoped_lock;
  37. // Constructor.
  38. posix_mutex()
  39. {
  40. int error = ::pthread_mutex_init(&mutex_, 0);
  41. if (error != 0)
  42. {
  43. boost::system::system_error e(
  44. boost::system::error_code(error,
  45. boost::asio::error::get_system_category()),
  46. "mutex");
  47. boost::throw_exception(e);
  48. }
  49. }
  50. // Destructor.
  51. ~posix_mutex()
  52. {
  53. ::pthread_mutex_destroy(&mutex_);
  54. }
  55. // Lock the mutex.
  56. void lock()
  57. {
  58. int error = ::pthread_mutex_lock(&mutex_);
  59. if (error != 0)
  60. {
  61. boost::system::system_error e(
  62. boost::system::error_code(error,
  63. boost::asio::error::get_system_category()),
  64. "mutex");
  65. boost::throw_exception(e);
  66. }
  67. }
  68. // Unlock the mutex.
  69. void unlock()
  70. {
  71. int error = ::pthread_mutex_unlock(&mutex_);
  72. if (error != 0)
  73. {
  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);
  79. }
  80. }
  81. private:
  82. friend class posix_event;
  83. ::pthread_mutex_t mutex_;
  84. };
  85. } // namespace detail
  86. } // namespace asio
  87. } // namespace boost
  88. #endif // defined(BOOST_HAS_PTHREADS)
  89. #include <boost/asio/detail/pop_options.hpp>
  90. #endif // BOOST_ASIO_DETAIL_POSIX_MUTEX_HPP