brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · 80b3e98 Raw
125 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// ADDITIONAL_COMPILE_FLAGS(gcc-style-warnings): -Wno-self-move12 13// jthread& operator=(jthread&&) noexcept;14 15#include <atomic>16#include <cassert>17#include <concepts>18#include <stop_token>19#include <thread>20#include <type_traits>21#include <utility>22#include <vector>23 24#include "make_test_thread.h"25#include "test_macros.h"26 27static_assert(std::is_nothrow_move_assignable_v<std::jthread>);28 29int main(int, char**) {30  // If &x == this is true, there are no effects.31  {32    std::jthread j = support::make_test_jthread([] {});33    auto id        = j.get_id();34    auto ssource   = j.get_stop_source();35    j              = std::move(j);36    assert(j.get_id() == id);37    assert(j.get_stop_source() == ssource);38  }39 40  // if joinable() is true, calls request_stop() and then join()41  // request_stop is called42  {43    std::jthread j1 = support::make_test_jthread([] {});44    bool called     = false;45    std::stop_callback cb(j1.get_stop_token(), [&called] { called = true; });46 47    std::jthread j2 = support::make_test_jthread([] {});48    j1              = std::move(j2);49    assert(called);50  }51 52  // if joinable() is true, calls request_stop() and then join()53  // join is called54  {55    std::atomic_int calledTimes = 0;56    std::vector<std::jthread> jts;57    constexpr auto numberOfThreads = 10u;58    jts.reserve(numberOfThreads);59    for (auto i = 0u; i < numberOfThreads; ++i) {60      jts.emplace_back(support::make_test_jthread([&] {61        std::this_thread::sleep_for(std::chrono::milliseconds(2));62        calledTimes.fetch_add(1, std::memory_order_relaxed);63      }));64    }65 66    for (auto i = 0u; i < numberOfThreads; ++i) {67      jts[i] = std::jthread{};68    }69 70    // If join was called as expected, calledTimes must equal to numberOfThreads71    // If join was not called, there is a chance that the check below happened72    // before test threads incrementing the counter, thus calledTimed would73    // be less than numberOfThreads.74    // This is not going to catch issues 100%. Creating more threads to increase75    // the probability of catching the issue76    assert(calledTimes.load(std::memory_order_relaxed) == numberOfThreads);77  }78 79  // then assigns the state of x to *this80  {81    std::jthread j1 = support::make_test_jthread([] {});82    std::jthread j2 = support::make_test_jthread([] {});83    auto id2        = j2.get_id();84    auto ssource2   = j2.get_stop_source();85 86    j1 = std::move(j2);87 88    assert(j1.get_id() == id2);89    assert(j1.get_stop_source() == ssource2);90  }91 92  // sets x to a default constructed state93  {94    std::jthread j1 = support::make_test_jthread([] {});95    std::jthread j2 = support::make_test_jthread([] {});96    j1              = std::move(j2);97 98    assert(j2.get_id() == std::jthread::id());99    assert(!j2.get_stop_source().stop_possible());100  }101 102  // joinable is false103  {104    std::jthread j1;105    std::jthread j2 = support::make_test_jthread([] {});106 107    auto j2Id = j2.get_id();108 109    j1 = std::move(j2);110 111    assert(j1.get_id() == j2Id);112  }113 114  // LWG3788: self-assignment115  {116    std::jthread j = support::make_test_jthread([] {});117    auto oldId     = j.get_id();118    j              = std::move(j);119 120    assert(j.get_id() == oldId);121  }122 123  return 0;124}125