| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 intptr_t fd_; | 48 intptr_t fd_; |
| 49 ProcessInfo* next_; | 49 ProcessInfo* next_; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 | 52 |
| 53 // Singly-linked list of ProcessInfo objects for all active processes | 53 // Singly-linked list of ProcessInfo objects for all active processes |
| 54 // started from Dart. | 54 // started from Dart. |
| 55 class ProcessInfoList { | 55 class ProcessInfoList { |
| 56 public: | 56 public: |
| 57 static void AddProcess(pid_t pid, intptr_t fd) { | 57 static void AddProcess(pid_t pid, intptr_t fd) { |
| 58 MutexLocker locker(&mutex_); | 58 MutexLocker locker(mutex_); |
| 59 ProcessInfo* info = new ProcessInfo(pid, fd); | 59 ProcessInfo* info = new ProcessInfo(pid, fd); |
| 60 info->set_next(active_processes_); | 60 info->set_next(active_processes_); |
| 61 active_processes_ = info; | 61 active_processes_ = info; |
| 62 } | 62 } |
| 63 | 63 |
| 64 | 64 |
| 65 static intptr_t LookupProcessExitFd(pid_t pid) { | 65 static intptr_t LookupProcessExitFd(pid_t pid) { |
| 66 MutexLocker locker(&mutex_); | 66 MutexLocker locker(mutex_); |
| 67 ProcessInfo* current = active_processes_; | 67 ProcessInfo* current = active_processes_; |
| 68 while (current != NULL) { | 68 while (current != NULL) { |
| 69 if (current->pid() == pid) { | 69 if (current->pid() == pid) { |
| 70 return current->fd(); | 70 return current->fd(); |
| 71 } | 71 } |
| 72 current = current->next(); | 72 current = current->next(); |
| 73 } | 73 } |
| 74 return 0; | 74 return 0; |
| 75 } | 75 } |
| 76 | 76 |
| 77 | 77 |
| 78 static void RemoveProcess(pid_t pid) { | 78 static void RemoveProcess(pid_t pid) { |
| 79 MutexLocker locker(&mutex_); | 79 MutexLocker locker(mutex_); |
| 80 ProcessInfo* prev = NULL; | 80 ProcessInfo* prev = NULL; |
| 81 ProcessInfo* current = active_processes_; | 81 ProcessInfo* current = active_processes_; |
| 82 while (current != NULL) { | 82 while (current != NULL) { |
| 83 if (current->pid() == pid) { | 83 if (current->pid() == pid) { |
| 84 if (prev == NULL) { | 84 if (prev == NULL) { |
| 85 active_processes_ = current->next(); | 85 active_processes_ = current->next(); |
| 86 } else { | 86 } else { |
| 87 prev->set_next(current->next()); | 87 prev->set_next(current->next()); |
| 88 } | 88 } |
| 89 delete current; | 89 delete current; |
| 90 return; | 90 return; |
| 91 } | 91 } |
| 92 prev = current; | 92 prev = current; |
| 93 current = current->next(); | 93 current = current->next(); |
| 94 } | 94 } |
| 95 } | 95 } |
| 96 | 96 |
| 97 private: | 97 private: |
| 98 // Linked list of ProcessInfo objects for all active processes | 98 // Linked list of ProcessInfo objects for all active processes |
| 99 // started from Dart code. | 99 // started from Dart code. |
| 100 static ProcessInfo* active_processes_; | 100 static ProcessInfo* active_processes_; |
| 101 // Mutex protecting all accesses to the linked list of active | 101 // Mutex protecting all accesses to the linked list of active |
| 102 // processes. | 102 // processes. |
| 103 static dart::Mutex mutex_; | 103 static dart::Mutex* mutex_; |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 | 106 |
| 107 ProcessInfo* ProcessInfoList::active_processes_ = NULL; | 107 ProcessInfo* ProcessInfoList::active_processes_ = NULL; |
| 108 dart::Mutex ProcessInfoList::mutex_; | 108 dart::Mutex* ProcessInfoList::mutex_ = new dart::Mutex(); |
| 109 | 109 |
| 110 | 110 |
| 111 // The exit code handler sets up a separate thread which is signalled | 111 // The exit code handler sets up a separate thread which is signalled |
| 112 // on SIGCHLD. That separate thread can then get the exit code from | 112 // on SIGCHLD. That separate thread can then get the exit code from |
| 113 // processes that have exited and communicate it to Dart through the | 113 // processes that have exited and communicate it to Dart through the |
| 114 // event loop. | 114 // event loop. |
| 115 class ExitCodeHandler { | 115 class ExitCodeHandler { |
| 116 public: | 116 public: |
| 117 // Ensure that the ExitCodeHandler has been initialized. | 117 // Ensure that the ExitCodeHandler has been initialized. |
| 118 static bool EnsureInitialized() { | 118 static bool EnsureInitialized() { |
| 119 // Multiple isolates could be starting processes at the same | 119 // Multiple isolates could be starting processes at the same |
| 120 // time. Make sure that only one of them initializes the | 120 // time. Make sure that only one of them initializes the |
| 121 // ExitCodeHandler. | 121 // ExitCodeHandler. |
| 122 MutexLocker locker(&mutex_); | 122 MutexLocker locker(mutex_); |
| 123 if (initialized_) { | 123 if (initialized_) { |
| 124 return true; | 124 return true; |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Allocate a pipe that the signal handler can write a byte to and | 127 // Allocate a pipe that the signal handler can write a byte to and |
| 128 // that the exit handler thread can poll. | 128 // that the exit handler thread can poll. |
| 129 int result = TEMP_FAILURE_RETRY(pipe(sig_chld_fds_)); | 129 int result = TEMP_FAILURE_RETRY(pipe(sig_chld_fds_)); |
| 130 if (result < 0) { | 130 if (result < 0) { |
| 131 return false; | 131 return false; |
| 132 } | 132 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 148 return true; | 148 return true; |
| 149 } | 149 } |
| 150 | 150 |
| 151 // Get the write end of the pipe. | 151 // Get the write end of the pipe. |
| 152 static int WakeUpFd() { | 152 static int WakeUpFd() { |
| 153 ASSERT(initialized_); | 153 ASSERT(initialized_); |
| 154 return sig_chld_fds_[1]; | 154 return sig_chld_fds_[1]; |
| 155 } | 155 } |
| 156 | 156 |
| 157 static void TerminateExitCodeThread() { | 157 static void TerminateExitCodeThread() { |
| 158 MutexLocker locker(&mutex_); | 158 MutexLocker locker(mutex_); |
| 159 if (!initialized_) { | 159 if (!initialized_) { |
| 160 return; | 160 return; |
| 161 } | 161 } |
| 162 | 162 |
| 163 uint8_t data = kThreadTerminateByte; | 163 uint8_t data = kThreadTerminateByte; |
| 164 ssize_t result = | 164 ssize_t result = |
| 165 TEMP_FAILURE_RETRY(write(ExitCodeHandler::WakeUpFd(), &data, 1)); | 165 TEMP_FAILURE_RETRY(write(ExitCodeHandler::WakeUpFd(), &data, 1)); |
| 166 if (result < 1) { | 166 if (result < 1) { |
| 167 perror("Failed to write to wake-up fd to terminate exit code thread"); | 167 perror("Failed to write to wake-up fd to terminate exit code thread"); |
| 168 } | 168 } |
| 169 | 169 |
| 170 { | 170 { |
| 171 MonitorLocker terminate_locker(&thread_terminate_monitor_); | 171 MonitorLocker terminate_locker(thread_terminate_monitor_); |
| 172 while (!thread_terminated_) { | 172 while (!thread_terminated_) { |
| 173 terminate_locker.Wait(); | 173 terminate_locker.Wait(); |
| 174 } | 174 } |
| 175 } | 175 } |
| 176 } | 176 } |
| 177 | 177 |
| 178 static void ExitCodeThreadTerminated() { | 178 static void ExitCodeThreadTerminated() { |
| 179 MonitorLocker locker(&thread_terminate_monitor_); | 179 MonitorLocker locker(thread_terminate_monitor_); |
| 180 thread_terminated_ = true; | 180 thread_terminated_ = true; |
| 181 locker.Notify(); | 181 locker.Notify(); |
| 182 } | 182 } |
| 183 | 183 |
| 184 private: | 184 private: |
| 185 static const uint8_t kThreadTerminateByte = 1; | 185 static const uint8_t kThreadTerminateByte = 1; |
| 186 | 186 |
| 187 // GetProcessExitCodes is called on a separate thread when a SIGCHLD | 187 // GetProcessExitCodes is called on a separate thread when a SIGCHLD |
| 188 // signal is received to retrieve the exit codes and post them to | 188 // signal is received to retrieve the exit codes and post them to |
| 189 // dart. | 189 // dart. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 if (data == ExitCodeHandler::kThreadTerminateByte) { | 244 if (data == ExitCodeHandler::kThreadTerminateByte) { |
| 245 ExitCodeThreadTerminated(); | 245 ExitCodeThreadTerminated(); |
| 246 return; | 246 return; |
| 247 } | 247 } |
| 248 // Get the exit code from all processes that have died. | 248 // Get the exit code from all processes that have died. |
| 249 GetProcessExitCodes(); | 249 GetProcessExitCodes(); |
| 250 } | 250 } |
| 251 } | 251 } |
| 252 } | 252 } |
| 253 | 253 |
| 254 static dart::Mutex mutex_; | 254 static dart::Mutex* mutex_; |
| 255 static bool initialized_; | 255 static bool initialized_; |
| 256 static int sig_chld_fds_[2]; | 256 static int sig_chld_fds_[2]; |
| 257 static bool thread_terminated_; | 257 static bool thread_terminated_; |
| 258 static dart::Monitor thread_terminate_monitor_; | 258 static dart::Monitor* thread_terminate_monitor_; |
| 259 }; | 259 }; |
| 260 | 260 |
| 261 | 261 |
| 262 dart::Mutex ExitCodeHandler::mutex_; | 262 dart::Mutex* ExitCodeHandler::mutex_ = new dart::Mutex(); |
| 263 bool ExitCodeHandler::initialized_ = false; | 263 bool ExitCodeHandler::initialized_ = false; |
| 264 int ExitCodeHandler::sig_chld_fds_[2] = { 0, 0 }; | 264 int ExitCodeHandler::sig_chld_fds_[2] = { 0, 0 }; |
| 265 bool ExitCodeHandler::thread_terminated_ = false; | 265 bool ExitCodeHandler::thread_terminated_ = false; |
| 266 dart::Monitor ExitCodeHandler::thread_terminate_monitor_; | 266 dart::Monitor* ExitCodeHandler::thread_terminate_monitor_ = new dart::Monitor(); |
| 267 | 267 |
| 268 | 268 |
| 269 static void SetChildOsErrorMessage(char** os_error_message) { | 269 static void SetChildOsErrorMessage(char** os_error_message) { |
| 270 *os_error_message = strdup(strerror(errno)); | 270 *os_error_message = strdup(strerror(errno)); |
| 271 } | 271 } |
| 272 | 272 |
| 273 | 273 |
| 274 static void SigChldHandler(int process_signal, siginfo_t* siginfo, void* tmp) { | 274 static void SigChldHandler(int process_signal, siginfo_t* siginfo, void* tmp) { |
| 275 // Save errno so it can be restored at the end. | 275 // Save errno so it can be restored at the end. |
| 276 int entry_errno = errno; | 276 int entry_errno = errno; |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 | 578 |
| 579 | 579 |
| 580 intptr_t Process::CurrentProcessId() { | 580 intptr_t Process::CurrentProcessId() { |
| 581 return static_cast<intptr_t>(getpid()); | 581 return static_cast<intptr_t>(getpid()); |
| 582 } | 582 } |
| 583 | 583 |
| 584 } // namespace bin | 584 } // namespace bin |
| 585 } // namespace dart | 585 } // namespace dart |
| 586 | 586 |
| 587 #endif // defined(TARGET_OS_ANDROID) | 587 #endif // defined(TARGET_OS_ANDROID) |
| OLD | NEW |