named_semaphore.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_NAMED_CONDITION_HPP
  11. #define BOOST_INTERPROCESS_POSIX_NAMED_CONDITION_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/posix/semaphore_wrapper.hpp>
  22. namespace boost {
  23. namespace interprocess {
  24. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  25. namespace ipcdetail{ class interprocess_tester; }
  26. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  27. namespace ipcdetail {
  28. class posix_named_semaphore
  29. {
  30. posix_named_semaphore();
  31. posix_named_semaphore(const posix_named_semaphore&);
  32. posix_named_semaphore &operator= (const posix_named_semaphore &);
  33. public:
  34. posix_named_semaphore
  35. (create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions())
  36. { semaphore_open(mp_sem, DoCreate, name, initialCount, perm); }
  37. posix_named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions())
  38. { semaphore_open(mp_sem, DoOpenOrCreate, name, initialCount, perm); }
  39. posix_named_semaphore(open_only_t, const char *name)
  40. { semaphore_open(mp_sem, DoOpen, name); }
  41. ~posix_named_semaphore()
  42. {
  43. if(mp_sem != BOOST_INTERPROCESS_POSIX_SEM_FAILED)
  44. semaphore_close(mp_sem);
  45. }
  46. void post()
  47. { semaphore_post(mp_sem); }
  48. void wait()
  49. { semaphore_wait(mp_sem); }
  50. bool try_wait()
  51. { return semaphore_try_wait(mp_sem); }
  52. bool timed_wait(const boost::posix_time::ptime &abs_time)
  53. { return semaphore_timed_wait(mp_sem, abs_time); }
  54. static bool remove(const char *name)
  55. { return semaphore_unlink(name); }
  56. private:
  57. friend class ipcdetail::interprocess_tester;
  58. void dont_close_on_destruction()
  59. { mp_sem = BOOST_INTERPROCESS_POSIX_SEM_FAILED; }
  60. sem_t *mp_sem;
  61. };
  62. } //namespace ipcdetail {
  63. } //namespace interprocess {
  64. } //namespace boost {
  65. #include <boost/interprocess/detail/config_end.hpp>
  66. #endif //#ifndef BOOST_INTERPROCESS_POSIX_NAMED_CONDITION_HPP