[libcxx-commits] [libcxx] be8c2df - [libc++][NFC] Clean up std::__call_once

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Sep 19 13:17:32 PDT 2023


Author: Daniel McIntosh
Date: 2023-09-19T16:17:20-04:00
New Revision: be8c2df2b15b17ef7a409a95333f09b016fc4a7e

URL: https://github.com/llvm/llvm-project/commit/be8c2df2b15b17ef7a409a95333f09b016fc4a7e
DIFF: https://github.com/llvm/llvm-project/commit/be8c2df2b15b17ef7a409a95333f09b016fc4a7e.diff

LOG: [libc++][NFC] Clean up std::__call_once

__call_once is large and cluttered with #ifdef preprocessor guards. This
cleans it up a bit by using an exception guard instead of try-catch.

Differential Revision: https://reviews.llvm.org/D112319
Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>

Added: 
    

Modified: 
    libcxx/src/mutex.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/src/mutex.cpp b/libcxx/src/mutex.cpp
index 1378e8872b2712d..b165b1bc9debfc5 100644
--- a/libcxx/src/mutex.cpp
+++ b/libcxx/src/mutex.cpp
@@ -8,6 +8,7 @@
 
 #include <__assert>
 #include <__thread/id.h>
+#include <__utility/exception_guard.h>
 #include <limits>
 #include <mutex>
 
@@ -205,56 +206,40 @@ void __call_once(volatile once_flag::_State_type& flag, void* arg,
                  void (*func)(void*))
 {
 #if defined(_LIBCPP_HAS_NO_THREADS)
-    if (flag == once_flag::_Unset)
-    {
-#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-        try
-        {
-#endif // _LIBCPP_HAS_NO_EXCEPTIONS
-            flag = once_flag::_Pending;
-            func(arg);
-            flag = once_flag::_Complete;
-#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-        }
-        catch (...)
-        {
-            flag = once_flag::_Unset;
-            throw;
-        }
-#endif // _LIBCPP_HAS_NO_EXCEPTIONS
+
+    if (flag == once_flag::_Unset) {
+        auto guard = std::__make_exception_guard([&flag] { flag = once_flag::_Unset; });
+        flag = once_flag::_Pending;
+        func(arg);
+        flag = once_flag::_Complete;
+        guard.__complete();
     }
+
 #else // !_LIBCPP_HAS_NO_THREADS
+
     __libcpp_mutex_lock(&mut);
     while (flag == once_flag::_Pending)
         __libcpp_condvar_wait(&cv, &mut);
-    if (flag == once_flag::_Unset)
-    {
-#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-        try
-        {
-#endif // _LIBCPP_HAS_NO_EXCEPTIONS
-            __libcpp_relaxed_store(&flag, once_flag::_Pending);
-            __libcpp_mutex_unlock(&mut);
-            func(arg);
-            __libcpp_mutex_lock(&mut);
-            __libcpp_atomic_store(&flag, once_flag::_Complete,
-                                  _AO_Release);
-            __libcpp_mutex_unlock(&mut);
-            __libcpp_condvar_broadcast(&cv);
-#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
-        }
-        catch (...)
-        {
+    if (flag == once_flag::_Unset) {
+        auto guard = std::__make_exception_guard([&flag] {
             __libcpp_mutex_lock(&mut);
             __libcpp_relaxed_store(&flag, once_flag::_Unset);
             __libcpp_mutex_unlock(&mut);
             __libcpp_condvar_broadcast(&cv);
-            throw;
-        }
-#endif // _LIBCPP_HAS_NO_EXCEPTIONS
-    }
-    else
+        });
+
+        __libcpp_relaxed_store(&flag, once_flag::_Pending);
+        __libcpp_mutex_unlock(&mut);
+        func(arg);
+        __libcpp_mutex_lock(&mut);
+        __libcpp_atomic_store(&flag, once_flag::_Complete, _AO_Release);
         __libcpp_mutex_unlock(&mut);
+        __libcpp_condvar_broadcast(&cv);
+        guard.__complete();
+    } else {
+        __libcpp_mutex_unlock(&mut);
+    }
+
 #endif // !_LIBCPP_HAS_NO_THREADS
 }
 


        


More information about the libcxx-commits mailing list