OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef DoubleBufferedDeque_h |
| 6 #define DoubleBufferedDeque_h |
| 7 |
| 8 #include "wtf/Deque.h" |
| 9 #include "wtf/Noncopyable.h" |
| 10 #include "wtf/ThreadingPrimitives.h" |
| 11 |
| 12 namespace blink { |
| 13 |
| 14 template <typename T> class DoubleBufferedDeque { |
| 15 WTF_MAKE_NONCOPYABLE(DoubleBufferedDeque); |
| 16 public: |
| 17 DoubleBufferedDeque() |
| 18 : m_activeIndex(0) { } |
| 19 |
| 20 void append(const T& value) |
| 21 { |
| 22 Locker<Mutex> lock(m_mutex); |
| 23 m_queue[m_activeIndex].append(value); |
| 24 } |
| 25 |
| 26 bool isEmpty() |
| 27 { |
| 28 Locker<Mutex> lock(m_mutex); |
| 29 return m_queue[m_activeIndex].isEmpty(); |
| 30 } |
| 31 |
| 32 WTF::Deque<T>& swapBuffers() |
| 33 { |
| 34 Locker<Mutex> lock(m_mutex); |
| 35 int oldIndex = m_activeIndex; |
| 36 m_activeIndex ^= 1; |
| 37 ASSERT(m_queue[m_activeIndex].isEmpty()); |
| 38 return m_queue[oldIndex]; |
| 39 } |
| 40 |
| 41 private: |
| 42 WTF::Deque<T> m_queue[2]; |
| 43 int m_activeIndex; |
| 44 Mutex m_mutex; |
| 45 }; |
| 46 |
| 47 } // namespace blink |
| 48 |
| 49 #endif // DoubleBufferedDeque_h |
OLD | NEW |