Skip to content

Commit 0ae8697

Browse files
mkustermanncommit-bot@chromium.org
authored andcommitted
[vm] Avoid allocating Monitor/Mutex/... with global initializer, use Init()/Cleanup() functions instead
This CL adds a `dart::embedder::Cleanup()` (we already have `dart::embedder::InitOnce()`). This allows us to allocate the global state and also tear it down. As a side-effect this will also not allocate those variables if not needed, which should fix b/151210948 Change-Id: I3c5c619586380bf27ee863ba026bbc631f243d85 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139640 Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Vyacheslav Egorov <[email protected]>
1 parent db5dc11 commit 0ae8697

13 files changed

+339
-28
lines changed

runtime/bin/dart_embedder_api_impl.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "bin/dartutils.h"
88
#include "bin/eventhandler.h"
99
#include "bin/isolate_data.h"
10+
#include "bin/process.h"
11+
#include "bin/secure_socket_filter.h"
1012
#include "bin/thread.h"
1113
#include "bin/utils.h"
1214
#include "bin/vmservice_impl.h"
@@ -37,10 +39,24 @@ bool InitOnce(char** error) {
3739
return false;
3840
}
3941
bin::TimerUtils::InitOnce();
42+
bin::Process::Init();
43+
#if !defined(DART_IO_SECURE_SOCKET_DISABLED)
44+
bin::SSLFilter::Init();
45+
#endif
4046
bin::EventHandler::Start();
4147
return true;
4248
}
4349

50+
void Cleanup() {
51+
bin::Process::ClearAllSignalHandlers();
52+
53+
bin::EventHandler::Stop();
54+
#if !defined(DART_IO_SECURE_SOCKET_DISABLED)
55+
bin::SSLFilter::Cleanup();
56+
#endif
57+
bin::Process::Cleanup();
58+
}
59+
4460
Dart_Isolate CreateKernelServiceIsolate(const IsolateCreationData& data,
4561
const uint8_t* buffer,
4662
intptr_t buffer_size,

runtime/bin/dart_io_api_impl.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "bin/io_natives.h"
1111
#include "bin/platform.h"
1212
#include "bin/process.h"
13+
#include "bin/secure_socket_filter.h"
1314
#include "bin/thread.h"
1415
#include "bin/utils.h"
1516

@@ -19,11 +20,19 @@ namespace bin {
1920
void BootstrapDartIo() {
2021
// Bootstrap 'dart:io' event handler.
2122
TimerUtils::InitOnce();
23+
Process::Init();
24+
#if !defined(DART_IO_SECURE_SOCKET_DISABLED)
25+
SSLFilter::Init();
26+
#endif
2227
EventHandler::Start();
2328
}
2429

2530
void CleanupDartIo() {
2631
EventHandler::Stop();
32+
#if !defined(DART_IO_SECURE_SOCKET_DISABLED)
33+
SSLFilter::Cleanup();
34+
#endif
35+
Process::Cleanup();
2736
}
2837

2938
void SetSystemTempDirectory(const char* system_temp) {

runtime/bin/main.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -860,8 +860,7 @@ bool RunMainIsolate(const char* script_name, CommandLineOptions* dart_options) {
860860
Syslog::PrintErr("VM cleanup failed: %s\n", error);
861861
free(error);
862862
}
863-
Process::ClearAllSignalHandlers();
864-
EventHandler::Stop();
863+
dart::embedder::Cleanup();
865864
Platform::Exit((exit_code != 0) ? exit_code : kErrorExitCode);
866865
}
867866
main_isolate = isolate;
@@ -1190,7 +1189,7 @@ void main(int argc, char** argv) {
11901189

11911190
error = Dart_Initialize(&init_params);
11921191
if (error != NULL) {
1193-
EventHandler::Stop();
1192+
dart::embedder::Cleanup();
11941193
Syslog::PrintErr("VM initialization failed: %s\n", error);
11951194
free(error);
11961195
Platform::Exit(kErrorExitCode);
@@ -1214,8 +1213,8 @@ void main(int argc, char** argv) {
12141213
Syslog::PrintErr("VM cleanup failed: %s\n", error);
12151214
free(error);
12161215
}
1217-
Process::ClearAllSignalHandlers();
1218-
EventHandler::Stop();
1216+
const intptr_t global_exit_code = Process::GlobalExitCode();
1217+
dart::embedder::Cleanup();
12191218

12201219
delete app_snapshot;
12211220
free(app_script_uri);
@@ -1230,7 +1229,7 @@ void main(int argc, char** argv) {
12301229
// Free environment if any.
12311230
Options::DestroyEnvironment();
12321231

1233-
Platform::Exit(Process::GlobalExitCode());
1232+
Platform::Exit(global_exit_code);
12341233
}
12351234

12361235
} // namespace bin

runtime/bin/process.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ enum ProcessStartMode {
8787

8888
class Process {
8989
public:
90+
static void Init();
91+
static void Cleanup();
92+
9093
// Start a new process providing access to stdin, stdout, stderr and
9194
// process exit streams.
9295
static int Start(Namespace* namespc,

runtime/bin/process_android.cc

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace dart {
3535
namespace bin {
3636

3737
int Process::global_exit_code_ = 0;
38-
Mutex* Process::global_exit_code_mutex_ = new Mutex();
38+
Mutex* Process::global_exit_code_mutex_ = nullptr;
3939
Process::ExitHook Process::exit_hook_ = NULL;
4040

4141
// ProcessInfo is used to map a process id to the file descriptor for
@@ -68,6 +68,9 @@ class ProcessInfo {
6868
// started from Dart.
6969
class ProcessInfoList {
7070
public:
71+
static void Init();
72+
static void Cleanup();
73+
7174
static void AddProcess(pid_t pid, intptr_t fd) {
7275
MutexLocker locker(mutex_);
7376
ProcessInfo* info = new ProcessInfo(pid, fd);
@@ -119,14 +122,17 @@ class ProcessInfoList {
119122
};
120123

121124
ProcessInfo* ProcessInfoList::active_processes_ = NULL;
122-
Mutex* ProcessInfoList::mutex_ = new Mutex();
125+
Mutex* ProcessInfoList::mutex_ = nullptr;
123126

124127
// The exit code handler sets up a separate thread which waits for child
125128
// processes to terminate. That separate thread can then get the exit code from
126129
// processes that have exited and communicate it to Dart through the
127130
// event loop.
128131
class ExitCodeHandler {
129132
public:
133+
static void Init();
134+
static void Cleanup();
135+
130136
// Notify the ExitCodeHandler that another process exists.
131137
static void ProcessStarted() {
132138
// Multiple isolates could be starting processes at the same
@@ -241,7 +247,7 @@ class ExitCodeHandler {
241247
bool ExitCodeHandler::running_ = false;
242248
int ExitCodeHandler::process_count_ = 0;
243249
bool ExitCodeHandler::terminate_done_ = false;
244-
Monitor* ExitCodeHandler::monitor_ = new Monitor();
250+
Monitor* ExitCodeHandler::monitor_ = nullptr;
245251

246252
class ProcessStarter {
247253
public:
@@ -953,7 +959,7 @@ int64_t Process::MaxRSS() {
953959
return usage.ru_maxrss * KB;
954960
}
955961

956-
static Mutex* signal_mutex = new Mutex();
962+
static Mutex* signal_mutex = nullptr;
957963
static SignalInfo* signal_handlers = NULL;
958964
static const int kSignalsCount = 7;
959965
static const int kSignals[kSignalsCount] = {
@@ -1057,6 +1063,54 @@ void Process::ClearSignalHandler(intptr_t signal, Dart_Port port) {
10571063
}
10581064
}
10591065

1066+
void ProcessInfoList::Init() {
1067+
ASSERT(ProcessInfoList::mutex_ == nullptr);
1068+
ProcessInfoList::mutex_ = new Mutex();
1069+
}
1070+
1071+
void ProcessInfoList::Cleanup() {
1072+
ASSERT(ProcessInfoList::mutex_ != nullptr);
1073+
delete ProcessInfoList::mutex_;
1074+
ProcessInfoList::mutex_ = nullptr;
1075+
}
1076+
1077+
void ExitCodeHandler::Init() {
1078+
ASSERT(ExitCodeHandler::monitor_ == nullptr);
1079+
ExitCodeHandler::monitor_ = new Monitor();
1080+
}
1081+
1082+
void ExitCodeHandler::Cleanup() {
1083+
ASSERT(ExitCodeHandler::monitor_ != nullptr);
1084+
delete ExitCodeHandler::monitor_;
1085+
ExitCodeHandler::monitor_ = nullptr;
1086+
}
1087+
1088+
void Process::Init() {
1089+
ExitCodeHandler::Init();
1090+
ProcessInfoList::Init();
1091+
1092+
ASSERT(signal_mutex == nullptr);
1093+
signal_mutex = new Mutex();
1094+
1095+
ASSERT(Process::global_exit_code_mutex_ == nullptr);
1096+
Process::global_exit_code_mutex_ = new Mutex();
1097+
}
1098+
1099+
void Process::Cleanup() {
1100+
ClearAllSignalHandlers();
1101+
1102+
ASSERT(signal_mutex != nullptr);
1103+
delete signal_mutex;
1104+
signal_mutex = nullptr;
1105+
1106+
ASSERT(Process::global_exit_code_mutex_ != nullptr);
1107+
delete Process::global_exit_code_mutex_;
1108+
Process::global_exit_code_mutex_ = nullptr;
1109+
1110+
ProcessInfoList::Cleanup();
1111+
ExitCodeHandler::Cleanup();
1112+
}
1113+
10601114
} // namespace bin
10611115
} // namespace dart
10621116

runtime/bin/process_fuchsia.cc

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace dart {
5050
namespace bin {
5151

5252
int Process::global_exit_code_ = 0;
53-
Mutex* Process::global_exit_code_mutex_ = new Mutex();
53+
Mutex* Process::global_exit_code_mutex_ = nullptr;
5454
Process::ExitHook Process::exit_hook_ = NULL;
5555

5656
// ProcessInfo is used to map a process id to the file descriptor for
@@ -140,7 +140,7 @@ class ProcessInfoList {
140140
};
141141

142142
ProcessInfo* ProcessInfoList::active_processes_ = NULL;
143-
Mutex* ProcessInfoList::mutex_ = new Mutex();
143+
Mutex* ProcessInfoList::mutex_ = nullptr;
144144

145145
// The exit code handler sets up a separate thread which waits for child
146146
// processes to terminate. That separate thread can then get the exit code from
@@ -297,7 +297,7 @@ class ExitCodeHandler {
297297
zx_handle_t ExitCodeHandler::port_ = ZX_HANDLE_INVALID;
298298
bool ExitCodeHandler::running_ = false;
299299
bool ExitCodeHandler::terminate_done_ = false;
300-
Monitor* ExitCodeHandler::monitor_ = new Monitor();
300+
Monitor* ExitCodeHandler::monitor_ = nullptr;
301301

302302
void Process::TerminateExitCodeHandler() {
303303
ExitCodeHandler::Terminate();
@@ -812,6 +812,54 @@ intptr_t Process::SetSignalHandler(intptr_t signal) {
812812

813813
void Process::ClearSignalHandler(intptr_t signal, Dart_Port port) {}
814814

815+
void ProcessInfoList::Init() {
816+
ASSERT(ProcessInfoList::mutex_ == nullptr);
817+
ProcessInfoList::mutex_ = new Mutex();
818+
}
819+
820+
void ProcessInfoList::Cleanup() {
821+
ASSERT(ProcessInfoList::mutex_ != nullptr);
822+
delete ProcessInfoList::mutex_;
823+
ProcessInfoList::mutex_ = nullptr;
824+
}
825+
826+
void ExitCodeHandler::Init() {
827+
ASSERT(ExitCodeHandler::monitor_ == nullptr);
828+
ExitCodeHandler::monitor_ = new Monitor();
829+
}
830+
831+
void ExitCodeHandler::Cleanup() {
832+
ASSERT(ExitCodeHandler::monitor_ != nullptr);
833+
delete ExitCodeHandler::monitor_;
834+
ExitCodeHandler::monitor_ = nullptr;
835+
}
836+
837+
void Process::Init() {
838+
ExitCodeHandler::Init();
839+
ProcessInfoList::Init();
840+
841+
ASSERT(signal_mutex == nullptr);
842+
signal_mutex = new Mutex();
843+
844+
ASSERT(Process::global_exit_code_mutex_ == nullptr);
845+
Process::global_exit_code_mutex_ = new Mutex();
846+
}
847+
848+
void Process::Cleanup() {
849+
ClearAllSignalHandlers();
850+
851+
ASSERT(signal_mutex != nullptr);
852+
delete signal_mutex;
853+
signal_mutex = nullptr;
854+
855+
ASSERT(Process::global_exit_code_mutex_ != nullptr);
856+
delete Process::global_exit_code_mutex_;
857+
Process::global_exit_code_mutex_ = nullptr;
858+
859+
ProcessInfoList::Cleanup();
860+
ExitCodeHandler::Cleanup();
861+
}
862+
815863
} // namespace bin
816864
} // namespace dart
817865

0 commit comments

Comments
 (0)