win_mutex.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // detail/win_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_WIN_MUTEX_HPP
  11. #define BOOST_ASIO_DETAIL_WIN_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_WINDOWS)
  17. #include <boost/asio/detail/noncopyable.hpp>
  18. #include <boost/asio/detail/scoped_lock.hpp>
  19. #include <boost/asio/detail/socket_types.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. class win_mutex
  25. : private noncopyable
  26. {
  27. public:
  28. typedef boost::asio::detail::scoped_lock<win_mutex> scoped_lock;
  29. // Constructor.
  30. BOOST_ASIO_DECL win_mutex();
  31. // Destructor.
  32. ~win_mutex()
  33. {
  34. ::DeleteCriticalSection(&crit_section_);
  35. }
  36. // Lock the mutex.
  37. void lock()
  38. {
  39. ::EnterCriticalSection(&crit_section_);
  40. }
  41. // Unlock the mutex.
  42. void unlock()
  43. {
  44. ::LeaveCriticalSection(&crit_section_);
  45. }
  46. private:
  47. // Initialisation must be performed in a separate function to the constructor
  48. // since the compiler does not support the use of structured exceptions and
  49. // C++ exceptions in the same function.
  50. BOOST_ASIO_DECL int do_init();
  51. ::CRITICAL_SECTION crit_section_;
  52. };
  53. } // namespace detail
  54. } // namespace asio
  55. } // namespace boost
  56. #include <boost/asio/detail/pop_options.hpp>
  57. #if defined(BOOST_ASIO_HEADER_ONLY)
  58. # include <boost/asio/detail/impl/win_mutex.ipp>
  59. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  60. #endif // defined(BOOST_ASIO_WINDOWS)
  61. #endif // BOOST_ASIO_DETAIL_WIN_MUTEX_HPP