Adding assertions will aid users that have bugs in their code to receive better error messages.
Reviewed By: #libc, ldionne
Differential Revision: https://reviews.llvm.org/D154626
[[__nodiscard__]] _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
arrival_token arrive(ptrdiff_t __update)
{
+ _LIBCPP_ASSERT_UNCATEGORIZED(
+ __update <= __expected_, "update is greater than the expected count for the current barrier phase");
+
auto const __old_phase = __phase_.load(memory_order_relaxed);
for(; __update; --__update)
if(__arrive_barrier_algorithm_base(__base_.get(), __old_phase)) {
auto const __old_phase = __phase.load(memory_order_relaxed);
auto const __result = __arrived.fetch_sub(update, memory_order_acq_rel) - update;
auto const new_expected = __expected.load(memory_order_relaxed);
- if(0 == __result) {
+
+ _LIBCPP_ASSERT_UNCATEGORIZED(
+ update <= new_expected, "update is greater than the expected count for the current barrier phase");
+
+ if (0 == __result) {
__completion();
__arrived.store(new_expected, memory_order_relaxed);
__phase.store(!__old_phase, memory_order_release);
{
auto const __inc = __arrived_unit * update;
auto const __old = __phase_arrived_expected.fetch_add(__inc, memory_order_acq_rel);
- if((__old ^ (__old + __inc)) & __phase_bit) {
+
+ _LIBCPP_ASSERT_UNCATEGORIZED(
+ update <= __old, "update is greater than the expected count for the current barrier phase");
+
+ if ((__old ^ (__old + __inc)) & __phase_bit) {
__phase_arrived_expected.fetch_add((__old & __expected_mask) << 32, memory_order_relaxed);
__phase_arrived_expected.notify_all();
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
explicit barrier(ptrdiff_t __count, _CompletionF __completion = _CompletionF())
: __b_(__count, _VSTD::move(__completion)) {
+ _LIBCPP_ASSERT_UNCATEGORIZED(
+ __count >= 0,
+ "barrier::barrier(ptrdiff_t, CompletionFunction): barrier cannot be initialized with a negative value");
+ _LIBCPP_ASSERT_UNCATEGORIZED(
+ __count <= max(),
+ "barrier::barrier(ptrdiff_t, CompletionFunction): barrier cannot be initialized with "
+ "a value greater than max()");
}
barrier(barrier const&) = delete;
[[__nodiscard__]] _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
arrival_token arrive(ptrdiff_t __update = 1)
{
+ _LIBCPP_ASSERT_UNCATEGORIZED(__update > 0, "barrier:arrive must be called with a value greater than 0");
return __b_.arrive(__update);
}
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
--- /dev/null
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: no-threads
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// REQUIRES: availability-synchronization_library-missing
+// XFAIL: target={{.+}}-apple-macosx10.{{13|15}}
+
+// REQUIRES: has-unix-headers
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1
+
+// <barrier>
+
+// class barrier;
+
+// void arrive(ptrdiff_t __update = 1);
+
+// Make sure that calling arrive with a negative value or with a value greater than 'expected' triggers an assertion
+
+#include <barrier>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ {
+ std::barrier<> b(5);
+ TEST_LIBCPP_ASSERT_FAILURE(b.arrive(0), "barrier:arrive must be called with a value greater than 0");
+ }
+
+ {
+ std::barrier<> b(5);
+ TEST_LIBCPP_ASSERT_FAILURE(b.arrive(10), "update is greater than the expected count for the current barrier phase");
+ }
+
+ return 0;
+}
--- /dev/null
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: no-threads
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// REQUIRES: availability-synchronization_library-missing
+// XFAIL: target={{.+}}-apple-macosx10.{{13|15}}
+
+// REQUIRES: has-unix-headers
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1
+
+// <barrier>
+
+// class barrier;
+
+// barrier(ptrdiff_t __count, _CompletionF __completion = _CompletionF());
+
+// Make sure that constructing barrier with a negative value triggers an assertion
+
+#include <barrier>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ {
+ TEST_LIBCPP_ASSERT_FAILURE(
+ [] { std::barrier<> b(-1); }(),
+ "barrier::barrier(ptrdiff_t, CompletionFunction): barrier cannot be initialized with a negative value");
+ }
+
+ {
+ TEST_LIBCPP_ASSERT_FAILURE(
+ [] {
+ auto completion = []() {};
+ std::barrier<decltype(completion)> b(-1, completion);
+ }(),
+ "barrier::barrier(ptrdiff_t, CompletionFunction): barrier cannot be initialized with a negative value");
+ }
+
+ // We can't check the precondition for max() because there's no value
+ // that would violate the precondition (in our implementation)
+
+ return 0;
+}