2017-01-30 19:27:00 +00:00
|
|
|
// Copyright 2017 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "src/compiler-dispatcher/optimizing-compile-dispatcher.h"
|
|
|
|
|
2019-05-17 12:13:44 +00:00
|
|
|
#include "src/api/api-inl.h"
|
2017-01-30 19:27:00 +00:00
|
|
|
#include "src/base/atomic-utils.h"
|
|
|
|
#include "src/base/platform/semaphore.h"
|
2019-05-21 09:30:15 +00:00
|
|
|
#include "src/codegen/compiler.h"
|
|
|
|
#include "src/codegen/optimized-compilation-info.h"
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/isolate.h"
|
2019-05-22 12:44:24 +00:00
|
|
|
#include "src/handles/handles.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2017-01-30 19:27:00 +00:00
|
|
|
#include "src/parsing/parse-info.h"
|
2017-04-18 14:01:12 +00:00
|
|
|
#include "test/unittests/test-helpers.h"
|
2017-01-30 19:27:00 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2017-11-13 12:04:57 +00:00
|
|
|
typedef TestWithNativeContext OptimizingCompileDispatcherTest;
|
2017-01-30 19:27:00 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-04-04 20:30:34 +00:00
|
|
|
class BlockingCompilationJob : public OptimizedCompilationJob {
|
2017-01-30 19:27:00 +00:00
|
|
|
public:
|
|
|
|
BlockingCompilationJob(Isolate* isolate, Handle<JSFunction> function)
|
2018-04-06 11:49:15 +00:00
|
|
|
: OptimizedCompilationJob(isolate->stack_guard()->real_climit(), &info_,
|
|
|
|
"BlockingCompilationJob",
|
2018-04-04 20:30:34 +00:00
|
|
|
State::kReadyToExecute),
|
2018-06-23 09:05:50 +00:00
|
|
|
shared_(function->shared(), isolate),
|
2018-04-06 11:49:15 +00:00
|
|
|
zone_(isolate->allocator(), ZONE_NAME),
|
2018-06-11 09:53:20 +00:00
|
|
|
info_(&zone_, isolate, shared_, function),
|
2017-01-30 19:27:00 +00:00
|
|
|
blocking_(false),
|
|
|
|
semaphore_(0) {}
|
|
|
|
~BlockingCompilationJob() override = default;
|
|
|
|
|
|
|
|
bool IsBlocking() const { return blocking_.Value(); }
|
|
|
|
void Signal() { semaphore_.Signal(); }
|
|
|
|
|
2018-04-04 20:30:34 +00:00
|
|
|
// OptimiziedCompilationJob implementation.
|
2017-11-15 14:36:57 +00:00
|
|
|
Status PrepareJobImpl(Isolate* isolate) override { UNREACHABLE(); }
|
2017-01-30 19:27:00 +00:00
|
|
|
|
|
|
|
Status ExecuteJobImpl() override {
|
|
|
|
blocking_.SetValue(true);
|
|
|
|
semaphore_.Wait();
|
|
|
|
blocking_.SetValue(false);
|
|
|
|
return SUCCEEDED;
|
|
|
|
}
|
|
|
|
|
2017-11-15 14:36:57 +00:00
|
|
|
Status FinalizeJobImpl(Isolate* isolate) override { return SUCCEEDED; }
|
2017-01-30 19:27:00 +00:00
|
|
|
|
|
|
|
private:
|
2017-07-21 09:32:32 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_;
|
2018-04-06 11:49:15 +00:00
|
|
|
Zone zone_;
|
2018-04-04 20:30:34 +00:00
|
|
|
OptimizedCompilationInfo info_;
|
2017-01-30 19:27:00 +00:00
|
|
|
base::AtomicValue<bool> blocking_;
|
|
|
|
base::Semaphore semaphore_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BlockingCompilationJob);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_F(OptimizingCompileDispatcherTest, Construct) {
|
|
|
|
OptimizingCompileDispatcher dispatcher(i_isolate());
|
|
|
|
ASSERT_TRUE(OptimizingCompileDispatcher::Enabled());
|
|
|
|
ASSERT_TRUE(dispatcher.IsQueueAvailable());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(OptimizingCompileDispatcherTest, NonBlockingFlush) {
|
2017-11-13 12:04:57 +00:00
|
|
|
Handle<JSFunction> fun =
|
|
|
|
RunJS<JSFunction>("function f() { function g() {}; return g;}; f();");
|
2018-11-30 13:53:39 +00:00
|
|
|
IsCompiledScope is_compiled_scope;
|
|
|
|
ASSERT_TRUE(
|
|
|
|
Compiler::Compile(fun, Compiler::CLEAR_EXCEPTION, &is_compiled_scope));
|
2017-01-30 19:27:00 +00:00
|
|
|
BlockingCompilationJob* job = new BlockingCompilationJob(i_isolate(), fun);
|
|
|
|
|
|
|
|
OptimizingCompileDispatcher dispatcher(i_isolate());
|
|
|
|
ASSERT_TRUE(OptimizingCompileDispatcher::Enabled());
|
|
|
|
ASSERT_TRUE(dispatcher.IsQueueAvailable());
|
|
|
|
dispatcher.QueueForOptimization(job);
|
|
|
|
|
|
|
|
// Busy-wait for the job to run on a background thread.
|
|
|
|
while (!job->IsBlocking()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not block.
|
2018-02-14 13:53:13 +00:00
|
|
|
dispatcher.Flush(BlockingBehavior::kDontBlock);
|
2017-01-30 19:27:00 +00:00
|
|
|
|
|
|
|
// Unblock the job & finish.
|
|
|
|
job->Signal();
|
|
|
|
dispatcher.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|