1 /*-------------------------------------------------------------------------
2 * drawElements C++ Base Library
3 * -----------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Thread-safe ring buffer template.
22 *//*--------------------------------------------------------------------*/
24 #include "deThreadSafeRingBuffer.hpp"
25 #include "deRandom.hpp"
26 #include "deThread.hpp"
42 Message (deUint16 threadId, deUint16 payload)
43 : data((threadId << 16) | payload)
52 deUint16 getThreadId (void) const { return (deUint16)(data >> 16); }
53 deUint16 getPayload (void) const { return (deUint16)(data & 0xffff); }
56 class Consumer : public Thread
59 Consumer (ThreadSafeRingBuffer<Message>& buffer, int numProducers)
62 m_lastPayload.resize(numProducers, 0);
63 m_payloadSum.resize(numProducers, 0);
70 Message msg = m_buffer.popBack();
72 deUint16 threadId = msg.getThreadId();
74 if (threadId == 0xffff)
77 DE_TEST_ASSERT(de::inBounds<int>(threadId, 0, (int)m_lastPayload.size()));
78 DE_TEST_ASSERT((m_lastPayload[threadId] == 0 && msg.getPayload() == 0) || m_lastPayload[threadId] < msg.getPayload());
80 m_lastPayload[threadId] = msg.getPayload();
81 m_payloadSum[threadId] += (deUint32)msg.getPayload();
85 deUint32 getPayloadSum (deUint16 threadId) const
87 return m_payloadSum[threadId];
91 ThreadSafeRingBuffer<Message>& m_buffer;
92 vector<deUint16> m_lastPayload;
93 vector<deUint32> m_payloadSum;
96 class Producer : public Thread
99 Producer (ThreadSafeRingBuffer<Message>& buffer, deUint16 threadId, int dataSize)
101 , m_threadId (threadId)
102 , m_dataSize (dataSize)
108 // Yield to give main thread chance to start other producers.
111 for (int ndx = 0; ndx < m_dataSize; ndx++)
112 m_buffer.pushFront(Message(m_threadId, (deUint16)ndx));
116 ThreadSafeRingBuffer<Message>& m_buffer;
123 void ThreadSafeRingBuffer_selfTest (void)
125 const int numIterations = 16;
126 for (int iterNdx = 0; iterNdx < numIterations; iterNdx++)
128 Random rnd (iterNdx);
129 int bufSize = rnd.getInt(1, 2048);
130 int numProducers = rnd.getInt(1, 16);
131 int numConsumers = rnd.getInt(1, 16);
132 int dataSize = rnd.getInt(1000, 10000);
133 ThreadSafeRingBuffer<Message> buffer (bufSize);
134 vector<Producer*> producers;
135 vector<Consumer*> consumers;
137 for (int i = 0; i < numProducers; i++)
138 producers.push_back(new Producer(buffer, (deUint16)i, dataSize));
140 for (int i = 0; i < numConsumers; i++)
141 consumers.push_back(new Consumer(buffer, numProducers));
144 for (vector<Consumer*>::iterator i = consumers.begin(); i != consumers.end(); i++)
148 for (vector<Producer*>::iterator i = producers.begin(); i != producers.end(); i++)
151 // Wait for producers.
152 for (vector<Producer*>::iterator i = producers.begin(); i != producers.end(); i++)
155 // Write end messages for consumers.
156 for (int i = 0; i < numConsumers; i++)
157 buffer.pushFront(Message(0xffff, 0));
159 // Wait for consumers.
160 for (vector<Consumer*>::iterator i = consumers.begin(); i != consumers.end(); i++)
163 // Verify payload sums.
165 for (int i = 0; i < dataSize; i++)
166 refSum += (deUint32)(deUint16)i;
168 for (int i = 0; i < numProducers; i++)
171 for (int j = 0; j < numConsumers; j++)
172 cmpSum += consumers[j]->getPayloadSum((deUint16)i);
173 DE_TEST_ASSERT(refSum == cmpSum);
177 for (vector<Producer*>::iterator i = producers.begin(); i != producers.end(); i++)
179 for (vector<Consumer*>::iterator i = consumers.begin(); i != consumers.end(); i++)