carb/thread/Futex.h

File members: carb/thread/Futex.h

// Copyright (c) 2020-2023, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//

#pragma once

#include "FutexImpl.h"

#include <atomic>

namespace carb
{
namespace thread
{

namespace futex
{

template <class T>
inline void wait(const std::atomic<T>& val, T compare) CARB_NOEXCEPT
{
    detail::Futex<T>::wait(val, compare);
}

template <class T, class Rep, class Period>
inline bool wait_for(const std::atomic<T>& val, T compare, const std::chrono::duration<Rep, Period>& duration) CARB_NOEXCEPT
{
    return detail::Futex<T>::wait_for(val, compare, duration);
}

template <class T, class Clock, class Duration>
inline bool wait_until(const std::atomic<T>& val,
                       T compare,
                       const std::chrono::time_point<Clock, Duration>& time_point) CARB_NOEXCEPT
{
    return detail::Futex<T>::wait_until(val, compare, time_point);
}

template <class T>
inline void wake(std::atomic<T>& val, unsigned count, unsigned maxCount = unsigned(INT_MAX)) CARB_NOEXCEPT
{
    if (count >= maxCount)
        detail::Futex<T>::notify_all(val);
    else
        detail::Futex<T>::notify_n(val, count);
}

template <class T>
inline void wake_one(std::atomic<T>& val) CARB_NOEXCEPT
{
    detail::Futex<T>::notify_one(val);
}

template <class T>
inline void wake_all(std::atomic<T>& val) CARB_NOEXCEPT
{
    detail::Futex<T>::notify_all(val);
}

} // namespace futex
} // namespace thread
} // namespace carb