Files
sdk/runtime/vm/thread_barrier_test.cc
T
Daniel Andersson 17a6b944d1 Add ThreadBarrier; use in GCMarker and unit test
Thread barrier with:
* fixed (at construction) number n of participating threads {T1,T2,T3,...,Tn}
* unknown number of rounds.
Requirements:
* there is some R such that each participating thread makes
  R calls to Sync() followed by its one and only call to Exit().
Guarantees:
* for any two threads Ti and Tj and round number r <= R,
  everything done by Ti before its r'th call to Sync() happens before
  everything done by Tj after its r'th call to Sync().
Note:
* it's not required that the thread that constructs the barrier participates.

BUG=

Review URL: https://codereview.chromium.org//1337943004 .
2015-09-16 12:11:47 -07:00

60 lines
1.3 KiB
C++

// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/assert.h"
#include "vm/random.h"
#include "vm/thread_barrier.h"
#include "vm/thread_pool.h"
#include "vm/unit_test.h"
namespace dart {
class FuzzTask : public ThreadPool::Task {
public:
FuzzTask(intptr_t num_rounds,
ThreadBarrier* barrier,
uint64_t seed)
: num_rounds_(num_rounds),
barrier_(barrier),
rng_(seed) {
}
virtual void Run() {
for (intptr_t i = 0; i < num_rounds_; ++i) {
RandomSleep();
barrier_->Sync();
}
barrier_->Exit();
}
private:
void RandomSleep() {
int64_t ms = rng_.NextUInt32() % 4;
if (ms > 0) {
OS::Sleep(ms);
}
}
const intptr_t num_rounds_;
ThreadBarrier* barrier_;
Random rng_;
};
UNIT_TEST_CASE(ThreadBarrier) {
static const intptr_t kNumTasks = 5;
static const intptr_t kNumRounds = 500;
ThreadBarrier barrier(kNumTasks + 1);
for (intptr_t i = 0; i < kNumTasks; ++i) {
Dart::thread_pool()->Run(new FuzzTask(kNumRounds, &barrier, i + 1));
}
for (intptr_t i = 0; i < kNumRounds; ++i) {
barrier.Sync();
}
barrier.Exit();
}
} // namespace dart