win_mutex.ipp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // detail/impl/win_mutex.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 ASIO_DETAIL_IMPL_WIN_MUTEX_IPP
  11. #define ASIO_DETAIL_IMPL_WIN_MUTEX_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_WINDOWS)
  17. #include "asio/detail/throw_error.hpp"
  18. #include "asio/detail/win_mutex.hpp"
  19. #include "asio/error.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. win_mutex::win_mutex()
  24. {
  25. int error = do_init();
  26. asio::error_code ec(error,
  27. asio::error::get_system_category());
  28. asio::detail::throw_error(ec, "mutex");
  29. }
  30. int win_mutex::do_init()
  31. {
  32. #if defined(__MINGW32__)
  33. // Not sure if MinGW supports structured exception handling, so for now
  34. // we'll just call the Windows API and hope.
  35. # if defined(UNDER_CE)
  36. ::InitializeCriticalSection(&crit_section_);
  37. # else
  38. if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
  39. return ::GetLastError();
  40. # endif
  41. return 0;
  42. #else
  43. __try
  44. {
  45. # if defined(UNDER_CE)
  46. ::InitializeCriticalSection(&crit_section_);
  47. # else
  48. if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
  49. return ::GetLastError();
  50. # endif
  51. }
  52. __except(GetExceptionCode() == STATUS_NO_MEMORY
  53. ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
  54. {
  55. return ERROR_OUTOFMEMORY;
  56. }
  57. return 0;
  58. #endif
  59. }
  60. } // namespace detail
  61. } // namespace asio
  62. #include "asio/detail/pop_options.hpp"
  63. #endif // defined(ASIO_WINDOWS)
  64. #endif // ASIO_DETAIL_IMPL_WIN_MUTEX_IPP