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