[libcxx-commits] [PATCH] D112319: [NFC][libcxx] Clean up std::__call_once

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Dec 14 07:45:11 PST 2021


ldionne added a comment.

In D112319#3190723 <https://reviews.llvm.org/D112319#3190723>, @Quuxplusone wrote:

> FWIW, when I see the name `__scope_guard` I assume it'll work like a `finally` clause, running its code unconditionally on scope exit. (Like https://quuxplusone.github.io/blog/2018/08/11/the-auto-macro/ . :))
> But what we want here is a `catch` clause, running its code only if an exception actually //is// thrown. No matter what you name the scope-guard, it's going to be a speed bump for the reader, whereas `try { func(arg); } catch(...) { some code; throw; }` can be understood instantly.
> Perhaps Louis's idea, but with the scope-guard class type named `struct __catch_and_rethrow_logic`? It needs //something// to catch the reader's attention that it's specifically the catch path, not a general-purpose scope guard.

Fair point. Let's bikeshed about the name and the specific design of that type in D115730 <https://reviews.llvm.org/D115730>.

With that review, we'd be able to write:

  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)
      {
          __transaction transaction([&flag] { flag = once_flag::_Unset; });
          flag = once_flag::_Pending;
          func(arg);
          flag = once_flag::_Complete;
          transaction.__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)
      {
          __transaction transaction([&mut, &flag, &cv] {
              __libcpp_mutex_lock(&mut);
              __libcpp_relaxed_store(&flag, once_flag::_Unset);
              __libcpp_mutex_unlock(&mut);
              __libcpp_condvar_broadcast(&cv);
          });
  
          __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);
          transaction.__complete();
      }
      else
      {
          __libcpp_mutex_unlock(&mut);
      }
  #endif // !_LIBCPP_HAS_NO_THREADS
  }


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112319/new/

https://reviews.llvm.org/D112319



More information about the libcxx-commits mailing list