2 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
14 #include "webrtc/base/criticalsection.h"
15 #include "webrtc/base/event.h"
16 #include "webrtc/base/gunit.h"
17 #include "webrtc/base/scopedptrcollection.h"
18 #include "webrtc/base/thread.h"
24 const int kLongTime = 10000; // 10 seconds
25 const int kNumThreads = 16;
26 const int kOperationsToRun = 1000;
29 class AtomicOpRunner : public MessageHandler {
31 explicit AtomicOpRunner(int initial_value)
32 : value_(initial_value),
34 start_event_(true, false),
35 done_event_(true, false) {}
37 int value() const { return value_; }
40 // Signal all threads to start.
43 // Wait for all threads to finish.
44 return done_event_.Wait(kLongTime);
47 void SetExpectedThreadCount(int count) {
48 threads_active_ = count;
51 virtual void OnMessage(Message* msg) {
52 std::vector<int> values;
53 values.reserve(kOperationsToRun);
56 ASSERT_TRUE(start_event_.Wait(kLongTime));
58 // Generate a bunch of values by updating value_ atomically.
59 for (int i = 0; i < kOperationsToRun; ++i) {
60 values.push_back(T::AtomicOp(&value_));
63 { // Add them all to the set.
64 CritScope cs(&all_values_crit_);
65 for (size_t i = 0; i < values.size(); ++i) {
66 std::pair<std::set<int>::iterator, bool> result =
67 all_values_.insert(values[i]);
68 // Each value should only be taken by one thread, so if this value
69 // has already been added, something went wrong.
70 EXPECT_TRUE(result.second)
71 << "Thread=" << Thread::Current() << " value=" << values[i];
75 // Signal that we're done.
76 if (AtomicOps::Decrement(&threads_active_) == 0) {
84 CriticalSection all_values_crit_;
85 std::set<int> all_values_;
91 static int AtomicOp(int* i) { return AtomicOps::Increment(i); }
95 static int AtomicOp(int* i) { return AtomicOps::Decrement(i); }
98 void StartThreads(ScopedPtrCollection<Thread>* threads,
99 MessageHandler* handler) {
100 for (int i = 0; i < kNumThreads; ++i) {
101 Thread* thread = new Thread();
103 thread->Post(handler);
104 threads->PushBack(thread);
110 TEST(AtomicOpsTest, Simple) {
112 EXPECT_EQ(1, AtomicOps::Increment(&value));
114 EXPECT_EQ(2, AtomicOps::Increment(&value));
116 EXPECT_EQ(1, AtomicOps::Decrement(&value));
118 EXPECT_EQ(0, AtomicOps::Decrement(&value));
122 TEST(AtomicOpsTest, Increment) {
123 // Create and start lots of threads.
124 AtomicOpRunner<IncrementOp> runner(0);
125 ScopedPtrCollection<Thread> threads;
126 StartThreads(&threads, &runner);
127 runner.SetExpectedThreadCount(kNumThreads);
129 // Release the hounds!
130 EXPECT_TRUE(runner.Run());
131 EXPECT_EQ(kOperationsToRun * kNumThreads, runner.value());
134 TEST(AtomicOpsTest, Decrement) {
135 // Create and start lots of threads.
136 AtomicOpRunner<DecrementOp> runner(kOperationsToRun * kNumThreads);
137 ScopedPtrCollection<Thread> threads;
138 StartThreads(&threads, &runner);
139 runner.SetExpectedThreadCount(kNumThreads);
141 // Release the hounds!
142 EXPECT_TRUE(runner.Run());
143 EXPECT_EQ(0, runner.value());