[dart:io] Update dart::bin::CleanupDartIo and no-SSL mode to support recent changes to IOService
https://dart-review.googlesource.com/c/sdk/+/437160 changed how the IOService port is created.
The Flutter engine calls dart::bin::CleanupDartIo during shutdown of the Dart VM. That API now needs to clean up the IOService.
This PR also adds support for the IOService port change when building Dart with secure sockets disabled.
TEST=building the Flutter engine
Change-Id: I2d61713cfbe3feddb7d361605c25229d05dd61a3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/438605
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
Commit-Queue: Jason Simmons <jsimmons@google.com>
diff --git a/runtime/bin/dart_embedder_api_impl.cc b/runtime/bin/dart_embedder_api_impl.cc
index c280fc8..ce1b89a 100644
--- a/runtime/bin/dart_embedder_api_impl.cc
+++ b/runtime/bin/dart_embedder_api_impl.cc
@@ -6,7 +6,11 @@
#include "bin/dartutils.h"
#include "bin/eventhandler.h"
+#if defined(DART_IO_SECURE_SOCKET_DISABLED)
+#include "bin/io_service_no_ssl.h"
+#else // defined(DART_IO_SECURE_SOCKET_DISABLED)
#include "bin/io_service.h"
+#endif // defined(DART_IO_SECURE_SOCKET_DISABLED)
#include "bin/isolate_data.h"
#include "bin/process.h"
#include "bin/secure_socket_filter.h"
diff --git a/runtime/bin/dart_io_api_impl.cc b/runtime/bin/dart_io_api_impl.cc
index 369bf2d..d6acd6f 100644
--- a/runtime/bin/dart_io_api_impl.cc
+++ b/runtime/bin/dart_io_api_impl.cc
@@ -8,6 +8,11 @@
#include "bin/directory.h"
#include "bin/eventhandler.h"
#include "bin/io_natives.h"
+#if defined(DART_IO_SECURE_SOCKET_DISABLED)
+#include "bin/io_service_no_ssl.h"
+#else // defined(DART_IO_SECURE_SOCKET_DISABLED)
+#include "bin/io_service.h"
+#endif // defined(DART_IO_SECURE_SOCKET_DISABLED)
#include "bin/platform.h"
#include "bin/process.h"
#if !defined(DART_IO_SECURE_SOCKET_DISABLED)
@@ -36,6 +41,7 @@
SSLFilter::Cleanup();
#endif
Process::Cleanup();
+ IOService::Cleanup();
}
void SetSystemTempDirectory(const char* system_temp) {
diff --git a/runtime/bin/io_service_no_ssl.cc b/runtime/bin/io_service_no_ssl.cc
index 406ad89..d456cdf6 100644
--- a/runtime/bin/io_service_no_ssl.cc
+++ b/runtime/bin/io_service_no_ssl.cc
@@ -53,10 +53,22 @@
}
intptr_t IOService::max_concurrency_ = 32;
+std::atomic<Dart_Port> IOService::port_ = ILLEGAL_PORT;
Dart_Port IOService::GetServicePort() {
- return Dart_NewConcurrentNativePort("IOService", IOServiceCallback,
- max_concurrency_);
+ Dart_Port port = port_;
+ if (port == ILLEGAL_PORT) {
+ port = Dart_NewConcurrentNativePort("IOService", IOServiceCallback,
+ max_concurrency_);
+ Dart_Port expected = ILLEGAL_PORT;
+ if (!port_.compare_exchange_strong(expected, port)) {
+ // Lost the initialization race. Use the winner's port and close our port.
+ // The winner's port is eventually implicitly closed by VM shutdown.
+ Dart_CloseNativePort(port);
+ return expected;
+ }
+ }
+ return port;
}
void FUNCTION_NAME(IOService_NewServicePort)(Dart_NativeArguments args) {
diff --git a/runtime/bin/io_service_no_ssl.h b/runtime/bin/io_service_no_ssl.h
index 9ab2762..db22cfd 100644
--- a/runtime/bin/io_service_no_ssl.h
+++ b/runtime/bin/io_service_no_ssl.h
@@ -9,6 +9,8 @@
#error "io_service_no_ssl.h can only be included on builds with IO enabled"
#endif
+#include <atomic>
+
#include "bin/builtin.h"
#include "bin/utils.h"
@@ -70,12 +72,14 @@
enum { IO_SERVICE_REQUEST_LIST(DECLARE_REQUEST) };
static Dart_Port GetServicePort();
+ static void Cleanup() { port_ = ILLEGAL_PORT; }
static intptr_t max_concurrency() { return max_concurrency_; }
static void set_max_concurrency(intptr_t value) { max_concurrency_ = value; }
private:
static intptr_t max_concurrency_;
+ static std::atomic<Dart_Port> port_;
DISALLOW_ALLOCATION();
DISALLOW_IMPLICIT_CONSTRUCTORS(IOService);