winapi_wrapper_common.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2011-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_DETAIL_WINAPI_WRAPPER_COMMON_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_WINAPI_WRAPPER_COMMON_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/interprocess/detail/win32_api.hpp>
  22. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  23. #include <boost/interprocess/errors.hpp>
  24. #include <boost/interprocess/exceptions.hpp>
  25. #include <limits>
  26. namespace boost {
  27. namespace interprocess {
  28. namespace ipcdetail {
  29. inline void winapi_wrapper_wait_for_single_object(void *handle)
  30. {
  31. unsigned long ret = winapi::wait_for_single_object(handle, winapi::infinite_time);
  32. if(ret != winapi::wait_object_0){
  33. if(ret != winapi::wait_abandoned){
  34. error_info err = system_error_code();
  35. throw interprocess_exception(err);
  36. }
  37. else{ //Special case for orphaned mutexes
  38. winapi::release_mutex(handle);
  39. throw interprocess_exception(owner_dead_error);
  40. }
  41. }
  42. }
  43. inline bool winapi_wrapper_try_wait_for_single_object(void *handle)
  44. {
  45. unsigned long ret = winapi::wait_for_single_object(handle, 0);
  46. if(ret == winapi::wait_object_0){
  47. return true;
  48. }
  49. else if(ret == winapi::wait_timeout){
  50. return false;
  51. }
  52. else{
  53. error_info err = system_error_code();
  54. throw interprocess_exception(err);
  55. }
  56. }
  57. inline bool winapi_wrapper_timed_wait_for_single_object(void *handle, const boost::posix_time::ptime &abs_time)
  58. {
  59. //Windows does not support infinity abs_time so check it
  60. if(abs_time == boost::posix_time::pos_infin){
  61. winapi_wrapper_wait_for_single_object(handle);
  62. return true;
  63. }
  64. const boost::posix_time::ptime cur_time = microsec_clock::universal_time();
  65. //Windows uses relative wait times so check for negative waits
  66. //and implement as 0 wait to allow try-semantics as POSIX mandates.
  67. unsigned long ret = winapi::wait_for_single_object
  68. ( handle
  69. , (abs_time <= cur_time) ? 0u
  70. : (abs_time - cur_time).total_milliseconds()
  71. );
  72. if(ret == winapi::wait_object_0){
  73. return true;
  74. }
  75. else if(ret == winapi::wait_timeout){
  76. return false;
  77. }
  78. else{
  79. error_info err = system_error_code();
  80. throw interprocess_exception(err);
  81. }
  82. }
  83. } //namespace ipcdetail {
  84. } //namespace interprocess {
  85. } //namespace boost {
  86. #include <boost/interprocess/detail/config_end.hpp>
  87. #endif //BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP