OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
7 | 7 |
8 #include "bin/process.h" | 8 #include "bin/process.h" |
9 | 9 |
10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 current = current->next(); | 98 current = current->next(); |
99 } | 99 } |
100 } | 100 } |
101 | 101 |
102 private: | 102 private: |
103 // Linked list of ProcessInfo objects for all active processes | 103 // Linked list of ProcessInfo objects for all active processes |
104 // started from Dart code. | 104 // started from Dart code. |
105 static ProcessInfo* active_processes_; | 105 static ProcessInfo* active_processes_; |
106 // Mutex protecting all accesses to the linked list of active | 106 // Mutex protecting all accesses to the linked list of active |
107 // processes. | 107 // processes. |
108 static dart::Mutex* mutex_; | 108 static Mutex* mutex_; |
109 }; | 109 }; |
110 | 110 |
111 | 111 |
112 ProcessInfo* ProcessInfoList::active_processes_ = NULL; | 112 ProcessInfo* ProcessInfoList::active_processes_ = NULL; |
113 dart::Mutex* ProcessInfoList::mutex_ = new dart::Mutex(); | 113 Mutex* ProcessInfoList::mutex_ = new Mutex(); |
114 | 114 |
115 | 115 |
116 // The exit code handler sets up a separate thread which waits for child | 116 // The exit code handler sets up a separate thread which waits for child |
117 // processes to terminate. That separate thread can then get the exit code from | 117 // processes to terminate. That separate thread can then get the exit code from |
118 // processes that have exited and communicate it to Dart through the | 118 // processes that have exited and communicate it to Dart through the |
119 // event loop. | 119 // event loop. |
120 class ExitCodeHandler { | 120 class ExitCodeHandler { |
121 public: | 121 public: |
122 // Notify the ExitCodeHandler that another process exists. | 122 // Notify the ExitCodeHandler that another process exists. |
123 static void ProcessStarted() { | 123 static void ProcessStarted() { |
124 // Multiple isolates could be starting processes at the same | 124 // Multiple isolates could be starting processes at the same |
125 // time. Make sure that only one ExitCodeHandler thread exists. | 125 // time. Make sure that only one ExitCodeHandler thread exists. |
126 MonitorLocker locker(monitor_); | 126 MonitorLocker locker(monitor_); |
127 process_count_++; | 127 process_count_++; |
128 | 128 |
129 monitor_->Notify(); | 129 monitor_->Notify(); |
130 | 130 |
131 if (running_) { | 131 if (running_) { |
132 return; | 132 return; |
133 } | 133 } |
134 | 134 |
135 // Start thread that handles process exits when wait returns. | 135 // Start thread that handles process exits when wait returns. |
136 int result = dart::Thread::Start(ExitCodeHandlerEntry, 0); | 136 int result = Thread::Start(ExitCodeHandlerEntry, 0); |
137 if (result != 0) { | 137 if (result != 0) { |
138 FATAL1("Failed to start exit code handler worker thread %d", result); | 138 FATAL1("Failed to start exit code handler worker thread %d", result); |
139 } | 139 } |
140 | 140 |
141 running_ = true; | 141 running_ = true; |
142 } | 142 } |
143 | 143 |
144 static void TerminateExitCodeThread() { | 144 static void TerminateExitCodeThread() { |
145 MonitorLocker locker(monitor_); | 145 MonitorLocker locker(monitor_); |
146 | 146 |
147 if (!running_) { | 147 if (!running_) { |
148 return; | 148 return; |
149 } | 149 } |
150 | 150 |
151 // Set terminate_done_ to false, so we can use it as a guard for our | 151 // Set terminate_done_ to false, so we can use it as a guard for our |
152 // monitor. | 152 // monitor. |
153 running_ = false; | 153 running_ = false; |
154 | 154 |
155 // Fork to wake up waitpid. | 155 // Fork to wake up waitpid. |
156 if (TEMP_FAILURE_RETRY(fork()) == 0) { | 156 if (TEMP_FAILURE_RETRY(fork()) == 0) { |
157 exit(0); | 157 exit(0); |
158 } | 158 } |
159 | 159 |
160 monitor_->Notify(); | 160 monitor_->Notify(); |
161 | 161 |
162 while (!terminate_done_) { | 162 while (!terminate_done_) { |
163 monitor_->Wait(dart::Monitor::kNoTimeout); | 163 monitor_->Wait(Monitor::kNoTimeout); |
164 } | 164 } |
165 } | 165 } |
166 | 166 |
167 private: | 167 private: |
168 // Entry point for the separate exit code handler thread started by | 168 // Entry point for the separate exit code handler thread started by |
169 // the ExitCodeHandler. | 169 // the ExitCodeHandler. |
170 static void ExitCodeHandlerEntry(uword param) { | 170 static void ExitCodeHandlerEntry(uword param) { |
171 pid_t pid = 0; | 171 pid_t pid = 0; |
172 int status = 0; | 172 int status = 0; |
173 while (true) { | 173 while (true) { |
174 { | 174 { |
175 MonitorLocker locker(monitor_); | 175 MonitorLocker locker(monitor_); |
176 while (running_ && process_count_ == 0) { | 176 while (running_ && process_count_ == 0) { |
177 monitor_->Wait(dart::Monitor::kNoTimeout); | 177 monitor_->Wait(Monitor::kNoTimeout); |
178 } | 178 } |
179 if (!running_) { | 179 if (!running_) { |
180 terminate_done_ = true; | 180 terminate_done_ = true; |
181 monitor_->Notify(); | 181 monitor_->Notify(); |
182 return; | 182 return; |
183 } | 183 } |
184 } | 184 } |
185 | 185 |
186 if ((pid = TEMP_FAILURE_RETRY(wait(&status))) > 0) { | 186 if ((pid = TEMP_FAILURE_RETRY(wait(&status))) > 0) { |
187 int exit_code = 0; | 187 int exit_code = 0; |
(...skipping 25 matching lines...) Expand all Loading... |
213 process_count_--; | 213 process_count_--; |
214 } | 214 } |
215 } | 215 } |
216 } | 216 } |
217 } | 217 } |
218 } | 218 } |
219 | 219 |
220 static bool terminate_done_; | 220 static bool terminate_done_; |
221 static int process_count_; | 221 static int process_count_; |
222 static bool running_; | 222 static bool running_; |
223 static dart::Monitor* monitor_; | 223 static Monitor* monitor_; |
224 }; | 224 }; |
225 | 225 |
226 | 226 |
227 bool ExitCodeHandler::running_ = false; | 227 bool ExitCodeHandler::running_ = false; |
228 int ExitCodeHandler::process_count_ = 0; | 228 int ExitCodeHandler::process_count_ = 0; |
229 bool ExitCodeHandler::terminate_done_ = false; | 229 bool ExitCodeHandler::terminate_done_ = false; |
230 dart::Monitor* ExitCodeHandler::monitor_ = new dart::Monitor(); | 230 Monitor* ExitCodeHandler::monitor_ = new Monitor(); |
231 | 231 |
232 | 232 |
233 static void SetChildOsErrorMessage(char** os_error_message) { | 233 static void SetChildOsErrorMessage(char** os_error_message) { |
234 const int kBufferSize = 1024; | 234 const int kBufferSize = 1024; |
235 char error_message[kBufferSize]; | 235 char error_message[kBufferSize]; |
236 strerror_r(errno, error_message, kBufferSize); | 236 strerror_r(errno, error_message, kBufferSize); |
237 *os_error_message = strdup(error_message); | 237 *os_error_message = strdup(error_message); |
238 } | 238 } |
239 | 239 |
240 | 240 |
(...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 bzero(&act, sizeof(act)); | 741 bzero(&act, sizeof(act)); |
742 act.sa_handler = SIG_DFL; | 742 act.sa_handler = SIG_DFL; |
743 VOID_NO_RETRY_EXPECTED(sigaction(signal, &act, NULL)); | 743 VOID_NO_RETRY_EXPECTED(sigaction(signal, &act, NULL)); |
744 } | 744 } |
745 } | 745 } |
746 | 746 |
747 } // namespace bin | 747 } // namespace bin |
748 } // namespace dart | 748 } // namespace dart |
749 | 749 |
750 #endif // defined(TARGET_OS_ANDROID) | 750 #endif // defined(TARGET_OS_ANDROID) |
OLD | NEW |