Files
o3de/Code/Legacy/CryCommon/MultiThread_Containers.h
T
Scott Romero 9a8a411a0b [development] removal of unused and low stakes code related to Cry-threading (#2896)
Removal highlights include:
 - File indexer (used CryThread<>) linked to long gone asset browser
 - Producer/consumer queues from CryMT
 - set/vector/CLocklessPointerQueue containers also from CryMT
 - Cry interlocked linked list and _InterlockedCompareExchange128
 - CryThread type
 - SAtomicVar types
 - CryAutoSet type
 - Various unused lock types
 -- AutoLockModify
 -- AutoLockRead
 -- CryOptionalAutoLock
 -- CryReadModifyLock
 -- CryRWLock
 -- ReadLock
 -- ReadLockCond
 -- WriteAfterReadLock
 - Misc. unused functions
 -- CryInterLockedAdd (not to be confused with CryInterlockedAdd, using a lower case "locked")
 -- CryInterlockedExchange64 (which was only defined for unix platforms)
 -- SpinLock
 -- JobSpinLock
 -- AtomicAdd
 -- JobAtomicAdd

Signed-off-by: AMZN-ScottR <24445312+AMZN-ScottR@users.noreply.github.com>
2021-08-06 13:23:14 -07:00

110 lines
3.6 KiB
C++

/*
* 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
*
*/
#ifndef CRYINCLUDE_CRYCOMMON_MULTITHREAD_CONTAINERS_H
#define CRYINCLUDE_CRYCOMMON_MULTITHREAD_CONTAINERS_H
#pragma once
#include "StlUtils.h"
#include "BitFiddling.h"
#include <queue>
#include <set>
#include <algorithm>
namespace CryMT
{
//////////////////////////////////////////////////////////////////////////
// Thread Safe wrappers on the standard STL containers.
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Multi-Thread safe queue container, can be used instead of std::vector.
//////////////////////////////////////////////////////////////////////////
template <class T, class Alloc = std::allocator<T> >
class queue
{
public:
typedef T value_type;
typedef std::vector<T, Alloc> container_type;
typedef CryAutoCriticalSection 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_back(x); };
void reserve(const size_t n) { AutoLock lock(m_cs); v.reserve(n); };
// classic pop function of queue should not be used for thread safety, use try_pop instead
//void pop() { AutoLock lock(m_cs); return v.erase(v.begin()); };
CryCriticalSection& 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 <class Func>
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.erase(v.begin());
return true;
}
return false;
};
//////////////////////////////////////////////////////////////////////////
bool try_remove(const T& value)
{
AutoLock lock(m_cs);
if (!v.empty())
{
typename container_type::iterator it = std::find(v.begin(), v.end(), value);
if (it != v.end())
{
v.erase(it);
return true;
}
}
return false;
};
template<typename Sizer>
void GetMemoryUsage(Sizer* pSizer) const
{
pSizer->AddObject(v);
}
private:
container_type v;
mutable CryCriticalSection m_cs;
};
}; // namespace CryMT
namespace stl
{
template <typename T>
void free_container(CryMT::queue<T>& v)
{
v.free_memory();
}
}
#endif // CRYINCLUDE_CRYCOMMON_MULTITHREAD_CONTAINERS_H