OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "platform/assert.h" |
| 6 #include "vm/random.h" |
| 7 #include "vm/thread_barrier.h" |
| 8 #include "vm/thread_pool.h" |
| 9 #include "vm/unit_test.h" |
| 10 |
| 11 namespace dart { |
| 12 |
| 13 class FuzzTask : public ThreadPool::Task { |
| 14 public: |
| 15 FuzzTask(intptr_t num_rounds, |
| 16 ThreadBarrier* barrier, |
| 17 uint64_t seed) |
| 18 : num_rounds_(num_rounds), |
| 19 barrier_(barrier), |
| 20 rng_(seed) { |
| 21 } |
| 22 |
| 23 virtual void Run() { |
| 24 for (intptr_t i = 0; i < num_rounds_; ++i) { |
| 25 RandomSleep(); |
| 26 barrier_->Sync(); |
| 27 } |
| 28 barrier_->Exit(); |
| 29 } |
| 30 |
| 31 private: |
| 32 void RandomSleep() { |
| 33 int64_t ms = rng_.NextUInt32() % 4; |
| 34 if (ms > 0) { |
| 35 OS::Sleep(ms); |
| 36 } |
| 37 } |
| 38 |
| 39 const intptr_t num_rounds_; |
| 40 ThreadBarrier* barrier_; |
| 41 Random rng_; |
| 42 }; |
| 43 |
| 44 |
| 45 UNIT_TEST_CASE(ThreadBarrier) { |
| 46 static const intptr_t kNumTasks = 5; |
| 47 static const intptr_t kNumRounds = 500; |
| 48 |
| 49 ThreadBarrier barrier(kNumTasks + 1); |
| 50 for (intptr_t i = 0; i < kNumTasks; ++i) { |
| 51 Dart::thread_pool()->Run(new FuzzTask(kNumRounds, &barrier, i + 1)); |
| 52 } |
| 53 for (intptr_t i = 0; i < kNumRounds; ++i) { |
| 54 barrier.Sync(); |
| 55 } |
| 56 barrier.Exit(); |
| 57 } |
| 58 |
| 59 } // namespace dart |
OLD | NEW |