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 "vm/thread_barrier.h" |
6 | #include "platform/assert.h" |
7 | #include "vm/random.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, ThreadBarrier* barrier, uint64_t seed) |
16 | : num_rounds_(num_rounds), barrier_(barrier), rng_(seed) {} |
17 | |
18 | virtual void Run() { |
19 | for (intptr_t i = 0; i < num_rounds_; ++i) { |
20 | RandomSleep(); |
21 | barrier_->Sync(); |
22 | } |
23 | barrier_->Exit(); |
24 | } |
25 | |
26 | private: |
27 | void RandomSleep() { |
28 | int64_t ms = rng_.NextUInt32() % 4; |
29 | if (ms > 0) { |
30 | OS::Sleep(ms); |
31 | } |
32 | } |
33 | |
34 | const intptr_t num_rounds_; |
35 | ThreadBarrier* barrier_; |
36 | Random rng_; |
37 | }; |
38 | |
39 | VM_UNIT_TEST_CASE(ThreadBarrier) { |
40 | static const intptr_t kNumTasks = 5; |
41 | static const intptr_t kNumRounds = 500; |
42 | |
43 | Monitor* monitor = new Monitor(); |
44 | Monitor* monitor_done = new Monitor(); |
45 | { |
46 | ThreadBarrier barrier(kNumTasks + 1, monitor, monitor_done); |
47 | for (intptr_t i = 0; i < kNumTasks; ++i) { |
48 | Dart::thread_pool()->Run<FuzzTask>(kNumRounds, &barrier, i + 1); |
49 | } |
50 | for (intptr_t i = 0; i < kNumRounds; ++i) { |
51 | barrier.Sync(); |
52 | } |
53 | barrier.Exit(); |
54 | } |
55 | |
56 | delete monitor_done; |
57 | delete monitor; |
58 | } |
59 | |
60 | } // namespace dart |
61 |