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