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 AtomicRingbuffer_h | |
6 #define AtomicRingbuffer_h | |
7 | |
8 #include "wtf/Atomics.h" | |
9 #include "wtf/Noncopyable.h" | |
10 #include "wtf/Vector.h" | |
11 | |
12 namespace blink { | |
13 | |
14 template <typename T, int size> class AtomicRingBuffer { | |
15 WTF_MAKE_NONCOPYABLE(AtomicRingBuffer); | |
16 public: | |
17 AtomicRingBuffer() : m_writeCounter(0), m_readIndex(1) | |
Sami
2014/08/06 10:19:52
nit: Blink has a different style for initializers,
alexclarke
2014/08/07 12:08:06
Acknowledged.
| |
18 { | |
19 m_values.resize(size); | |
20 for (int i = 0; i < size; i++) | |
21 m_values[i] = nullptr; | |
22 } | |
23 | |
24 // May be called from any thread | |
25 void pushMaySpinlock(T* value) | |
26 { | |
27 int pos = atomicAdd(&m_writeCounter, 1) % size; | |
28 while (!atomicTestAndSwap((void* volatile*) &m_values[pos], 0, value)) { | |
Sami
2014/08/06 10:19:52
nit: nullptr instead of 0?
alexclarke
2014/08/07 12:08:06
Acknowledged.
| |
29 // spin | |
30 } | |
31 } | |
32 | |
33 // Returns null if there's nothing queued up. May only be called from the ma in thread. | |
34 T* pop() | |
35 { | |
36 T* value = (T*) atomicExchange((void* volatile*) &m_values[m_readIndex], nullptr); | |
Sami
2014/08/06 10:19:52
Please use C++-style casts instead of C ones. Is t
alexclarke
2014/08/07 12:08:06
Acknowledged.
| |
37 if (value) | |
38 m_readIndex = (m_readIndex + 1) % size; | |
39 return value; | |
40 } | |
41 | |
42 private: | |
43 volatile int m_writeCounter; | |
Sami
2014/08/06 10:19:52
nit: m_writeIndex to match the other var?
alexclarke
2014/08/07 12:08:06
Acknowledged.
| |
44 int m_readIndex; | |
45 Vector<T*> m_values; | |
46 }; | |
47 | |
48 } // namespace blink | |
49 | |
50 #endif // AtomicRingbuffer_h | |
OLD | NEW |