winapi_wrapper_common.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 bool winapi_wrapper_timed_wait_for_single_object(void *handle, const boost::posix_time::ptime &abs_time);
  30. inline void winapi_wrapper_wait_for_single_object(void *handle)
  31. {
  32. winapi_wrapper_timed_wait_for_single_object(handle, boost::posix_time::pos_infin);
  33. }
  34. inline bool winapi_wrapper_try_wait_for_single_object(void *handle)
  35. {
  36. return winapi_wrapper_timed_wait_for_single_object(handle, boost::posix_time::min_date_time);
  37. }
  38. inline bool winapi_wrapper_timed_wait_for_single_object(void *handle, const boost::posix_time::ptime &abs_time)
  39. {
  40. const boost::posix_time::ptime cur_time = microsec_clock::universal_time();
  41. //Windows uses relative wait times so check for negative waits
  42. //and implement as 0 wait to allow try-semantics as POSIX mandates.
  43. unsigned long time = 0u;
  44. if (abs_time == boost::posix_time::pos_infin){
  45. time = winapi::infinite_time;
  46. }
  47. else if(abs_time > cur_time){
  48. time = (abs_time - cur_time).total_milliseconds();
  49. }
  50. unsigned long ret = winapi::wait_for_single_object(handle, time);
  51. if(ret == winapi::wait_object_0){
  52. return true;
  53. }
  54. else if(ret == winapi::wait_timeout){
  55. return false;
  56. }
  57. else if(ret == winapi::wait_abandoned){ //Special case for orphaned mutexes
  58. winapi::release_mutex(handle);
  59. throw interprocess_exception(owner_dead_error);
  60. }
  61. else{
  62. error_info err = system_error_code();
  63. throw interprocess_exception(err);
  64. }
  65. }
  66. } //namespace ipcdetail {
  67. } //namespace interprocess {
  68. } //namespace boost {
  69. #include <boost/interprocess/detail/config_end.hpp>
  70. #endif //BOOST_INTERPROCESS_DETAIL_WINAPI_WRAPPER_COMMON_HPP