/Src/Dependencies/Boost/boost/interprocess/sync/emulation/interprocess_semaphore.hpp

http://hadesmem.googlecode.com/ · C++ Header · 75 lines · 47 code · 12 blank · 16 comment · 8 complexity · dfe966da121e7b16599d58ef9f0693cf MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2009. 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. #include<boost/interprocess/exceptions.hpp>
  11. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  12. namespace boost {
  13. namespace interprocess {
  14. inline interprocess_semaphore::~interprocess_semaphore()
  15. {}
  16. inline interprocess_semaphore::interprocess_semaphore(unsigned int initialCount)
  17. { detail::atomic_write32(&this->m_count, boost::uint32_t(initialCount)); }
  18. inline void interprocess_semaphore::post()
  19. {
  20. detail::atomic_inc32(&m_count);
  21. }
  22. inline void interprocess_semaphore::wait()
  23. {
  24. while(!detail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0))){
  25. while(detail::atomic_read32(&m_count) == 0){
  26. detail::thread_yield();
  27. }
  28. }
  29. }
  30. inline bool interprocess_semaphore::try_wait()
  31. {
  32. return detail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0));
  33. }
  34. inline bool interprocess_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
  35. {
  36. if(abs_time == boost::posix_time::pos_infin){
  37. this->wait();
  38. return true;
  39. }
  40. //Obtain current count and target time
  41. boost::posix_time::ptime now(microsec_clock::universal_time());
  42. if(now >= abs_time)
  43. return false;
  44. do{
  45. if(this->try_wait()){
  46. break;
  47. }
  48. now = microsec_clock::universal_time();
  49. if(now >= abs_time){
  50. return this->try_wait();
  51. }
  52. // relinquish current time slice
  53. detail::thread_yield();
  54. }while (true);
  55. return true;
  56. }
  57. /*
  58. inline int interprocess_semaphore::get_count() const
  59. {
  60. return (int)detail::atomic_read32(&m_count);
  61. }*/
  62. } //namespace interprocess {
  63. } //namespace boost {