1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-05-02 08:57:44 +00:00
sapphire/src/common/Util/LockedQueue.h

130 lines
2.5 KiB
C
Raw Normal View History

2017-08-08 13:53:47 +02:00
#pragma once
2017-08-08 13:53:47 +02:00
#include <memory>
#include <thread>
#include <mutex>
#include <queue>
2018-10-24 17:58:57 +02:00
#include <algorithm>
#include <utility>
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
namespace Core
{
2018-10-28 21:53:21 +01:00
template< class T >
class LockedQueue
{
public:
LockedQueue();
2018-10-28 21:53:21 +01:00
~LockedQueue();
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
T pop();
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
//we can pass this in by reference, instead of copying
void push( const T object );
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
//we can pass this in by reference
//this will push it onto the queue, and swap the object
// with a default-constructed T at the same time.
void push_swap( T& object );
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
void push_reset( T& object );
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
std::size_t size();
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
protected:
std::queue< T > m_queue;
std::mutex m_mutex;
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
};
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
template< class T >
LockedQueue< T >::LockedQueue()
{
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
}
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
template< class T >
std::size_t LockedQueue< T >::size()
{
std::lock_guard< std::mutex > lock( m_mutex );
return m_queue.size();
}
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
template< class T >
LockedQueue< T >::~LockedQueue()
{
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
}
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
template< class T >
T LockedQueue< T >::pop()
{
2018-10-28 21:53:21 +01:00
std::lock_guard< std::mutex > lock( m_mutex );
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
if( m_queue.empty() )
{
return T();
}
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
T result = m_queue.front();
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
m_queue.pop();
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
return result;
}
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
template< class T >
void LockedQueue< T >::push( const T object )
{
std::lock_guard< std::mutex > lock( m_mutex );
m_queue.push( object );
}
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
template< class T >
void LockedQueue< T >::push_swap( T& object )
{
std::lock_guard< std::mutex > lock( m_mutex );
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
m_queue.push( object );
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
T default_ctored_object = T();
//this is a special swap that will do a legit naive swap normally,
// except if there exists a function called T::swap(), which is
// specialized and possibly faster.
std::swap( object, default_ctored_object );
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
//default_ctored_object is now the value of object, and it will go out
// of scope here. In the case that T is a shared_ptr of some kind,
// this will allow that the object on the queue is the *last* shared_ptr
// in existance by the time this function returns.
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
}
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
template< class T >
void LockedQueue< T >::push_reset( T& object )
{
std::lock_guard< std::mutex > lock( m_mutex );
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
m_queue.push( object );
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
T default_ctored_object = T();
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
object.reset();
2017-08-08 13:53:47 +02:00
2018-10-28 21:53:21 +01:00
//default_ctored_object is now the value of object, and it will go out
// of scope here. In the case that T is a shared_ptr of some kind,
// this will allow that the object on the queue is the *last* shared_ptr
// in existance by the time this function returns.
}
2017-08-08 13:53:47 +02:00
2018-10-24 17:58:57 +02:00
}