qpcpp/source/qk_mutex.cpp

173 lines
5.6 KiB
C++
Raw Normal View History

2015-05-14 16:05:04 -04:00
/// @file
2016-05-09 11:44:05 -04:00
/// @brief QP::QMutex::init(), QP::QMutex::lock(), and QP::QMutex::unlock()
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++
2016-05-05 12:22:15 -04:00
/// Last updated for version 5.6.4
2016-05-09 11:44:05 -04:00
/// Last updated on 2016-05-09
2014-04-13 21:35:34 -04:00
///
/// Q u a n t u m L e a P s
/// ---------------------------
/// innovating embedded systems
///
2015-12-31 14:56:37 -05:00
/// Copyright (C) Quantum Leaps, LLC. All rights reserved.
2014-04-13 21:35:34 -04:00
///
/// 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:
2015-12-31 14:56:37 -05:00
/// http://www.state-machine.com
/// mailto:info@state-machine.com
2014-04-13 21:35:34 -04:00
///***************************************************************************
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
2015-12-31 14:56:37 -05:00
#include "qassert.h" // QP assertions
2014-04-13 21:35:34 -04:00
#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
2015-12-31 14:56:37 -05:00
// protection against including this source file in a wrong project
#ifndef qk_h
#error "Source file included in a project NOT based on the QK kernel"
#endif // qk_h
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
2015-12-31 14:56:37 -05:00
Q_DEFINE_THIS_MODULE("qk_mutex")
2016-04-01 13:55:34 -04:00
enum {
MUTEX_UNUSED = 0xFF
};
2014-04-13 21:35:34 -04:00
//****************************************************************************
2015-05-14 16:05:04 -04:00
/// @description
2016-04-01 13:55:34 -04:00
/// Initializes QK priority-ceiling mutex to the specified ceiling priority.
///
/// @param[in] prio ceiling priority of the mutex
2014-04-13 21:35:34 -04:00
///
2016-04-01 13:55:34 -04:00
/// @note
/// A mutex must be initialized before it can be locked or unlocked.
2014-04-13 21:35:34 -04:00
///
2016-05-09 11:44:05 -04:00
/// @sa QP::QMutex::lock(), QP::QMutex::unlock()
2014-04-13 21:35:34 -04:00
///
2015-05-14 16:05:04 -04:00
/// @usage
2016-04-01 13:55:34 -04:00
/// The following example shows how to initialize, lock and unlock QK mutex:
2015-05-14 16:05:04 -04:00
/// @include qk_mux.cpp
2014-04-13 21:35:34 -04:00
///
2016-05-09 11:44:05 -04:00
void QMutex::init(uint_fast8_t const prio) {
2016-04-01 13:55:34 -04:00
m_lockPrio = prio;
m_prevPrio = static_cast<uint_fast8_t>(MUTEX_UNUSED);
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
2016-04-01 13:55:34 -04:00
/// This function locks the QK mutex.
2015-12-31 14:56:37 -05:00
///
2016-04-01 13:55:34 -04:00
/// @note
/// A mutex must be initialized before it can be locked or unlocked.
///
/// @note
2016-05-09 11:44:05 -04:00
/// QP::QMutex::lock() must be always followed by the corresponding
/// QP::QMutex::unlock().
2016-04-01 13:55:34 -04:00
///
2016-05-09 11:44:05 -04:00
/// @sa QP::QMutex::init(), QP::QMutex::unlock()
2014-04-13 21:35:34 -04:00
///
2015-12-31 14:56:37 -05:00
/// @usage
2016-04-01 13:55:34 -04:00
/// The following example shows how to initialize, lock and unlock QK mutex:
2015-12-31 14:56:37 -05:00
/// @include qk_mux.cpp
2014-04-13 21:35:34 -04:00
///
2016-05-09 11:44:05 -04:00
void QMutex::lock(void) {
2015-12-31 14:56:37 -05:00
QF_CRIT_STAT_
QF_CRIT_ENTRY_();
2016-04-01 13:55:34 -04:00
/// @pre scheduler cannot be locked from the ISR context
/// and the mutex must be unused
Q_REQUIRE_ID(700, (!QK_ISR_CONTEXT_())
&& (m_prevPrio == static_cast<uint_fast8_t>(MUTEX_UNUSED)));
m_prevPrio = QK_lockPrio_; // save the previous prio
if (QK_lockPrio_ < m_lockPrio) { // raising the lock prio?
QK_lockPrio_ = m_lockPrio;
2015-12-31 14:56:37 -05:00
}
2016-04-01 13:55:34 -04:00
QS_BEGIN_NOCRIT_(QS_SCHED_LOCK,
static_cast<void *>(0), static_cast<void *>(0))
QS_TIME_(); // timestamp
QS_2U8_(static_cast<uint8_t>(m_prevPrio), /* the previous lock prio */
static_cast<uint8_t>(QK_lockPrio_)); // the new lock prio
QS_END_NOCRIT_()
2015-12-31 14:56:37 -05:00
QF_CRIT_EXIT_();
}
//****************************************************************************
2015-05-14 16:05:04 -04:00
/// @description
2016-04-01 13:55:34 -04:00
/// This function unlocks the QK mutex.
///
/// @note
/// A mutex must be initialized before it can be locked or unlocked.
2015-12-31 14:56:37 -05:00
///
2016-04-01 13:55:34 -04:00
/// @note
2016-05-09 11:44:05 -04:00
/// QP::QMutex::unlock() must always follow the corresponding
/// QP::QMutex::lock().
2015-12-31 14:56:37 -05:00
///
2016-05-09 11:44:05 -04:00
/// @sa QP::QMutex::init(), QP::QMutex::lock()
2014-04-13 21:35:34 -04:00
///
2015-05-14 16:05:04 -04:00
/// @usage
2016-04-01 13:55:34 -04:00
/// The following example shows how to initialize, lock and unlock QK mutex:
2015-05-14 16:05:04 -04:00
/// @include qk_mux.cpp
2014-04-13 21:35:34 -04:00
///
2016-05-09 11:44:05 -04:00
void QMutex::unlock(void) {
2012-08-14 18:00:48 -04:00
QF_CRIT_STAT_
QF_CRIT_ENTRY_();
2016-04-01 13:55:34 -04:00
/// @pre scheduler cannot be unlocked from the ISR context
/// and the mutex must not be unused
Q_REQUIRE_ID(800, (!QK_ISR_CONTEXT_())
&& (m_prevPrio != static_cast<uint_fast8_t>(MUTEX_UNUSED)));
QS_BEGIN_NOCRIT_(QS_SCHED_UNLOCK,
static_cast<void *>(0), static_cast<void *>(0))
QS_TIME_(); // timestamp
QS_2U8_(static_cast<uint8_t>(m_prevPrio),/* the previouis lock prio */
static_cast<uint8_t>(QK_lockPrio_)); // the new lock prio
QS_END_NOCRIT_()
uint_fast8_t p = m_prevPrio;
m_prevPrio = static_cast<uint_fast8_t>(MUTEX_UNUSED);
if (QK_lockPrio_ > p) {
QK_lockPrio_ = p; // restore the previous lock prio
p = QK_schedPrio_(); // find the highest-prio AO ready to run
if (p != static_cast<uint_fast8_t>(0)) { // priority found?
QK_sched_(p); // schedule any unlocked AOs
2012-08-14 18:00:48 -04:00
}
}
QF_CRIT_EXIT_();
}
2014-04-13 21:35:34 -04:00
} // namespace QP
2013-10-10 20:01:51 -04:00