diff options
author | jeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-05 01:57:21 +0000 |
---|---|---|
committer | jeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-05 01:57:21 +0000 |
commit | 43bc28320c2e383b74d4e4c031bc01505e3a6608 (patch) | |
tree | b2055861e7dd39839018b7e1d9cfb4a25150e578 | |
parent | 4c7073f666fd6c0209e1e9ae87e53698371ed973 (diff) | |
download | chromium_src-43bc28320c2e383b74d4e4c031bc01505e3a6608.zip chromium_src-43bc28320c2e383b74d4e4c031bc01505e3a6608.tar.gz chromium_src-43bc28320c2e383b74d4e4c031bc01505e3a6608.tar.bz2 |
* Add timeout support to POSIX WaitForSingleProcess()
* Unify Linux & Mac implementations of WaitForSingleProcess()
Review URL: http://codereview.chromium.org/12969
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6416 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/process_util_linux.cc | 6 | ||||
-rw-r--r-- | base/process_util_mac.mm | 7 | ||||
-rw-r--r-- | base/process_util_posix.cc | 20 |
3 files changed, 19 insertions, 14 deletions
diff --git a/base/process_util_linux.cc b/base/process_util_linux.cc index ec45d3f..7b8e411 100644 --- a/base/process_util_linux.cc +++ b/base/process_util_linux.cc @@ -264,12 +264,6 @@ bool WaitForProcessesToExit(const std::wstring& executable_name, return result; } -bool WaitForSingleProcess(ProcessHandle handle, int wait_milliseconds) { - int status; - waitpid(handle, &status, 0); - return WIFEXITED(status); -} - bool CleanupProcesses(const std::wstring& executable_name, int wait_milliseconds, int exit_code, diff --git a/base/process_util_mac.mm b/base/process_util_mac.mm index 99843de..a0a123a 100644 --- a/base/process_util_mac.mm +++ b/base/process_util_mac.mm @@ -52,13 +52,6 @@ bool LaunchApp(const CommandLine& cl, return LaunchApp(cl.argv(), wait, process_handle); } -bool WaitForSingleProcess(ProcessHandle handle, int wait_milliseconds) { - // TODO(playmobil): Do we need to support wait_milliseconds? - int status; - waitpid(handle, &status, 0); - return WIFEXITED(status); -} - bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) { // TODO(pinkerton): can we implement this? On linux it relies on /proc. return false; diff --git a/base/process_util_posix.cc b/base/process_util_posix.cc index 226589a..3bead6a 100644 --- a/base/process_util_posix.cc +++ b/base/process_util_posix.cc @@ -7,11 +7,13 @@ #include <sys/resource.h> #include <sys/time.h> #include <sys/types.h> +#include <sys/wait.h> #include <unistd.h> #include "base/basictypes.h" #include "base/logging.h" #include "base/sys_info.h" +#include "base/time.h" const int kMicrosecondsPerSecond = 1000000; @@ -51,6 +53,22 @@ void RaiseProcessToHighPriority() { // setpriority() or sched_getscheduler, but these all require extra rights. } +bool WaitForSingleProcess(ProcessHandle handle, int wait_milliseconds) { + int status; + pid_t ret_pid = waitpid(handle, &status, WNOHANG); + + // If the process hasn't exited yet, then sleep and try again. + Time wakeup_time = Time::Now() + TimeDelta::FromMilliseconds( + wait_milliseconds); + while (ret_pid == 0 && Time::Now() < wakeup_time) { + int64 sleep_time_usecs = (wakeup_time - Time::Now()).InMicroseconds(); + usleep(sleep_time_usecs); // usleep will exit on EINTR. + ret_pid = waitpid(handle, &status, WNOHANG); + } + + return WIFEXITED(status); +} + namespace { int64 TimeValToMicroseconds(const struct timeval& tv) { @@ -69,7 +87,7 @@ int ProcessMetrics::GetCPUUsage() { retval = getrusage(RUSAGE_SELF, &usage); if (retval) return 0; - + int64 system_time = (TimeValToMicroseconds(usage.ru_stime) + TimeValToMicroseconds(usage.ru_utime)) / processor_count_; |