aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-11-25 12:48:15 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-11-25 12:48:15 +0200
commit44c0fc2820bb13018b200d3ec990c30cdd2a08cd (patch)
tree99445eac0ab0c0d10bcaf6b35020db0788f43e25
parentefd154a6af61e80be1b0c46642cefd73cc83d7ed (diff)
Add process::try_wait() for non-blocking wait
-rw-r--r--butl/process6
-rw-r--r--butl/process.cxx20
2 files changed, 26 insertions, 0 deletions
diff --git a/butl/process b/butl/process
index bbd0162..6077ad3 100644
--- a/butl/process
+++ b/butl/process
@@ -75,6 +75,12 @@ namespace butl
bool
wait ();
+ // Return true if the process has already terminated in which case
+ // the argument is set to the result of wait().
+ //
+ bool
+ try_wait (bool&);
+
~process () {if (id != 0) wait ();}
// Move constructible-only type.
diff --git a/butl/process.cxx b/butl/process.cxx
index 66a831e..bd13392 100644
--- a/butl/process.cxx
+++ b/butl/process.cxx
@@ -121,6 +121,26 @@ namespace butl
return WIFEXITED (status) && WEXITSTATUS (status) == 0;
}
+ bool process::
+ try_wait (bool& s)
+ {
+ if (id != 0)
+ {
+ int r (waitpid (id, &status, WNOHANG));
+
+ if (r == 0) // Not exited yet.
+ return false;
+
+ id = 0; // We have tried.
+
+ if (r == -1)
+ throw process_error (errno, false);
+ }
+
+ s = WIFEXITED (status) && WEXITSTATUS (status) == 0;
+ return true;
+ }
+
#else // _WIN32
static void