pthread_helpers.hpp 5.4 KB

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