CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
83 lines
2.2 KiB
C++
83 lines
2.2 KiB
C++
//===- LazyAtomicPointerTest.cpp ------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/ADT/LazyAtomicPointer.h"
|
|
#include "llvm/Config/llvm-config.h"
|
|
#include "llvm/Support/ThreadPool.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
|
|
TEST(LazyAtomicPointer, loadOrGenerate) {
|
|
int Value = 0;
|
|
LazyAtomicPointer<int> Ptr;
|
|
DefaultThreadPool Threads;
|
|
for (unsigned I = 0; I < 4; ++I)
|
|
Threads.async([&]() {
|
|
Ptr.loadOrGenerate([&]() {
|
|
// Make sure this is only called once.
|
|
static std::atomic<bool> Once(false);
|
|
bool Current = false;
|
|
EXPECT_TRUE(Once.compare_exchange_strong(Current, true));
|
|
return &Value;
|
|
});
|
|
});
|
|
|
|
Threads.wait();
|
|
EXPECT_EQ(Ptr.load(), &Value);
|
|
}
|
|
|
|
#if (LLVM_ENABLE_THREADS)
|
|
TEST(LazyAtomicPointer, BusyState) {
|
|
int Value = 0;
|
|
LazyAtomicPointer<int> Ptr;
|
|
DefaultThreadPool Threads;
|
|
|
|
std::mutex BusyLock, EndLock;
|
|
std::condition_variable Busy, End;
|
|
bool IsBusy = false, IsEnd = false;
|
|
Threads.async([&]() {
|
|
Ptr.loadOrGenerate([&]() {
|
|
// Notify busy state.
|
|
{
|
|
std::lock_guard<std::mutex> Lock(BusyLock);
|
|
IsBusy = true;
|
|
}
|
|
Busy.notify_all();
|
|
std::unique_lock<std::mutex> LEnd(EndLock);
|
|
// Wait for end state.
|
|
End.wait(LEnd, [&]() { return IsEnd; });
|
|
return &Value;
|
|
});
|
|
});
|
|
|
|
// Wait for busy state.
|
|
std::unique_lock<std::mutex> LBusy(BusyLock);
|
|
Busy.wait(LBusy, [&]() { return IsBusy; });
|
|
int *ExistingValue = nullptr;
|
|
// Busy state will not exchange the value.
|
|
EXPECT_FALSE(Ptr.compare_exchange_weak(ExistingValue, nullptr));
|
|
// Busy state return nullptr on load/compare_exchange_weak.
|
|
EXPECT_EQ(ExistingValue, nullptr);
|
|
EXPECT_EQ(Ptr.load(), nullptr);
|
|
|
|
// End busy state.
|
|
{
|
|
std::lock_guard<std::mutex> Lock(EndLock);
|
|
IsEnd = true;
|
|
}
|
|
End.notify_all();
|
|
Threads.wait();
|
|
EXPECT_EQ(Ptr.load(), &Value);
|
|
}
|
|
#endif
|
|
|
|
} // namespace
|