Repository: mesos
Updated Branches:
  refs/heads/master 11a2311f7 -> 3b1eeec85


Added os::clone function in stout.

Review: https://reviews.apache.org/r/38639


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/f279cf2f
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/f279cf2f
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/f279cf2f

Branch: refs/heads/master
Commit: f279cf2f82541852b878bdd2e881fe24cd8d0a7b
Parents: 11a2311
Author: haosdent huang <[email protected]>
Authored: Tue Sep 22 23:02:54 2015 -0700
Committer: Jie Yu <[email protected]>
Committed: Tue Sep 22 23:02:55 2015 -0700

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/os/linux.hpp   | 46 +++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/f279cf2f/3rdparty/libprocess/3rdparty/stout/include/stout/os/linux.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/linux.hpp 
b/3rdparty/libprocess/3rdparty/stout/include/stout/os/linux.hpp
index b994b13..15ca091 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/linux.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/linux.hpp
@@ -28,6 +28,7 @@
 
 #include <stout/error.hpp>
 #include <stout/foreach.hpp>
+#include <stout/lambda.hpp>
 #include <stout/option.hpp>
 #include <stout/proc.hpp>
 #include <stout/result.hpp>
@@ -37,6 +38,49 @@
 
 namespace os {
 
+
+// Helper for clone() which expects an int(void*).
+static int childMain(void* _func)
+{
+  const lambda::function<int()>* func =
+    static_cast<const lambda::function<int()>*> (_func);
+
+  return (*func)();
+}
+
+
+inline pid_t clone(const lambda::function<int()>& func, int flags)
+{
+  // Stack for the child.
+  // - unsigned long long used for best alignment.
+  // - 8 MiB appears to be the default for "ulimit -s" on OSX and Linux.
+  //
+  // NOTE: We need to allocate the stack dynamically. This is because
+  // glibc's 'clone' will modify the stack passed to it, therefore the
+  // stack must NOT be shared as multiple 'clone's can be invoked
+  // simultaneously.
+  int stackSize = 8 * 1024 * 1024;
+  unsigned long long *stack =
+    new unsigned long long[stackSize/sizeof(unsigned long long)];
+
+  pid_t pid = ::clone(
+      childMain,
+      &stack[stackSize/sizeof(stack[0]) - 1],  // stack grows down.
+      flags,
+      (void*) &func);
+
+  // If CLONE_VM is not set, ::clone would create a process which runs in a
+  // separate copy of the memory space of the calling process. So we destroy 
the
+  // stack here to avoid memory leak. If CLONE_VM is set, ::clone would create 
a
+  // thread which runs in the same memory space with the calling process.
+  if (!(flags & CLONE_VM)) {
+    delete[] stack;
+  }
+
+  return pid;
+}
+
+
 inline Result<Process> process(pid_t pid)
 {
   // Page size, used for memory accounting.
@@ -91,7 +135,7 @@ inline Result<Process> process(pid_t pid)
 }
 
 
-inline Try<std::set<pid_t> > pids()
+inline Try<std::set<pid_t>> pids()
 {
   return proc::pids();
 }

Reply via email to