Index: libcxx/include/barrier =================================================================== --- libcxx/include/barrier +++ libcxx/include/barrier @@ -300,6 +300,13 @@ _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 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; @@ -308,6 +315,7 @@ [[__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 Index: libcxx/test/libcxx/thread/thread.barrier/assert.arrive.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/libcxx/thread/thread.barrier/assert.arrive.pass.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// + +// class barrier; + +// void arrive(ptrdiff_t __update = 1); + +// Make sure that calling arrive with a negative value triggers an assertion. + +// REQUIRES: has-unix-headers +// XFAIL: availability-verbose_abort-missing +// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 + +#include + +#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"); + } + + return 0; +} Index: libcxx/test/libcxx/thread/thread.barrier/assert.ctor.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/libcxx/thread/thread.barrier/assert.ctor.pass.cpp @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// + +// class barrier; + +// barrier(ptrdiff_t __count, _CompletionF __completion = _CompletionF()); + +// Make sure that constructing barrier with a negative value triggers an assertion + +// REQUIRES: has-unix-headers +// XFAIL: availability-verbose_abort-missing +// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 + +#include + +#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"); + } + + // We can't check the precondition for max() because there's no value + // that would violate the precondition (in our implementation) + + return 0; +}