Loading...
Searching...
No Matches
queue_mpmc_mutex.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2018 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_MPMC_QUEUE_MUTEX_INCLUDED
#define ETL_MPMC_QUEUE_MUTEX_INCLUDED
#include "platform.h"
#include "mutex.h"
#if ETL_HAS_MUTEX
#include "alignment.h"
#include "integral_limits.h"
#include "memory_model.h"
#include "parameter_type.h"
#include "placement_new.h"
#include "utility.h"
#include <stddef.h>
#include <stdint.h>
namespace etl
{
//***************************************************************************
//***************************************************************************
class queue_mpmc_exception : public exception
{
public:
queue_mpmc_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class queue_mpmc_empty : public queue_mpmc_exception
{
public:
queue_mpmc_empty(string_type file_name_, numeric_type line_number_)
: queue_mpmc_exception(ETL_ERROR_TEXT("queue_mpmc_mutex:empty", ETL_QUEUE_MPMC_MUTEX_FILE_ID"A"), file_name_, line_number_)
{
}
};
template <size_t MEMORY_MODEL = etl::memory_model::MEMORY_MODEL_LARGE>
class queue_mpmc_mutex_base
{
public:
typedef typename etl::size_type_lookup<MEMORY_MODEL>::type size_type;
//*************************************************************************
//*************************************************************************
size_type capacity() const
{
return MAX_SIZE;
}
//*************************************************************************
//*************************************************************************
size_type max_size() const
{
return MAX_SIZE;
}
protected:
queue_mpmc_mutex_base(size_type max_size_)
: write_index(0)
, read_index(0)
, current_size(0)
, MAX_SIZE(max_size_)
{
}
//*************************************************************************
//*************************************************************************
static size_type get_next_index(size_type index, size_type maximum)
{
++index;
if (index == maximum) ETL_UNLIKELY
{
index = 0;
}
return index;
}
size_type write_index;
size_type read_index;
size_type current_size;
const size_type MAX_SIZE;
//*************************************************************************
//*************************************************************************
#if defined(ETL_POLYMORPHIC_MPMC_QUEUE_MUTEX) || defined(ETL_POLYMORPHIC_CONTAINERS)
public:
virtual ~queue_mpmc_mutex_base() {}
#else
protected:
~queue_mpmc_mutex_base() {}
#endif
};
//***************************************************************************
//***************************************************************************
template <typename T, const size_t MEMORY_MODEL = etl::memory_model::MEMORY_MODEL_LARGE>
class iqueue_mpmc_mutex : public queue_mpmc_mutex_base<MEMORY_MODEL>
{
private:
typedef etl::queue_mpmc_mutex_base<MEMORY_MODEL> base_t;
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
#if ETL_USING_CPP11
typedef T&& rvalue_reference;
#endif
typedef typename base_t::size_type size_type;
using base_t::current_size;
using base_t::get_next_index;
using base_t::MAX_SIZE;
using base_t::read_index;
using base_t::write_index;
//*************************************************************************
//*************************************************************************
bool push(const_reference value)
{
access.lock();
bool result = push_implementation(value);
access.unlock();
return result;
}
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
bool push(rvalue_reference value)
{
access.lock();
bool result = push_implementation(etl::move(value));
access.unlock();
return result;
}
#endif
#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_QUEUE_MPMC_MUTEX_FORCE_CPP03_IMPLEMENTATION)
//*************************************************************************
//*************************************************************************
template <typename... Args>
bool emplace(Args&&... args)
{
access.lock();
bool result = emplace_implementation(etl::forward<Args>(args)...);
access.unlock();
return result;
}
#else
//*************************************************************************
//*************************************************************************
bool emplace()
{
access.lock();
bool result = emplace_implementation();
access.unlock();
return result;
}
//*************************************************************************
//*************************************************************************
template <typename T1>
bool emplace(const T1& value1)
{
access.lock();
bool result = emplace_implementation(value1);
access.unlock();
return result;
}
//*************************************************************************
//*************************************************************************
template <typename T1, typename T2>
bool emplace(const T1& value1, const T2& value2)
{
access.lock();
bool result = emplace_implementation(value1, value2);
access.unlock();
return result;
}
//*************************************************************************
//*************************************************************************
template <typename T1, typename T2, typename T3>
bool emplace(const T1& value1, const T2& value2, const T3& value3)
{
access.lock();
bool result = emplace_implementation(value1, value2, value3);
access.unlock();
return result;
}
//*************************************************************************
//*************************************************************************
template <typename T1, typename T2, typename T3, typename T4>
bool emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
{
access.lock();
bool result = emplace_implementation(value1, value2, value3, value4);
access.unlock();
return result;
}
#endif
//*************************************************************************
//*************************************************************************
bool pop(reference value)
{
access.lock();
bool result = pop_implementation(value);
access.unlock();
return result;
}
//*************************************************************************
//*************************************************************************
bool pop()
{
access.lock();
bool result = pop_implementation();
access.unlock();
return result;
}
//*************************************************************************
//*************************************************************************
reference front()
{
#if ETL_CHECKING_EXTRA
access.lock();
if (current_size != 0)
{
reference inner_result = front_implementation();
access.unlock();
return inner_result;
}
else
{
access.unlock();
ETL_ASSERT_FAIL(ETL_ERROR(queue_mpmc_empty));
// fall through to return something to satisfy the compiler, even
// though this should never be reached due to undefined behaviour.
}
#endif
access.lock();
reference result = front_implementation();
access.unlock();
return result;
}
//*************************************************************************
//*************************************************************************
const_reference front() const
{
#if ETL_CHECKING_EXTRA
access.lock();
if (current_size != 0)
{
const_reference inner_result = front_implementation();
access.unlock();
return inner_result;
}
else
{
access.unlock();
ETL_ASSERT_FAIL(ETL_ERROR(queue_mpmc_empty));
// fall through to return something to satisfy the compiler, even
// though this should never be reached due to undefined behaviour.
}
#endif
access.lock();
const_reference result = front_implementation();
access.unlock();
return result;
}
//*************************************************************************
//*************************************************************************
void clear()
{
access.lock();
if ETL_IF_CONSTEXPR (etl::is_trivially_destructible<T>::value)
{
this->write_index = 0;
this->read_index = 0;
this->current_size = 0;
}
else
{
while (pop_implementation())
{
// Do nothing.
}
}
access.unlock();
}
//*************************************************************************
//*************************************************************************
bool empty() const
{
access.lock();
size_type result = (current_size == 0);
access.unlock();
return result;
}
//*************************************************************************
//*************************************************************************
bool full() const
{
access.lock();
size_type result = (current_size == MAX_SIZE);
access.unlock();
return result;
}
//*************************************************************************
//*************************************************************************
size_type size() const
{
access.lock();
size_type result = current_size;
access.unlock();
return result;
}
//*************************************************************************
//*************************************************************************
size_type available() const
{
access.lock();
size_type result = MAX_SIZE - current_size;
access.unlock();
return result;
}
protected:
//*************************************************************************
//*************************************************************************
iqueue_mpmc_mutex(T* p_buffer_, size_type max_size_)
: base_t(max_size_)
, p_buffer(p_buffer_)
{
}
private:
//*************************************************************************
//*************************************************************************
bool push_implementation(const_reference value)
{
if (current_size != MAX_SIZE)
{
::new (&p_buffer[write_index]) T(value);
write_index = get_next_index(write_index, MAX_SIZE);
++current_size;
return true;
}
// Queue is full.
return false;
}
#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_QUEUE_MPMC_MUTEX_FORCE_CPP03_IMPLEMENTATION)
//*************************************************************************
//*************************************************************************
bool push_implementation(rvalue_reference value)
{
if (current_size != MAX_SIZE)
{
::new (&p_buffer[write_index]) T(etl::move(value));
write_index = get_next_index(write_index, MAX_SIZE);
++current_size;
return true;
}
// Queue is full.
return false;
}
#endif
#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_QUEUE_MPMC_MUTEX_FORCE_CPP03_IMPLEMENTATION)
//*************************************************************************
//*************************************************************************
template <typename... Args>
bool emplace_implementation(Args&&... args)
{
if (current_size != MAX_SIZE)
{
::new (&p_buffer[write_index]) T(etl::forward<Args>(args)...);
write_index = get_next_index(write_index, MAX_SIZE);
++current_size;
return true;
}
// Queue is full.
return false;
}
#else
//*************************************************************************
//*************************************************************************
bool emplace_implementation()
{
if (current_size != MAX_SIZE)
{
::new (&p_buffer[write_index]) T();
write_index = get_next_index(write_index, MAX_SIZE);
++current_size;
return true;
}
// Queue is full.
return false;
}
//*************************************************************************
template <typename T1>
bool emplace_implementation(const T1& value1)
{
if (current_size != MAX_SIZE)
{
::new (&p_buffer[write_index]) T(value1);
write_index = get_next_index(write_index, MAX_SIZE);
++current_size;
return true;
}
// Queue is full.
return false;
}
//*************************************************************************
//*************************************************************************
template <typename T1, typename T2>
bool emplace_implementation(const T1& value1, const T2& value2)
{
if (current_size != MAX_SIZE)
{
::new (&p_buffer[write_index]) T(value1, value2);
write_index = get_next_index(write_index, MAX_SIZE);
++current_size;
return true;
}
// Queue is full.
return false;
}
//*************************************************************************
//*************************************************************************
template <typename T1, typename T2, typename T3>
bool emplace_implementation(const T1& value1, const T2& value2, const T3& value3)
{
if (current_size != MAX_SIZE)
{
::new (&p_buffer[write_index]) T(value1, value2, value3);
write_index = get_next_index(write_index, MAX_SIZE);
++current_size;
return true;
}
// Queue is full.
return false;
}
//*************************************************************************
//*************************************************************************
template <typename T1, typename T2, typename T3, typename T4>
bool emplace_implementation(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
{
if (current_size != MAX_SIZE)
{
::new (&p_buffer[write_index]) T(value1, value2, value3, value4);
write_index = get_next_index(write_index, MAX_SIZE);
++current_size;
return true;
}
// Queue is full.
return false;
}
#endif
//*************************************************************************
//*************************************************************************
bool pop_implementation(reference value)
{
if (current_size == 0)
{
// Queue is empty
return false;
}
#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_QUEUE_LOCKABLE_FORCE_CPP03_IMPLEMENTATION)
value = etl::move(p_buffer[read_index]);
#else
value = p_buffer[read_index];
#endif
p_buffer[read_index].~T();
read_index = get_next_index(read_index, MAX_SIZE);
--current_size;
return true;
}
//*************************************************************************
//*************************************************************************
bool pop_implementation()
{
if (current_size == 0)
{
// Queue is empty
return false;
}
p_buffer[read_index].~T();
read_index = get_next_index(read_index, MAX_SIZE);
--current_size;
return true;
}
//*************************************************************************
//*************************************************************************
reference front_implementation()
{
return p_buffer[read_index];
}
//*************************************************************************
//*************************************************************************
const_reference front_implementation() const
{
return p_buffer[read_index];
}
// Disable copy construction and assignment.
iqueue_mpmc_mutex(const iqueue_mpmc_mutex&);
iqueue_mpmc_mutex& operator=(const iqueue_mpmc_mutex&);
T* p_buffer;
mutable etl::mutex access;
};
//***************************************************************************
//***************************************************************************
template <typename T, size_t SIZE, const size_t MEMORY_MODEL = etl::memory_model::MEMORY_MODEL_LARGE>
class queue_mpmc_mutex : public etl::iqueue_mpmc_mutex<T, MEMORY_MODEL>
{
private:
typedef etl::iqueue_mpmc_mutex<T, MEMORY_MODEL> base_t;
public:
typedef typename base_t::size_type size_type;
ETL_STATIC_ASSERT((SIZE <= etl::integral_limits<size_type>::max), "Size too large for memory model");
static ETL_CONSTANT size_type MAX_SIZE = size_type(SIZE);
//*************************************************************************
//*************************************************************************
queue_mpmc_mutex()
: base_t(reinterpret_cast<T*>(&buffer[0]), MAX_SIZE)
{
}
//*************************************************************************
//*************************************************************************
~queue_mpmc_mutex()
{
base_t::clear();
}
private:
queue_mpmc_mutex(const queue_mpmc_mutex&) ETL_DELETE;
queue_mpmc_mutex& operator=(const queue_mpmc_mutex&) ETL_DELETE;
#if ETL_USING_CPP11
queue_mpmc_mutex(queue_mpmc_mutex&&) = delete;
queue_mpmc_mutex& operator=(queue_mpmc_mutex&&) = delete;
#endif
typename etl::aligned_storage<sizeof(T), etl::alignment_of<T>::value>::type buffer[MAX_SIZE];
};
template <typename T, size_t SIZE, const size_t MEMORY_MODEL>
ETL_CONSTANT typename queue_mpmc_mutex<T, SIZE, MEMORY_MODEL>::size_type queue_mpmc_mutex<T, SIZE, MEMORY_MODEL>::MAX_SIZE;
} // namespace etl
#endif
#endif
alignment.h
etl::exception
Definition exception.h:59
integral_limits.h
memory_model.h
etl
Definition absolute.h:40
etl::mutex
std::mutex mutex
This mutex class is implemented using std::mutex.
Definition mutex_std.h:42
etl::size
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1434
parameter_type.h
placement_new.h
platform.h
utility.h