brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.3 KiB · d7efed7 Raw
94 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// UNSUPPORTED: no-threads10// UNSUPPORTED: c++03, c++11, c++14, c++1711 12// [[nodiscard]] bool stop_possible() const noexcept;13// Returns: false if:14//    - *this does not have ownership of a stop state, or15//    - a stop request was not made and there are no associated stop_source objects;16// otherwise, true.17 18#include <cassert>19#include <concepts>20#include <optional>21#include <stop_token>22#include <type_traits>23 24#include "make_test_thread.h"25#include "test_macros.h"26 27template <class T>28concept IsStopPossibleNoexcept = requires(const T& t) {29  { t.stop_possible() } noexcept;30};31 32static_assert(IsStopPossibleNoexcept<std::stop_token>);33 34int main(int, char**) {35  // no state36  {37    const std::stop_token st;38    assert(!st.stop_possible());39  }40 41  // a stop request was not made and there are no associated stop_source objects42  {43    std::optional<std::stop_source> ss{std::in_place};44    const auto st = ss->get_token();45    ss.reset();46 47    assert(!st.stop_possible());48  }49 50  // a stop request was not made, but there is an associated stop_source objects51  {52    std::stop_source ss;53    const auto st = ss.get_token();54    assert(st.stop_possible());55  }56 57  // a stop request was made and there are no associated stop_source objects58  {59    std::optional<std::stop_source> ss{std::in_place};60    const auto st = ss->get_token();61    ss->request_stop();62    ss.reset();63 64    assert(st.stop_possible());65  }66 67  // a stop request was made and there is an associated stop_source objects68  {69    std::stop_source ss;70    const auto st = ss.get_token();71    ss.request_stop();72    assert(st.stop_possible());73  }74 75  // a stop request was made on a different thread and76  // there are no associated stop_source objects77  {78    std::optional<std::stop_source> ss{std::in_place};79    const auto st = ss->get_token();80 81    std::thread t = support::make_test_thread([&]() {82      ss->request_stop();83      ss.reset();84    });85 86    assert(st.stop_possible());87    t.join();88    assert(st.stop_possible());89 90  }91 92  return 0;93}94