mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 00:13:50 +00:00
![Ryan Macnak](/assets/img/avatar_default.png)
This makes the ParallelScavenge and ParallelMark tasks safe to be run by an embedder-provided task runner that doesn't guarantee immediate execution. TEST=ci Change-Id: I485c1873a5eb7a208a9cc4fb32aa770c0ad6f773 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/214870 Reviewed-by: Liam Appelbe <liama@google.com> Commit-Queue: Ryan Macnak <rmacnak@google.com>
54 lines
1.3 KiB
C++
54 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 "vm/thread_barrier.h"
|
|
#include "platform/assert.h"
|
|
#include "vm/random.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_->Release();
|
|
}
|
|
|
|
private:
|
|
void RandomSleep() {
|
|
int64_t ms = rng_.NextUInt32() % 4;
|
|
if (ms > 0) {
|
|
OS::Sleep(ms);
|
|
}
|
|
}
|
|
|
|
const intptr_t num_rounds_;
|
|
ThreadBarrier* barrier_;
|
|
Random rng_;
|
|
};
|
|
|
|
VM_UNIT_TEST_CASE(ThreadBarrier) {
|
|
static const intptr_t kNumTasks = 5;
|
|
static const intptr_t kNumRounds = 500;
|
|
|
|
ThreadBarrier* barrier = new ThreadBarrier(kNumTasks + 1, kNumTasks + 1);
|
|
for (intptr_t i = 0; i < kNumTasks; ++i) {
|
|
Dart::thread_pool()->Run<FuzzTask>(kNumRounds, barrier, i + 1);
|
|
}
|
|
for (intptr_t i = 0; i < kNumRounds; ++i) {
|
|
barrier->Sync();
|
|
}
|
|
barrier->Release();
|
|
}
|
|
|
|
} // namespace dart
|