Block 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* -*-c++-*- OpenThreads - Copyright (C) 1998-2007 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef _OPENTHREADS_BLOCK_
  14. #define _OPENTHREADS_BLOCK_
  15. #include <OpenThreads/Thread>
  16. #include <OpenThreads/Barrier>
  17. #include <OpenThreads/Condition>
  18. #include <OpenThreads/ScopedLock>
  19. namespace OpenThreads {
  20. /** Block is a block that can be used to halt a thread that is waiting another thread to release it.*/
  21. class Block
  22. {
  23. public:
  24. Block():
  25. _released(false) {}
  26. ~Block()
  27. {
  28. release();
  29. }
  30. inline bool block()
  31. {
  32. ScopedLock<OpenThreads::Mutex> mutlock(_mut);
  33. if( !_released )
  34. {
  35. return _cond.wait(&_mut)==0;
  36. }
  37. else
  38. {
  39. return true;
  40. }
  41. }
  42. inline bool block(unsigned long timeout)
  43. {
  44. ScopedLock<OpenThreads::Mutex> mutlock(_mut);
  45. if( !_released )
  46. {
  47. return _cond.wait(&_mut, timeout)==0;
  48. }
  49. else
  50. {
  51. return true;
  52. }
  53. }
  54. inline void release()
  55. {
  56. ScopedLock<OpenThreads::Mutex> mutlock(_mut);
  57. if (!_released)
  58. {
  59. _released = true;
  60. _cond.broadcast();
  61. }
  62. }
  63. inline void reset()
  64. {
  65. ScopedLock<OpenThreads::Mutex> mutlock(_mut);
  66. _released = false;
  67. }
  68. inline void set(bool doRelease)
  69. {
  70. if (doRelease!=_released)
  71. {
  72. if (doRelease) release();
  73. else reset();
  74. }
  75. }
  76. protected:
  77. Mutex _mut;
  78. Condition _cond;
  79. bool _released;
  80. private:
  81. Block(const Block&) {}
  82. };
  83. /** BlockCount is a block that can be used to halt a thread that is waiting for a specified number of operations to be completed.*/
  84. class BlockCount
  85. {
  86. public:
  87. BlockCount(unsigned int blockCount):
  88. _blockCount(blockCount),
  89. _currentCount(0) {}
  90. ~BlockCount()
  91. {
  92. _blockCount = 0;
  93. release();
  94. }
  95. inline void completed()
  96. {
  97. OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
  98. if (_currentCount>0)
  99. {
  100. --_currentCount;
  101. if (_currentCount==0)
  102. {
  103. // osg::notify(osg::NOTICE)<<"Released"<<std::endl;
  104. _cond.broadcast();
  105. }
  106. }
  107. }
  108. inline void block()
  109. {
  110. OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
  111. if (_currentCount)
  112. _cond.wait(&_mut);
  113. }
  114. inline void reset()
  115. {
  116. OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
  117. if (_currentCount!=_blockCount)
  118. {
  119. if (_blockCount==0) _cond.broadcast();
  120. _currentCount = _blockCount;
  121. }
  122. }
  123. inline void release()
  124. {
  125. OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
  126. if (_currentCount)
  127. {
  128. _currentCount = 0;
  129. _cond.broadcast();
  130. }
  131. }
  132. inline void setBlockCount(unsigned int blockCount) { _blockCount = blockCount; }
  133. inline unsigned int getBlockCount() const { return _blockCount; }
  134. inline unsigned int getCurrentCount() const { return _currentCount; }
  135. protected:
  136. OpenThreads::Mutex _mut;
  137. OpenThreads::Condition _cond;
  138. unsigned int _blockCount;
  139. unsigned int _currentCount;
  140. private:
  141. BlockCount(const BlockCount&) {}
  142. };
  143. }
  144. #endif