#
root/OpenSceneGraph/trunk/src/osgPlugins/ffmpeg/MessageQueue.hpp
@
9816

Revision 9816, 2.5 kB (checked in by robert, 8 years ago) |
---|

Rev | Line | |
---|---|---|

[9816] | 1 | |

2 | #ifndef HEADER_GUARD_OSGFFMPEG_MESSAGE_QUEUE_H | |

3 | #define HEADER_GUARD_OSGFFMPEG_MESSAGE_QUEUE_H | |

4 | ||

5 | #include <OpenThreads/Condition> | |

6 | #include <OpenThreads/Mutex> | |

7 | #include <OpenThreads/ScopedLock> | |

8 | ||

9 | #include <deque> | |

10 | ||

11 | ||

12 | ||

13 | namespace osgFFmpeg { | |

14 | ||

15 | ||

16 | ||

17 | template <class T> | |

18 | class MessageQueue | |

19 | { | |

20 | public: | |

21 | ||

22 | typedef T value_type; | |

23 | typedef size_t size_type; | |

24 | ||

25 | MessageQueue(); | |

26 | ~MessageQueue(); | |

27 | ||

28 | void clear(); | |

29 | ||

30 | void push(const T & value); | |

31 | ||

32 | value_type pop(); | |

33 | value_type tryPop(bool & is_empty); | |

34 | value_type timedPop(bool & is_empty, unsigned long ms); | |

35 | ||

36 | private: | |

37 | ||

38 | MessageQueue(const MessageQueue &); | |

39 | MessageQueue & operator = (const MessageQueue &); | |

40 | ||

41 | typedef std::deque<T> Queue; | |

42 | typedef OpenThreads::Condition Condition; | |

43 | typedef OpenThreads::Mutex Mutex; | |

44 | typedef OpenThreads::ScopedLock<Mutex> ScopedLock; | |

45 | ||

46 | Mutex m_mutex; | |

47 | Condition m_not_empty; | |

48 | Queue m_queue; | |

49 | }; | |

50 | ||

51 | ||

52 | ||

53 | ||

54 | ||

55 | template <class T> | |

56 | MessageQueue<T>::MessageQueue() | |

57 | { | |

58 | ||

59 | } | |

60 | ||

61 | ||

62 | ||

63 | template <class T> | |

64 | MessageQueue<T>::~MessageQueue() | |

65 | { | |

66 | ||

67 | } | |

68 | ||

69 | ||

70 | ||

71 | template <class T> | |

72 | void MessageQueue<T>::clear() | |

73 | { | |

74 | ScopedLock lock(m_mutex); | |

75 | ||

76 | m_queue.clear(); | |

77 | } | |

78 | ||

79 | ||

80 | ||

81 | template <class T> | |

82 | void MessageQueue<T>::push(const T & value) | |

83 | { | |

84 | { | |

85 | ScopedLock lock(m_mutex); | |

86 | m_queue.push_back(value); | |

87 | } | |

88 | ||

89 | m_not_empty.signal(); | |

90 | } | |

91 | ||

92 | ||

93 | ||

94 | template <class T> | |

95 | typename MessageQueue<T>::value_type MessageQueue<T>::pop() | |

96 | { | |

97 | ScopedLock lock(m_mutex); | |

98 | ||

99 | while (m_queue.empty()) | |

100 | m_not_empty.wait(&m_mutex); | |

101 | ||

102 | const value_type value = m_queue.front(); | |

103 | m_queue.pop_front(); | |

104 | ||

105 | return value; | |

106 | } | |

107 | ||

108 | ||

109 | ||

110 | template <class T> | |

111 | typename MessageQueue<T>::value_type MessageQueue<T>::tryPop(bool & is_empty) | |

112 | { | |

113 | ScopedLock lock(m_mutex); | |

114 | ||

115 | is_empty = m_queue.empty(); | |

116 | ||

117 | if (is_empty) | |

118 | return value_type(); | |

119 | ||

120 | const value_type value = m_queue.front(); | |

121 | m_queue.pop_front(); | |

122 | ||

123 | return value; | |

124 | } | |

125 | ||

126 | ||

127 | ||

128 | template <class T> | |

129 | typename MessageQueue<T>::value_type MessageQueue<T>::timedPop(bool & is_empty, const unsigned long ms) | |

130 | { | |

131 | ScopedLock lock(m_mutex); | |

132 | ||

133 | // We don't wait in a loop to avoid an infinite loop (as the ms timeout would not be decremented). | |

134 | // This means that timedPop() could return with (is_empty = true) before the timeout has been hit. | |

135 | ||

136 | if (m_queue.empty()) | |

137 | m_not_empty.wait(&m_mutex, ms); | |

138 | ||

139 | is_empty = m_queue.empty(); | |

140 | ||

141 | if (is_empty) | |

142 | return value_type(); | |

143 | ||

144 | const value_type value = m_queue.front(); | |

145 | m_queue.pop_front(); | |

146 | ||

147 | return value; | |

148 | } | |

149 | ||

150 | ||

151 | ||

152 | } // namespace osgFFmpeg | |

153 | ||

154 | ||

155 | ||

156 | #endif // HEADER_GUARD_OSGFFMPEG_MESSAGE_QUEUE_H |

**Note:**See TracBrowser for help on using the browser.