/* * Copyright (c) Contributors to the Open 3D Engine Project. * For complete copyright and license terms please see the LICENSE at the root of this distribution. * * SPDX-License-Identifier: Apache-2.0 OR MIT * */ #pragma once #include "StlUtils.h" #include #include #include namespace CryMT { ////////////////////////////////////////////////////////////////////////// // Thread Safe wrappers on the standard STL containers. ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// // Multi-Thread safe queue container. ////////////////////////////////////////////////////////////////////////// template > class queue { public: typedef T value_type; typedef std::queue> container_type; typedef AZStd::lock_guard AutoLock; ////////////////////////////////////////////////////////////////////////// // std::queue interface ////////////////////////////////////////////////////////////////////////// const T& front() const { AutoLock lock(m_cs); return v.front(); }; const T& back() const { AutoLock lock(m_cs); return v.back(); } void push(const T& x) { AutoLock lock(m_cs); return v.push(x); }; void reserve(const size_t n) { AutoLock lock(m_cs); v.reserve(n); }; AZStd::recursive_mutex& get_lock() const { return m_cs; } bool empty() const { AutoLock lock(m_cs); return v.empty(); } int size() const { AutoLock lock(m_cs); return v.size(); } void clear() { AutoLock lock(m_cs); v.clear(); } void free_memory() { AutoLock lock(m_cs); stl::free_container(v); } template void sort(const Func& compare_less) { AutoLock lock(m_cs); std::sort(v.begin(), v.end(), compare_less); } ////////////////////////////////////////////////////////////////////////// bool try_pop(T& returnValue) { AutoLock lock(m_cs); if (!v.empty()) { returnValue = v.front(); v.pop(); return true; } return false; }; private: container_type v; mutable AZStd::recursive_mutex m_cs; }; }; // namespace CryMT namespace stl { template void free_container(CryMT::queue& v) { v.free_memory(); } }