pthread_helpers.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_PTHREAD_HELPERS_HPP
  11. #define BOOST_INTERPROCESS_PTHREAD_HELPERS_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 <pthread.h>
  22. #include <errno.h>
  23. #include <boost/interprocess/exceptions.hpp>
  24. namespace boost {
  25. namespace interprocess {
  26. namespace ipcdetail{
  27. #if defined BOOST_INTERPROCESS_POSIX_PROCESS_SHARED
  28. //!Makes pthread_mutexattr_t cleanup easy when using exceptions
  29. struct mutexattr_wrapper
  30. {
  31. //!Constructor
  32. mutexattr_wrapper(bool recursive = false)
  33. {
  34. if(pthread_mutexattr_init(&m_attr)!=0 ||
  35. pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0 ||
  36. (recursive &&
  37. pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_RECURSIVE)!= 0 ))
  38. throw interprocess_exception("pthread_mutexattr_xxxx failed");
  39. }
  40. //!Destructor
  41. ~mutexattr_wrapper() { pthread_mutexattr_destroy(&m_attr); }
  42. //!This allows using mutexattr_wrapper as pthread_mutexattr_t
  43. operator pthread_mutexattr_t&() { return m_attr; }
  44. pthread_mutexattr_t m_attr;
  45. };
  46. //!Makes pthread_condattr_t cleanup easy when using exceptions
  47. struct condattr_wrapper
  48. {
  49. //!Constructor
  50. condattr_wrapper()
  51. {
  52. if(pthread_condattr_init(&m_attr)!=0 ||
  53. pthread_condattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0)
  54. throw interprocess_exception("pthread_condattr_xxxx failed");
  55. }
  56. //!Destructor
  57. ~condattr_wrapper() { pthread_condattr_destroy(&m_attr); }
  58. //!This allows using condattr_wrapper as pthread_condattr_t
  59. operator pthread_condattr_t&(){ return m_attr; }
  60. pthread_condattr_t m_attr;
  61. };
  62. //!Makes initialized pthread_mutex_t cleanup easy when using exceptions
  63. class mutex_initializer
  64. {
  65. public:
  66. //!Constructor. Takes interprocess_mutex attributes to initialize the interprocess_mutex
  67. mutex_initializer(pthread_mutex_t &mut, pthread_mutexattr_t &mut_attr)
  68. : mp_mut(&mut)
  69. {
  70. if(pthread_mutex_init(mp_mut, &mut_attr) != 0)
  71. throw interprocess_exception("pthread_mutex_init failed");
  72. }
  73. ~mutex_initializer() { if(mp_mut) pthread_mutex_destroy(mp_mut); }
  74. void release() {mp_mut = 0; }
  75. private:
  76. pthread_mutex_t *mp_mut;
  77. };
  78. //!Makes initialized pthread_cond_t cleanup easy when using exceptions
  79. class condition_initializer
  80. {
  81. public:
  82. condition_initializer(pthread_cond_t &cond, pthread_condattr_t &cond_attr)
  83. : mp_cond(&cond)
  84. {
  85. if(pthread_cond_init(mp_cond, &cond_attr)!= 0)
  86. throw interprocess_exception("pthread_cond_init failed");
  87. }
  88. ~condition_initializer() { if(mp_cond) pthread_cond_destroy(mp_cond); }
  89. void release() { mp_cond = 0; }
  90. private:
  91. pthread_cond_t *mp_cond;
  92. };
  93. #endif // #if defined BOOST_INTERPROCESS_POSIX_PROCESS_SHARED
  94. #if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
  95. //!Makes pthread_barrierattr_t cleanup easy when using exceptions
  96. struct barrierattr_wrapper
  97. {
  98. //!Constructor
  99. barrierattr_wrapper()
  100. {
  101. if(pthread_barrierattr_init(&m_attr)!=0 ||
  102. pthread_barrierattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0)
  103. throw interprocess_exception("pthread_barrierattr_xxx failed");
  104. }
  105. //!Destructor
  106. ~barrierattr_wrapper() { pthread_barrierattr_destroy(&m_attr); }
  107. //!This allows using mutexattr_wrapper as pthread_barrierattr_t
  108. operator pthread_barrierattr_t&() { return m_attr; }
  109. pthread_barrierattr_t m_attr;
  110. };
  111. //!Makes initialized pthread_barrier_t cleanup easy when using exceptions
  112. class barrier_initializer
  113. {
  114. public:
  115. //!Constructor. Takes barrier attributes to initialize the barrier
  116. barrier_initializer(pthread_barrier_t &mut,
  117. pthread_barrierattr_t &mut_attr,
  118. int count)
  119. : mp_barrier(&mut)
  120. {
  121. if(pthread_barrier_init(mp_barrier, &mut_attr, count) != 0)
  122. throw interprocess_exception("pthread_barrier_init failed");
  123. }
  124. ~barrier_initializer() { if(mp_barrier) pthread_barrier_destroy(mp_barrier); }
  125. void release() {mp_barrier = 0; }
  126. private:
  127. pthread_barrier_t *mp_barrier;
  128. };
  129. #endif //#if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
  130. }//namespace ipcdetail
  131. }//namespace interprocess
  132. }//namespace boost
  133. #include <boost/interprocess/detail/config_end.hpp>
  134. #endif //ifdef BOOST_INTERPROCESS_PTHREAD_HELPERS_HPP