60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
|
|
|
#pragma once
|
|
|
|
#include "Engine/Core/Memory/Memory.h"
|
|
#include <ThirdParty/concurrentqueue.h>
|
|
|
|
/// <summary>
|
|
/// The default engine configuration for concurrentqueue.
|
|
/// </summary>
|
|
struct ConcurrentQueueSettings : public moodycamel::ConcurrentQueueDefaultTraits
|
|
{
|
|
// Use bigger blocks
|
|
static const size_t BLOCK_SIZE = 256;
|
|
|
|
// Use default engine memory allocator
|
|
static inline void* malloc(size_t size)
|
|
{
|
|
return Allocator::Allocate((uint64)size);
|
|
}
|
|
|
|
// Use default engine memory allocator
|
|
static inline void free(void* ptr)
|
|
{
|
|
return Allocator::Free(ptr);
|
|
}
|
|
};
|
|
|
|
/// <summary>
|
|
/// Lock-free implementation of thread-safe queue.
|
|
/// Based on: https://github.com/cameron314/concurrentqueue
|
|
/// </summary>
|
|
template<typename T>
|
|
class ConcurrentQueue : public moodycamel::ConcurrentQueue<T, ConcurrentQueueSettings>
|
|
{
|
|
public:
|
|
|
|
typedef moodycamel::ConcurrentQueue<T, ConcurrentQueueSettings> Base;
|
|
|
|
public:
|
|
|
|
/// <summary>
|
|
/// Gets an estimate of the total number of elements currently in the queue.
|
|
/// </summary>
|
|
/// <returns>The items count.</returns>
|
|
FORCE_INLINE int32 Count() const
|
|
{
|
|
return static_cast<int32>(Base::size_approx());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds item to the collection.
|
|
/// </summary>
|
|
/// <param name="item">The item to add.</param>
|
|
FORCE_INLINE void Add(T& item)
|
|
{
|
|
enqueue(item);
|
|
}
|
|
};
|