Chromium Code Reviews| 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 <process.h> | 5 #include <process.h> |
| 6 | 6 |
| 7 #include "bin/builtin.h" | 7 #include "bin/builtin.h" |
| 8 #include "bin/process.h" | 8 #include "bin/process.h" |
| 9 #include "bin/eventhandler.h" | 9 #include "bin/eventhandler.h" |
| 10 #include "bin/thread.h" | 10 #include "bin/thread.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 // Link to next ProcessInfo object in the singly-linked list. | 61 // Link to next ProcessInfo object in the singly-linked list. |
| 62 ProcessInfo* next_; | 62 ProcessInfo* next_; |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 | 65 |
| 66 // Singly-linked list of ProcessInfo objects for all active processes | 66 // Singly-linked list of ProcessInfo objects for all active processes |
| 67 // started from Dart. | 67 // started from Dart. |
| 68 class ProcessInfoList { | 68 class ProcessInfoList { |
| 69 public: | 69 public: |
| 70 static void AddProcess(DWORD pid, HANDLE handle, HANDLE pipe) { | 70 static void AddProcess(DWORD pid, HANDLE handle, HANDLE pipe) { |
| 71 // Create a wait operation for the process handle to extract | 71 // Register a callback to extract the exit code, when the process |
| 72 // the exit code. | 72 // is signaled. The callback runs in a independent thread from the OS pool. |
| 73 // Because the callback depends on the process list containing | |
| 74 // the process, lock the mutex until the process is added to the list. | |
| 75 MutexLocker locker(&mutex_); | |
| 73 HANDLE wait_handle = INVALID_HANDLE_VALUE; | 76 HANDLE wait_handle = INVALID_HANDLE_VALUE; |
| 74 BOOL success = RegisterWaitForSingleObject( | 77 BOOL success = RegisterWaitForSingleObject( |
| 75 &wait_handle, | 78 &wait_handle, |
| 76 handle, | 79 handle, |
| 77 &ExitCodeCallback, | 80 &ExitCodeCallback, |
| 78 reinterpret_cast<void*>(pid), | 81 reinterpret_cast<void*>(pid), |
| 79 INFINITE, | 82 INFINITE, |
| 80 WT_EXECUTEONLYONCE); | 83 WT_EXECUTEONLYONCE); |
| 81 if (!success) { | 84 if (!success) { |
| 82 FATAL("Failed to register exit code wait operation."); | 85 FATAL("Failed to register exit code wait operation."); |
| 83 } | 86 } |
| 84 ProcessInfo* info = new ProcessInfo(pid, handle, wait_handle, pipe); | 87 ProcessInfo* info = new ProcessInfo(pid, handle, wait_handle, pipe); |
| 85 // Now mutate the process list under the mutex. | 88 // Mutate the process list under the mutex. |
| 86 MutexLocker locker(&mutex_); | |
| 87 info->set_next(active_processes_); | 89 info->set_next(active_processes_); |
| 88 active_processes_ = info; | 90 active_processes_ = info; |
| 89 } | 91 } |
| 90 | 92 |
| 91 static bool LookupProcess(DWORD pid, | 93 static bool LookupProcess(DWORD pid, |
| 92 HANDLE* handle, | 94 HANDLE* handle, |
| 93 HANDLE* wait_handle, | 95 HANDLE* wait_handle, |
| 94 HANDLE* pipe) { | 96 HANDLE* pipe) { |
| 95 MutexLocker locker(&mutex_); | 97 MutexLocker locker(&mutex_); |
| 96 ProcessInfo* current = active_processes_; | 98 ProcessInfo* current = active_processes_; |
| (...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 502 | 504 |
| 503 bool Process::Kill(intptr_t id, int signal) { | 505 bool Process::Kill(intptr_t id, int signal) { |
| 504 USE(signal); // signal is not used on windows. | 506 USE(signal); // signal is not used on windows. |
| 505 HANDLE process_handle; | 507 HANDLE process_handle; |
| 506 HANDLE wait_handle; | 508 HANDLE wait_handle; |
| 507 HANDLE exit_pipe; | 509 HANDLE exit_pipe; |
| 508 bool success = ProcessInfoList::LookupProcess(id, | 510 bool success = ProcessInfoList::LookupProcess(id, |
| 509 &process_handle, | 511 &process_handle, |
| 510 &wait_handle, | 512 &wait_handle, |
| 511 &exit_pipe); | 513 &exit_pipe); |
| 512 ASSERT(success); | 514 if (!success) { |
| 515 return true; // The process has already died. Report a successful kill. | |
|
Anders Johnsen
2012/08/21 14:33:47
Returning true here is inconsistent with linux/mac
| |
| 516 } | |
| 513 BOOL result = TerminateProcess(process_handle, -1); | 517 BOOL result = TerminateProcess(process_handle, -1); |
| 514 if (!result) { | 518 if (!result) { |
| 515 return false; | 519 return false; |
| 516 } | 520 } |
| 517 return true; | 521 return true; |
| 518 } | 522 } |
| 519 | 523 |
| 520 | 524 |
| 521 void Process::TerminateExitCodeHandler() { | 525 void Process::TerminateExitCodeHandler() { |
| 522 // Nothing needs to be done on Windows. | 526 // Nothing needs to be done on Windows. |
| 523 } | 527 } |
| 524 | 528 |
| 525 | 529 |
| 526 intptr_t Process::CurrentProcessId() { | 530 intptr_t Process::CurrentProcessId() { |
| 527 return static_cast<intptr_t>(GetCurrentProcessId()); | 531 return static_cast<intptr_t>(GetCurrentProcessId()); |
| 528 } | 532 } |
| OLD | NEW |