semaphore.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-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_POSIX_SEMAPHORE_HPP
  11. #define BOOST_INTERPROCESS_POSIX_SEMAPHORE_HPP
  12. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  18. #include <boost/interprocess/sync/posix/semaphore_wrapper.hpp>
  19. namespace boost {
  20. namespace interprocess {
  21. namespace ipcdetail {
  22. class posix_semaphore
  23. {
  24. posix_semaphore();
  25. posix_semaphore(const posix_semaphore&);
  26. posix_semaphore &operator= (const posix_semaphore &);
  27. public:
  28. posix_semaphore(unsigned int initialCount)
  29. { semaphore_init(&m_sem, initialCount); }
  30. ~posix_semaphore()
  31. { semaphore_destroy(&m_sem); }
  32. void post()
  33. { semaphore_post(&m_sem); }
  34. void wait()
  35. { semaphore_wait(&m_sem); }
  36. bool try_wait()
  37. { return semaphore_try_wait(&m_sem); }
  38. bool timed_wait(const boost::posix_time::ptime &abs_time)
  39. { return semaphore_timed_wait(&m_sem, abs_time); }
  40. private:
  41. sem_t m_sem;
  42. };
  43. } //namespace ipcdetail {
  44. } //namespace interprocess {
  45. } //namespace boost {
  46. #include <boost/interprocess/detail/config_end.hpp>
  47. #endif //#ifndef BOOST_INTERPROCESS_POSIX_SEMAPHORE_HPP