2015-05-14 16:05:04 -04:00
|
|
|
/// @file
|
|
|
|
/// @brief QP::QK_ceilingPrio_, QP::QK::mutexLock(), and QP::QK::mutexUnlock()
|
2014-04-13 21:35:34 -04:00
|
|
|
/// definitions.
|
2015-05-14 16:05:04 -04:00
|
|
|
/// @ingroup qk
|
|
|
|
/// @cond
|
2014-04-13 21:35:34 -04:00
|
|
|
///***************************************************************************
|
2014-09-22 11:48:11 -04:00
|
|
|
/// Product: QK/C++
|
2015-05-14 16:05:04 -04:00
|
|
|
/// Last updated for version 5.4.0
|
|
|
|
/// Last updated on 2015-03-14
|
2014-04-13 21:35:34 -04:00
|
|
|
///
|
|
|
|
/// Q u a n t u m L e a P s
|
|
|
|
/// ---------------------------
|
|
|
|
/// innovating embedded systems
|
|
|
|
///
|
|
|
|
/// Copyright (C) Quantum Leaps, www.state-machine.com.
|
|
|
|
///
|
|
|
|
/// This program is open source software: you can redistribute it and/or
|
|
|
|
/// modify it under the terms of the GNU General Public License as published
|
|
|
|
/// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
/// (at your option) any later version.
|
|
|
|
///
|
|
|
|
/// Alternatively, this program may be distributed and modified under the
|
|
|
|
/// terms of Quantum Leaps commercial licenses, which expressly supersede
|
|
|
|
/// the GNU General Public License and are specifically designed for
|
|
|
|
/// licensees interested in retaining the proprietary status of their code.
|
|
|
|
///
|
|
|
|
/// This program is distributed in the hope that it will be useful,
|
|
|
|
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
/// GNU General Public License for more details.
|
|
|
|
///
|
|
|
|
/// You should have received a copy of the GNU General Public License
|
|
|
|
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
///
|
|
|
|
/// Contact information:
|
|
|
|
/// Web: www.state-machine.com
|
|
|
|
/// Email: info@state-machine.com
|
|
|
|
///***************************************************************************
|
2015-05-14 16:05:04 -04:00
|
|
|
/// @endcond
|
2014-04-13 21:35:34 -04:00
|
|
|
|
|
|
|
#define QP_IMPL // this is QF/QK implementation
|
|
|
|
#include "qf_port.h" // QF port
|
|
|
|
#include "qk_pkg.h" // QK package-scope internal interface
|
|
|
|
#ifdef Q_SPY // QS software tracing enabled?
|
|
|
|
#include "qs_port.h" // include QS port
|
|
|
|
#else
|
|
|
|
#include "qs_dummy.h" // disable the QS software tracing
|
|
|
|
#endif // Q_SPY
|
2012-08-14 18:00:48 -04:00
|
|
|
|
|
|
|
#ifdef QK_NO_MUTEX
|
|
|
|
#error "qk_mutex.cpp included in the build when QK_NO_MUTEX defined"
|
|
|
|
#endif
|
|
|
|
|
2014-04-13 21:35:34 -04:00
|
|
|
// Package-scope objects *****************************************************
|
2012-08-14 18:00:48 -04:00
|
|
|
extern "C" {
|
2014-04-13 21:35:34 -04:00
|
|
|
uint_fast8_t volatile QK_ceilingPrio_; // ceiling priority of a mutex
|
2012-08-14 18:00:48 -04:00
|
|
|
}
|
|
|
|
|
2013-10-10 20:01:51 -04:00
|
|
|
namespace QP {
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2014-04-13 21:35:34 -04:00
|
|
|
//****************************************************************************
|
2015-05-14 16:05:04 -04:00
|
|
|
/// @description
|
2014-04-13 21:35:34 -04:00
|
|
|
/// Lock the QK scheduler up to the given priority level.
|
|
|
|
///
|
2015-05-14 16:05:04 -04:00
|
|
|
/// @param[in] prioCeiling priority ceiling to lock the mutex
|
2014-04-13 21:35:34 -04:00
|
|
|
///
|
2015-05-14 16:05:04 -04:00
|
|
|
/// @returns the previous value of the mutex priority ceiling
|
2014-04-13 21:35:34 -04:00
|
|
|
///
|
2015-05-14 16:05:04 -04:00
|
|
|
/// @note This function should be always paired with QP::QK::mutexUnlock().
|
2014-04-13 21:35:34 -04:00
|
|
|
/// The code between QP::QK::mutexLock() and QP::QK::mutexUnlock() should
|
|
|
|
/// be kept to the minimum.
|
|
|
|
///
|
2015-05-14 16:05:04 -04:00
|
|
|
/// @usage
|
|
|
|
/// @include qk_mux.cpp
|
2014-04-13 21:35:34 -04:00
|
|
|
///
|
|
|
|
QMutex QK::mutexLock(uint_fast8_t const prioCeiling) {
|
2012-08-14 18:00:48 -04:00
|
|
|
QF_CRIT_STAT_
|
|
|
|
QF_CRIT_ENTRY_();
|
2014-04-13 21:35:34 -04:00
|
|
|
QMutex mutex = QK_ceilingPrio_; // original QK priority ceiling
|
2012-08-14 18:00:48 -04:00
|
|
|
if (QK_ceilingPrio_ < prioCeiling) {
|
2014-04-13 21:35:34 -04:00
|
|
|
QK_ceilingPrio_ = prioCeiling; // raise the QK priority ceiling
|
2012-08-14 18:00:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QS_BEGIN_NOCRIT_(QS_QK_MUTEX_LOCK,
|
|
|
|
static_cast<void *>(0), static_cast<void *>(0))
|
2014-04-13 21:35:34 -04:00
|
|
|
QS_TIME_(); // timestamp
|
|
|
|
QS_U8_(mutex); // the original priority
|
|
|
|
QS_U8_(QK_ceilingPrio_); // the current priority ceiling
|
2012-08-14 18:00:48 -04:00
|
|
|
QS_END_NOCRIT_()
|
|
|
|
|
|
|
|
QF_CRIT_EXIT_();
|
|
|
|
return mutex;
|
|
|
|
}
|
2014-04-13 21:35:34 -04:00
|
|
|
|
|
|
|
//****************************************************************************
|
2015-05-14 16:05:04 -04:00
|
|
|
/// @description
|
2014-04-13 21:35:34 -04:00
|
|
|
/// Unlock the QK scheduler up to the saved priority level.
|
|
|
|
///
|
2015-05-14 16:05:04 -04:00
|
|
|
/// @param[in] mutex previous priority level to unlock the mutex
|
2014-04-13 21:35:34 -04:00
|
|
|
///
|
2015-05-14 16:05:04 -04:00
|
|
|
/// @description
|
|
|
|
/// @note This function should be always paired with QP::QK::mutexLock().
|
2014-04-13 21:35:34 -04:00
|
|
|
/// The code between QP::QK::mutexLock() and QP::QK::mutexUnlock() should
|
|
|
|
/// be kept to the minimum.
|
|
|
|
///
|
2015-05-14 16:05:04 -04:00
|
|
|
/// @usage
|
|
|
|
/// @include qk_mux.cpp
|
2014-04-13 21:35:34 -04:00
|
|
|
///
|
2012-08-14 18:00:48 -04:00
|
|
|
void QK::mutexUnlock(QMutex mutex) {
|
|
|
|
QF_CRIT_STAT_
|
|
|
|
QF_CRIT_ENTRY_();
|
|
|
|
|
|
|
|
QS_BEGIN_NOCRIT_(QS_QK_MUTEX_UNLOCK,
|
|
|
|
static_cast<void *>(0), static_cast<void *>(0))
|
2014-04-13 21:35:34 -04:00
|
|
|
QS_TIME_(); // timestamp
|
|
|
|
QS_U8_(mutex); // the original priority
|
|
|
|
QS_U8_(QK_ceilingPrio_); // the current priority ceiling
|
2012-08-14 18:00:48 -04:00
|
|
|
QS_END_NOCRIT_()
|
|
|
|
|
|
|
|
if (QK_ceilingPrio_ > mutex) {
|
2014-04-13 21:35:34 -04:00
|
|
|
QK_ceilingPrio_ = mutex; // restore the saved priority ceiling
|
|
|
|
mutex = QK_schedPrio_(); // reuse 'mutex' to hold priority
|
|
|
|
if (mutex != static_cast<uint_fast8_t>(0)) {
|
2012-08-14 18:00:48 -04:00
|
|
|
QK_sched_(mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QF_CRIT_EXIT_();
|
|
|
|
}
|
|
|
|
|
2014-04-13 21:35:34 -04:00
|
|
|
} // namespace QP
|
2013-10-10 20:01:51 -04:00
|
|
|
|