You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2015/06/04 09:30:23 UTC
[2/9] mesos git commit: Move synchronized.hpp into stout.
Move synchronized.hpp into stout.
Review: https://reviews.apache.org/r/35012
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/90c10cbd
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/90c10cbd
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/90c10cbd
Branch: refs/heads/master
Commit: 90c10cbd6ff183b2ed00cfd98451ea5b9abd9f5b
Parents: 9f9d773
Author: Joris Van Remoortere <jo...@gmail.com>
Authored: Thu Jun 4 00:24:45 2015 -0700
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Thu Jun 4 00:24:45 2015 -0700
----------------------------------------------------------------------
.../3rdparty/stout/include/Makefile.am | 1 +
.../stout/include/stout/synchronized.hpp | 75 ++++++++++++++++++++
2 files changed, 76 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/90c10cbd/3rdparty/libprocess/3rdparty/stout/include/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am
index 79239d7..6ac2f04 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am
+++ b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am
@@ -72,6 +72,7 @@ nobase_include_HEADERS = \
stout/strings.hpp \
stout/subcommand.hpp \
stout/svn.hpp \
+ stout/synchronized.hpp \
stout/tests/utils.hpp \
stout/thread.hpp \
stout/try.hpp \
http://git-wip-us.apache.org/repos/asf/mesos/blob/90c10cbd/3rdparty/libprocess/3rdparty/stout/include/stout/synchronized.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/synchronized.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/synchronized.hpp
new file mode 100644
index 0000000..60eaf26
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/synchronized.hpp
@@ -0,0 +1,75 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __STOUT_SYNCHRONIZED_HPP__
+#define __STOUT_SYNCHRONIZED_HPP__
+
+#include <atomic>
+#include <mutex>
+#include <type_traits>
+
+// A helper class for the synchronized(m) macro. It is an RAII 'guard'
+// for a synchronization primitive 'T'. The general template handles
+// cases such as 'std::mutex' and 'std::recursive_mutex'.
+template <typename T>
+class Synchronized
+{
+public:
+ Synchronized(T* _lock) : lock(CHECK_NOTNULL(_lock)) { lock->lock(); }
+ Synchronized(T** _lock) : Synchronized(*CHECK_NOTNULL(_lock)) {}
+
+ ~Synchronized() { lock->unlock(); }
+
+ operator bool() const { return true; }
+private:
+ T* lock;
+};
+
+
+// A specialization of the Synchronized class for 'std::atomic_flag'.
+// This is necessary as the locking functions are different.
+template <>
+class Synchronized<std::atomic_flag>
+{
+public:
+ Synchronized(std::atomic_flag* _flag) : flag(CHECK_NOTNULL(_flag))
+ {
+ while (flag->test_and_set(std::memory_order_acquire)) {}
+ }
+ Synchronized(std::atomic_flag** _flag)
+ : Synchronized(*CHECK_NOTNULL(_flag)) {}
+
+ ~Synchronized()
+ {
+ flag->clear(std::memory_order_release);
+ }
+
+ operator bool() const { return true; }
+private:
+ std::atomic_flag* flag;
+};
+
+
+// A macro for acquiring a scoped 'guard' on any type that can satisfy
+// the 'Synchronized' interface. Currently this includes 'std::mutex',
+// 'std::recursive_mutex' and 'std::atomic_flag'.
+// Example:
+// std::mutex m;
+// synchronized (m) {
+// // Do something under the lock.
+// }
+#define synchronized(m) \
+ if (auto __ ## __file__ ## _ ## __line__ ## __lock = Synchronized<typename std::remove_pointer<decltype(m)>::type>(&m)) // NOLINT(whitespace/line_length)
+
+#endif // __STOUT_SYNCHRONIZED_HPP__