common_algorithms.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2012-2013. 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_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
  11. #define BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_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/sync/spin/wait.hpp>
  22. namespace boost {
  23. namespace interprocess {
  24. namespace ipcdetail {
  25. template<class MutexType>
  26. bool try_based_timed_lock(MutexType &m, const boost::posix_time::ptime &abs_time)
  27. {
  28. //Same as lock()
  29. if(abs_time == boost::posix_time::pos_infin){
  30. m.lock();
  31. return true;
  32. }
  33. //Always try to lock to achieve POSIX guarantees:
  34. // "Under no circumstance shall the function fail with a timeout if the mutex
  35. // can be locked immediately. The validity of the abs_timeout parameter need not
  36. // be checked if the mutex can be locked immediately."
  37. else if(m.try_lock()){
  38. return true;
  39. }
  40. else{
  41. spin_wait swait;
  42. while(microsec_clock::universal_time() < abs_time){
  43. if(m.try_lock()){
  44. return true;
  45. }
  46. swait.yield();
  47. }
  48. return false;
  49. }
  50. }
  51. template<class MutexType>
  52. void try_based_lock(MutexType &m)
  53. {
  54. if(!m.try_lock()){
  55. spin_wait swait;
  56. do{
  57. if(m.try_lock()){
  58. break;
  59. }
  60. else{
  61. swait.yield();
  62. }
  63. }
  64. while(1);
  65. }
  66. }
  67. template<class MutexType>
  68. void timed_based_lock(MutexType &m, unsigned const uCheckPeriodSec)
  69. {
  70. const boost::posix_time::time_duration dur(0, 0, uCheckPeriodSec);
  71. boost::posix_time::ptime deadline(microsec_clock::universal_time()+dur);
  72. if(!m.timed_lock(deadline)){
  73. spin_wait swait;
  74. do{
  75. deadline = microsec_clock::universal_time()+dur;
  76. if(m.timed_lock(deadline)){
  77. break;
  78. }
  79. else{
  80. swait.yield();
  81. }
  82. }
  83. while(1);
  84. }
  85. }
  86. template<class MutexType>
  87. void timed_based_timed_lock(MutexType &m, const boost::posix_time::ptime &abs_time, unsigned const uCheckPeriodSec)
  88. {
  89. const boost::posix_time::time_duration dur(0, 0, uCheckPeriodSec);
  90. boost::posix_time::ptime deadline(microsec_clock::universal_time()+dur);
  91. if(abs_time <= deadline){
  92. m.timed_lock(abs_time);
  93. }
  94. else if(!m.timed_lock(deadline)){
  95. spin_wait swait;
  96. do{
  97. deadline = microsec_clock::universal_time()+dur;
  98. if(m.timed_lock(deadline)){
  99. break;
  100. }
  101. else{
  102. swait.yield();
  103. }
  104. }
  105. while(1);
  106. }
  107. }
  108. } //namespace ipcdetail
  109. } //namespace interprocess
  110. } //namespace boost
  111. #include <boost/interprocess/detail/config_end.hpp>
  112. #endif //BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP