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