<commit_before>/**
 * \file
 * \brief Semaphore class implementation
 *
 * \author Copyright (C) 2014 <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 2014-11-18
 */

#include "distortos/Semaphore.hpp"

#include "distortos/scheduler/getScheduler.hpp"
#include "distortos/scheduler/Scheduler.hpp"

#include "distortos/architecture/InterruptMaskingLock.hpp"

#include <cerrno>

namespace distortos
{

/*---------------------------------------------------------------------------------------------------------------------+
| public functions
+---------------------------------------------------------------------------------------------------------------------*/

Semaphore::Semaphore(int value) :
		blockedList_{scheduler::getScheduler().getThreadControlBlockListAllocator(),
				scheduler::ThreadControlBlock::State::BlockedOnSemaphore},
		value_{value}
{

}

Semaphore::~Semaphore()
{

}

int Semaphore::post()
{
	architecture::InterruptMaskingLock interruptMaskingLock;

	if (value_ == std::numeric_limits<decltype(value_)>::max())
		return EOVERFLOW;

	++value_;

	if (blockedList_.empty() == false)
		scheduler::getScheduler().unblock(blockedList_.begin());

	return 0;
}

int Semaphore::tryWait()
{
	architecture::InterruptMaskingLock interruptMaskingLock;

	if (value_ <= 0)	// lock not possible?
		return EAGAIN;

	--value_;

	return 0;
}

int Semaphore::wait()
{
	architecture::InterruptMaskingLock interruptMaskingLock;

	--value_;

	if (value_ >= 0)
		return 0;

	scheduler::getScheduler().block(blockedList_);

	return 0;
}
<commit_msg>Semaphore: implement Semaphore::tryWait() with Semaphore::tryWaitInternal()<commit_after>/**
 * \file
 * \brief Semaphore class implementation
 *
 * \author Copyright (C) 2014 <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 2014-11-18
 */

#include "distortos/Semaphore.hpp"

#include "distortos/scheduler/getScheduler.hpp"
#include "distortos/scheduler/Scheduler.hpp"

#include "distortos/architecture/InterruptMaskingLock.hpp"

#include <cerrno>

namespace distortos
{

/*---------------------------------------------------------------------------------------------------------------------+
| public functions
+---------------------------------------------------------------------------------------------------------------------*/

Semaphore::Semaphore(int value) :
		blockedList_{scheduler::getScheduler().getThreadControlBlockListAllocator(),
				scheduler::ThreadControlBlock::State::BlockedOnSemaphore},
		value_{value}
{

}

Semaphore::~Semaphore()
{

}

int Semaphore::post()
{
	architecture::InterruptMaskingLock interruptMaskingLock;

	if (value_ == std::numeric_limits<decltype(value_)>::max())
		return EOVERFLOW;

	++value_;

	if (blockedList_.empty() == false)
		scheduler::getScheduler().unblock(blockedList_.begin());

	return 0;
}

int Semaphore::tryWait()
{
	architecture::InterruptMaskingLock interruptMaskingLock;
	return tryWaitInternal();
}

int Semaphore::wait()
{
	architecture::InterruptMaskingLock interruptMaskingLock;

	--value_;

	if (value_ >= 0)
		return 0;

	scheduler::getScheduler().block(blockedList_);

	return 0;
}

{'fraction_non_alphanumeric': 0.24937238493723848, 'fraction_numerical': 0.011715481171548118, 'mean_word_length': 2.670419651995906, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 11, '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': '349562', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1063, 'n_words': 225}