Home | History | Annotate | Download | only in wtf
      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 
     11 namespace WTF {
     12 
     13 // A helper class for managing double buffered deques, typically where the client locks when appending or swapping.
     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         m_queue[m_activeIndex].append(value);
     23     }
     24 
     25     bool isEmpty() const
     26     {
     27         return m_queue[m_activeIndex].isEmpty();
     28     }
     29 
     30     Deque<T>& swapBuffers()
     31     {
     32         int oldIndex = m_activeIndex;
     33         m_activeIndex ^= 1;
     34         ASSERT(m_queue[m_activeIndex].isEmpty());
     35         return m_queue[oldIndex];
     36     }
     37 
     38 private:
     39     Deque<T> m_queue[2];
     40     int m_activeIndex;
     41 };
     42 
     43 } // namespace WTF
     44 
     45 using WTF::DoubleBufferedDeque;
     46 
     47 #endif // DoubleBufferedDeque_h
     48