<commit_before>/**
 * \file
 * \brief MessageQueueBase class implementation
 *
 * \author Copyright (C) 2015 <NAME> http://www.distortec.com http://www.freddiechopin.info
 *
 * \par License
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
 * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * \date 2015-01-14
 */

#include "distortos/synchronization/MessageQueueBase.hpp"

#include "distortos/architecture/InterruptMaskingLock.hpp"

namespace distortos
{

namespace synchronization
{

/*---------------------------------------------------------------------------------------------------------------------+
| private functions
+---------------------------------------------------------------------------------------------------------------------*/

MessageQueueBase::MessageQueueBase(const size_t maxElements) :
		popSemaphore_{0, maxElements},
		pushSemaphore_{maxElements, maxElements},
		pool_{},
		poolAllocator_{pool_},
		entryList_{poolAllocator_},
		freeEntryList_{poolAllocator_}
{<commit_msg>MessageQueueBase: add local PushInternalFunctor class<commit_after>/**
 * \file
 * \brief MessageQueueBase class implementation
 *
 * \author Copyright (C) 2015 <NAME> http://www.distortec.com http://www.freddiechopin.info
 *
 * \par License
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
 * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * \date 2015-01-14
 */

#include "distortos/synchronization/MessageQueueBase.hpp"

#include "distortos/architecture/InterruptMaskingLock.hpp"

namespace distortos
{

namespace synchronization
{

namespace
{

/*---------------------------------------------------------------------------------------------------------------------+
| local types
+---------------------------------------------------------------------------------------------------------------------*/

/// PushInternalFunctor class is a MessageQueueBase::InternalFunctor used for pushing of elements to the queue
class PushInternalFunctor : public MessageQueueBase::InternalFunctor
{
public:

	/**
	 * \brief PushInternalFunctor's constructor
	 *
	 * \param [in] priority is the priority of new element
	 * \param [in] functor is a reference to Functor which will execute actions related to pushing - it will get a
	 * pointer to storage for element
	 */

	constexpr PushInternalFunctor(const uint8_t priority, const MessageQueueBase::Functor& functor) :
			functor_(functor),
			priority_{priority}
	{

	}

	/**
	 * \brief PushInternalFunctor's function call operator
	 *
	 * Pops one entry from \a freeEntryList, passes the storage to \a functor_ and pushes this entry to \a entryList.
	 *
	 * \param [in] entryList is a reference to EntryList of MessageQueueBase
	 * \param [in] freeEntryList is a reference to FreeEntryList of MessageQueueBase
	 */

	virtual void operator()(MessageQueueBase::EntryList& entryList, MessageQueueBase::FreeEntryList& freeEntryList)
			const override
	{
		auto entry = *freeEntryList.begin();
		freeEntryList.pop_front();

		entry.priority = priority_;

		functor_(entry.storage);

		entryList.sortedEmplace(entry);
	}

private:

	/// reference to Functor which will execute actions related to pushing
	const MessageQueueBase::Functor& functor_;

	/// priority of new element
	const uint8_t priority_;
};

}	// namespace

/*---------------------------------------------------------------------------------------------------------------------+
| private functions
+---------------------------------------------------------------------------------------------------------------------*/

MessageQueueBase::MessageQueueBase(const size_t maxElements) :
		popSemaphore_{0, maxElements},
		pushSemaphore_{maxElements, maxElements},
		pool_{},
		poolAllocator_{pool_},
		entryList_{poolAllocator_},
		freeEntryList_{poolAllocator_}
{
{'fraction_non_alphanumeric': 0.25869017632241814, 'fraction_numerical': 0.00906801007556675, 'mean_word_length': 2.047582501918649, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6709377', 'n_tokens_mistral': 1065, 'n_tokens_neox': 969, 'n_words': 296}