brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · 6b47025 Raw
78 lines · cpp
1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7 8// UNSUPPORTED: c++03, c++11, c++14, c++179// UNSUPPORTED: no-threads10// XFAIL: !has-64-bit-atomics11// XFAIL: !has-1024-bit-atomics12 13// void notify_all() const noexcept;14 15#include <atomic>16#include <cassert>17#include <thread>18#include <type_traits>19#include <vector>20 21#include "atomic_helpers.h"22#include "make_test_thread.h"23#include "test_macros.h"24 25template <typename T>26struct TestNotifyAll {27  void operator()() const {28    T x(T(1));29    std::atomic_ref<T> const a(x);30 31    bool done                      = false;32    std::atomic<int> started_num   = 0;33    std::atomic<int> wait_done_num = 0;34 35    constexpr auto number_of_threads = 8;36    std::vector<std::thread> threads;37    threads.reserve(number_of_threads);38 39    for (auto j = 0; j < number_of_threads; ++j) {40      threads.push_back(support::make_test_thread([&a, &started_num, &done, &wait_done_num] {41        started_num.fetch_add(1, std::memory_order::relaxed);42 43        a.wait(T(1));44        wait_done_num.fetch_add(1, std::memory_order::relaxed);45 46        // likely to fail if wait did not block47        assert(done);48      }));49    }50 51    while (started_num.load(std::memory_order::relaxed) != number_of_threads) {52      std::this_thread::yield();53    }54 55    std::this_thread::sleep_for(std::chrono::milliseconds(1));56 57    done = true;58    a.store(T(3));59    a.notify_all();60 61    // notify_all should unblock all the threads so that the loop below won't stuck62    while (wait_done_num.load(std::memory_order::relaxed) != number_of_threads) {63      std::this_thread::yield();64    }65 66    for (auto& thread : threads) {67      thread.join();68    }69 70    ASSERT_NOEXCEPT(a.notify_all());71  }72};73 74int main(int, char**) {75  TestEachAtomicType<TestNotifyAll>()();76  return 0;77}78