Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
thread_barrier_test.cc
Go to the documentation of this file.
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
11namespace dart {
12
13class 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_->Release();
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
40 const intptr_t kNumTasks = 5;
41 const intptr_t kNumRounds = 500;
42
43 ThreadBarrier* barrier = new ThreadBarrier(kNumTasks + 1, kNumTasks + 1);
44 for (intptr_t i = 0; i < kNumTasks; ++i) {
45 Dart::thread_pool()->Run<FuzzTask>(kNumRounds, barrier, i + 1);
46 }
47 for (intptr_t i = 0; i < kNumRounds; ++i) {
48 barrier->Sync();
49 }
50 barrier->Release();
51}
52
53} // namespace dart
static ThreadPool * thread_pool()
Definition dart.h:73
FuzzTask(intptr_t num_rounds, ThreadBarrier *barrier, uint64_t seed)
static void Sleep(int64_t millis)
uint32_t NextUInt32()
Definition random.cc:73
bool Run(Args &&... args)
Definition thread_pool.h:45
#define VM_UNIT_TEST_CASE(name)
Definition unit_test.h:33