[libcxx-commits] [PATCH] D154425: [libc++] add basic runtime assertions to <latch>

Edo via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 4 02:43:49 PDT 2023


diamante0018 created this revision.
Herald added a project: All.
diamante0018 requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

I think adding a few runtimes checks with assertions will aid users that have bugs in their code to better troubleshoot issues.

Please let me know if the messages attached to the various assertions can be improved. I am not yet familiar with how those should be worded but I looked at some other libc++ modules to take inspiration from them.

Thanks.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154425

Files:
  libcxx/include/latch


Index: libcxx/include/latch
===================================================================
--- libcxx/include/latch
+++ libcxx/include/latch
@@ -70,12 +70,17 @@
     __atomic_base<ptrdiff_t> __a_;
 
 public:
-    static _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t max() noexcept {
+    static _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t max() noexcept
+    {
         return numeric_limits<ptrdiff_t>::max();
     }
 
     inline _LIBCPP_INLINE_VISIBILITY
-    constexpr explicit latch(ptrdiff_t __expected) : __a_(__expected) { }
+    constexpr explicit latch(ptrdiff_t __expected) : __a_(__expected)
+    {
+        _LIBCPP_ASSERT_UNCATEGORIZED(__expected >= 0,
+                                     "__expected cannot be negative");
+    }
 
     _LIBCPP_HIDE_FROM_ABI ~latch() = default;
     latch(const latch&) = delete;
@@ -84,6 +89,8 @@
     inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
     void count_down(ptrdiff_t __update = 1)
     {
+        _LIBCPP_ASSERT_UNCATEGORIZED(
+            __update >= 0, "latch::count_down called with a negative value");
         auto const __old = __a_.fetch_sub(__update, memory_order_release);
         if(__old == __update)
             __a_.notify_all();
@@ -103,6 +110,8 @@
     inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
     void arrive_and_wait(ptrdiff_t __update = 1)
     {
+        _LIBCPP_ASSERT_UNCATEGORIZED(
+            __update >= 0, "latch::arrive_and_wait called with a negative value");
         count_down(__update);
         wait();
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154425.537009.patch
Type: text/x-patch
Size: 1538 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230704/db738f65/attachment.bin>


More information about the libcxx-commits mailing list