brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.9 KiB · 1ad6f78 Raw
79 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// bool request_stop() noexcept;13 14#include <cassert>15#include <chrono>16#include <concepts>17#include <optional>18#include <stop_token>19#include <type_traits>20 21#include "make_test_thread.h"22#include "test_macros.h"23 24template <class T>25concept IsRequestStopNoexcept = requires(T& t) {26  { t.request_stop() } noexcept;27};28 29static_assert(IsRequestStopNoexcept<std::stop_source>);30 31int main(int, char**) {32  // If *this does not have ownership of a stop state, returns false33  {34    std::stop_source ss{std::nostopstate};35    auto ret = ss.request_stop();36    assert(!ret);37    assert(!ss.stop_requested());38  }39 40  // Otherwise, atomically determines whether the owned stop state has received41  // a stop request, and if not, makes a stop request42  {43    std::stop_source ss;44 45    auto ret = ss.request_stop();46    assert(ret);47    assert(ss.stop_requested());48  }49 50  // already requested51  {52    std::stop_source ss;53    ss.request_stop();54    assert(ss.stop_requested());55 56    auto ret = ss.request_stop();57    assert(!ret);58    assert(ss.stop_requested());59  }60 61  // If the request was made, the callbacks registered by62  // associated stop_callback objects are synchronously called.63  {64    std::stop_source ss;65    auto st = ss.get_token();66 67    bool cb1Called = false;68    bool cb2Called = false;69    std::stop_callback sc1(st, [&] { cb1Called = true; });70    std::stop_callback sc2(st, [&] { cb2Called = true; });71 72    ss.request_stop();73    assert(cb1Called);74    assert(cb2Called);75  }76 77  return 0;78}79