e2efffa005
This reland commit5a32d8bc7cwith a fix for thread leak (Issue #56717): when `ThreadPool` is shutting down asynchronously the last worker should detach itself to prevent leaking associated low-level data structures, because no thread will join it. A hang in service isolate shutdown (caused by an existing bug) was fixed by commit157a0dc7f9. This CL turns native ports into a thin abstraction over underlying thread pool instead of building them as full fledged MessageHandler. This allows to easily implement a variation of native ports which can handle messages concurrently with the given degree of concurrency. This type of port can be used to greatly simplify implementation of IOService - which previously had to do its own concurrency management on top of "single threaded" native ports. This capability is exposed as `Dart_NewConcurrentNativePort` API. The new implementation is in general much cleaner then the old one with one exception: `Dart_CloseNativePort` API has unfortunate design where underlying message handler is destroyed asynchronously and `Dart_CloseNativePort` returns immediately without waiting for pending tasks to complete. Implementing this on top of `ThreadPool` requires some changes to thread pool implementation. Issue https://github.com/dart-lang/sdk/issues/55844 Closes https://github.com/dart-lang/sdk/issues/56717 TEST=ci Change-Id: Ic68bfb60757685afd75c80a70cdec66cc13c149b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/385000 Reviewed-by: Martin Kustermann <kustermann@google.com> Commit-Queue: Slava Egorov <vegorov@google.com>
72 lines
1.9 KiB
C++
72 lines
1.9 KiB
C++
// Copyright (c) 2012, 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/native_message_handler.h"
|
|
|
|
#include <memory>
|
|
#include <utility>
|
|
|
|
#include "vm/dart_api_message.h"
|
|
#include "vm/isolate.h"
|
|
#include "vm/message.h"
|
|
#include "vm/message_snapshot.h"
|
|
#include "vm/snapshot.h"
|
|
|
|
namespace dart {
|
|
|
|
NativeMessageHandler::NativeMessageHandler(const char* name,
|
|
Dart_NativeMessageHandler func,
|
|
intptr_t max_concurrency)
|
|
: name_(Utils::StrDup(name)), func_(func), pool_(max_concurrency) {}
|
|
|
|
NativeMessageHandler::~NativeMessageHandler() {}
|
|
|
|
#if defined(DEBUG)
|
|
void NativeMessageHandler::CheckAccess() const {
|
|
ASSERT(Isolate::Current() == nullptr);
|
|
}
|
|
#endif
|
|
|
|
namespace {
|
|
class HandleMessage : public ThreadPool::Task {
|
|
public:
|
|
HandleMessage(Dart_NativeMessageHandler handler,
|
|
std::unique_ptr<Message> message)
|
|
: handler_(handler), message_(std::move(message)) {
|
|
ASSERT(handler != nullptr);
|
|
}
|
|
|
|
virtual void Run() {
|
|
ApiNativeScope scope;
|
|
Dart_CObject* object = ReadApiMessage(scope.zone(), message_.get());
|
|
handler_(message_->dest_port(), object);
|
|
}
|
|
|
|
private:
|
|
Dart_NativeMessageHandler handler_;
|
|
std::unique_ptr<Message> message_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(HandleMessage);
|
|
};
|
|
} // namespace
|
|
|
|
void NativeMessageHandler::PostMessage(std::unique_ptr<Message> message,
|
|
bool before_events /* = false */) {
|
|
if (message->IsOOB()) {
|
|
UNREACHABLE();
|
|
}
|
|
|
|
pool_.Run<HandleMessage>(func_, std::move(message));
|
|
}
|
|
|
|
void NativeMessageHandler::RequestDeletion(NativeMessageHandler* handler) {
|
|
ThreadPool::RequestShutdown(&handler->pool_, [handler]() { delete handler; });
|
|
}
|
|
|
|
void NativeMessageHandler::Shutdown() {
|
|
pool_.Shutdown();
|
|
}
|
|
|
|
} // namespace dart
|