[PATCH] D21260: Implement variadic lock_guard.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 13 15:22:06 PDT 2016


EricWF marked an inline comment as done.

================
Comment at: include/mutex:592-652
@@ -578,3 +591,63 @@
 #endif  // _LIBCPP_HAS_NO_VARIADICS
 
+
+#if defined(_LIBCPP_ABI_VARIADIC_LOCK_GUARD) \
+    && !defined(_LIBCPP_CXX03_LANG)
+template <>
+class _LIBCPP_TYPE_VIS_ONLY lock_guard<> {
+public:
+    explicit lock_guard() = default;
+    ~lock_guard() = default;
+
+    _LIBCPP_INLINE_VISIBILITY
+    explicit lock_guard(adopt_lock_t) {}
+
+    lock_guard(lock_guard const&) = delete;
+    lock_guard& operator=(lock_guard const&) = delete;
+};
+
+template <class _M1, class _M2, class ..._MRest>
+class _LIBCPP_TYPE_VIS_ONLY lock_guard<_M1, _M2, _MRest...>
+{
+    typedef tuple<_M1&, _M2&, _MRest&...> _MutexTuple;
+
+public:
+    _LIBCPP_INLINE_VISIBILITY
+    explicit lock_guard(_M1& __m1, _M2& __m2, _MRest&... __mrest)
+      : __t_(__m1, __m2, __mrest...)
+    {
+        _VSTD::lock(__m1, __m2, __mrest...);
+    }
+
+    _LIBCPP_INLINE_VISIBILITY
+    lock_guard(_M1& __m1, _M2& __m2, _MRest&... __mrest, adopt_lock_t)
+        : __t_(__m1, __m2, __mrest...)
+    {
+    }
+
+    _LIBCPP_INLINE_VISIBILITY
+    ~lock_guard() {
+        typedef typename __make_tuple_indices<sizeof...(_MRest) + 2>::type _Indices;
+        __unlock_unpack(_Indices{}, __t_);
+    }
+
+    lock_guard(lock_guard const&) = delete;
+    lock_guard& operator=(lock_guard const&) = delete;
+
+private:
+    template <size_t ..._Indx>
+    _LIBCPP_INLINE_VISIBILITY
+    static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
+        _VSTD::__swallow(__unlock(_VSTD::get<_Indx>(__mt))...);
+    }
+
+    template <class _Mutex>
+    _LIBCPP_INLINE_VISIBILITY
+    static bool __unlock(_Mutex& __m) { __m.unlock(); return false; }
+
+    _MutexTuple __t_;
+};
+
+#endif // _LIBCPP_ABI_VARIADIC_LOCK_GUARD
+
 _LIBCPP_END_NAMESPACE_STD
----------------
Done, but you might want to check that change. I tried to do it so that it's clear that the variadic lock_guard is only available in ABI V2.


http://reviews.llvm.org/D21260





More information about the cfe-commits mailing list