Skip to content

Commit

Permalink
Enhance _STL_VERIFY for std::barrier (#3757)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan T. Lavavej <stl@nuwen.net>
  • Loading branch information
achabense and StephanTLavavej committed Jun 15, 2023
1 parent f528f02 commit 25a58af
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions stl/inc/barrier
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ inline constexpr ptrdiff_t _Barrier_value_mask = ~_Barrier_arrival_token
inline constexpr ptrdiff_t _Barrier_value_shift = 1;
inline constexpr ptrdiff_t _Barrier_invalid_token = 0;
inline constexpr ptrdiff_t _Barrier_value_step = 1 << _Barrier_value_shift;
inline constexpr ptrdiff_t _Barrier_max = (1ULL << (sizeof(ptrdiff_t) * CHAR_BIT - 2)) - 1;
inline constexpr ptrdiff_t _Barrier_max = PTRDIFF_MAX >> _Barrier_value_shift;

template <class _Completion_function>
class _Arrival_token {
Expand Down Expand Up @@ -81,7 +81,7 @@ public:
constexpr explicit barrier(
const ptrdiff_t _Expected, _Completion_function _Fn = _Completion_function()) noexcept /* strengthened */
: _Val(_One_then_variadic_args_t{}, _STD move(_Fn), _Expected << _Barrier_value_shift) {
_STL_VERIFY(_Val._Myval2._Current.load(memory_order_relaxed) >= 0,
_STL_VERIFY(_Expected >= 0 && _Expected <= (max) (),
"Precondition: expected >= 0 and expected <= max() (N4950 [thread.barrier.class]/9)");
}

Expand All @@ -93,9 +93,8 @@ public:
}

_NODISCARD_BARRIER_TOKEN arrival_token arrive(ptrdiff_t _Update = 1) noexcept /* strengthened */ {
// Shifting before precondition check, so that exceeding max() will trigger precondition check too
_STL_VERIFY(_Update > 0 && _Update <= (max) (), "Precondition: update > 0 (N4950 [thread.barrier.class]/12)");
_Update <<= _Barrier_value_shift;
_STL_VERIFY(_Update > 0, "Precondition: update > 0 (N4950 [thread.barrier.class]/12)");
// TRANSITION, GH-1133: should be memory_order_release
ptrdiff_t _Current = _Val._Myval2._Current.fetch_sub(_Update) - _Update;
_STL_VERIFY(_Current >= 0, "Precondition: update is less than or equal to the expected count "
Expand Down

0 comments on commit 25a58af

Please sign in to comment.