qpc/source/qk_mutex.c

125 lines
4.2 KiB
C
Raw Normal View History

2014-04-06 11:43:13 -04:00
/**
2015-04-28 13:45:35 -04:00
* @file
* @ingroup qk
* @brief QK_mutexLock() and QK_mutexUnlock() definitions.
* @cond
2014-04-06 11:43:13 -04:00
******************************************************************************
2015-04-28 13:45:35 -04:00
* Last updated for version 5.4.0
* Last updated on 2015-03-13
2012-08-14 18:07:04 -04:00
*
* Q u a n t u m L e a P s
* ---------------------------
* innovating embedded systems
*
2014-04-06 11:43:13 -04:00
* Copyright (C) Quantum Leaps, www.state-machine.com.
2012-08-14 18:07:04 -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
2012-08-14 18:07:04 -04:00
* (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:
2014-04-06 11:43:13 -04:00
* Web: www.state-machine.com
* Email: info@state-machine.com
******************************************************************************
2015-04-28 13:45:35 -04:00
* @endcond
2012-08-14 18:07:04 -04:00
*/
2015-04-28 13:45:35 -04:00
#define QP_IMPL /* this is QP implementation */
#include "qf_port.h" /* QF port */
#include "qk_pkg.h" /* QK package-scope interface */
2014-04-06 11:43:13 -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:07:04 -04:00
#ifdef QK_NO_MUTEX
#error "qk_mutex.c included in the build when QK_NO_MUTEX defined"
#endif
/* Package-scope objects ****************************************************/
2014-04-06 11:43:13 -04:00
uint_fast8_t volatile QK_ceilingPrio_; /* ceiling priority of a mutex */
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/****************************************************************************/
/**
2015-04-28 13:45:35 -04:00
* @description
2014-04-06 11:43:13 -04:00
* Lock the QK scheduler up to the given priority level.
*
2015-04-28 13:45:35 -04:00
* @param[in] prioCeiling priority ceiling to lock the mutex
2014-04-06 11:43:13 -04:00
*
2015-04-28 13:45:35 -04:00
* @returns the previous value of the mutex priority ceiling
2014-04-06 11:43:13 -04:00
*
2015-04-28 13:45:35 -04:00
* @note This function should be always paired with QK_mutexUnlock(). The
2014-04-06 11:43:13 -04:00
* code between QK_mutexLock() and QK_mutexUnlock() should be kept to the
* minimum.
*
2015-04-28 13:45:35 -04:00
* @usage
* @include qk_mux.c
2014-04-06 11:43:13 -04:00
*/
QMutex QK_mutexLock(uint_fast8_t prioCeiling) {
QMutex mutex;
2012-08-14 18:07:04 -04:00
QF_CRIT_STAT_
QF_CRIT_ENTRY_();
2014-04-06 11:43:13 -04:00
mutex = QK_ceilingPrio_; /* the original QK priority ceiling to return */
2012-08-14 18:07:04 -04:00
if (QK_ceilingPrio_ < prioCeiling) {
2014-04-06 11:43:13 -04:00
QK_ceilingPrio_ = prioCeiling; /* raise the QK priority ceiling */
2012-08-14 18:07:04 -04:00
}
QS_BEGIN_NOCRIT_(QS_QK_MUTEX_LOCK, (void *)0, (void *)0)
2014-04-06 11:43:13 -04:00
QS_TIME_(); /* timestamp */
QS_U8_((uint8_t)mutex); /* the original priority */
QS_U8_((uint8_t)QK_ceilingPrio_); /* the current priority ceiling */
2012-08-14 18:07:04 -04:00
QS_END_NOCRIT_()
QF_CRIT_EXIT_();
return mutex;
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
/**
2015-04-28 13:45:35 -04:00
* @description
2014-04-06 11:43:13 -04:00
* Unlock the QK scheduler up to the saved priority level.
*
2015-04-28 13:45:35 -04:00
* @param[in] mutex previous priority level to unlock the mutex
2014-04-06 11:43:13 -04:00
*
2015-04-28 13:45:35 -04:00
* @description
* @note This function should be always paired with QK_mutexLock(). The
2014-04-06 11:43:13 -04:00
* code between QK_mutexLock() and QK_mutexUnlock() should be kept to the
* minimum.
*
2015-04-28 13:45:35 -04:00
* @usage
* @include qk_mux.c
2014-04-06 11:43:13 -04:00
*/
2012-08-14 18:07:04 -04:00
void QK_mutexUnlock(QMutex mutex) {
QF_CRIT_STAT_
QF_CRIT_ENTRY_();
QS_BEGIN_NOCRIT_(QS_QK_MUTEX_UNLOCK, (void *)0, (void *)0)
2014-04-06 11:43:13 -04:00
QS_TIME_(); /* timestamp */
QS_U8_((uint8_t)mutex); /* the original priority */
QS_U8_((uint8_t)QK_ceilingPrio_); /* the current priority ceiling */
2012-08-14 18:07:04 -04:00
QS_END_NOCRIT_()
if (QK_ceilingPrio_ > mutex) {
2014-04-06 11:43:13 -04:00
QK_ceilingPrio_ = mutex; /* restore the saved priority ceiling */
mutex = QK_schedPrio_(); /* reuse 'mutex' to hold priority */
if (mutex != (QMutex)0) {
2012-08-14 18:07:04 -04:00
QK_sched_(mutex);
}
}
QF_CRIT_EXIT_();
}