QP/C
qk_mutex.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Product: QK/C
00003 * Last Updated for Version: 4.4.00
00004 * Date of the Last Update:  Jan 04, 2012
00005 *
00006 *                    Q u a n t u m     L e a P s
00007 *                    ---------------------------
00008 *                    innovating embedded systems
00009 *
00010 * Copyright (C) 2002-2012 Quantum Leaps, LLC. All rights reserved.
00011 *
00012 * This software may be distributed and modified under the terms of the GNU
00013 * General Public License version 2 (GPL) as published by the Free Software
00014 * Foundation and appearing in the file GPL.TXT included in the packaging of
00015 * this file. Please note that GPL Section 2[b] requires that all works based
00016 * on this software must also be made publicly available under the terms of
00017 * the GPL ("Copyleft").
00018 *
00019 * Alternatively, this software may be distributed and modified under the
00020 * terms of Quantum Leaps commercial licenses, which expressly supersede
00021 * the GPL and are specifically designed for licensees interested in
00022 * retaining the proprietary status of their code.
00023 *
00024 * Contact information:
00025 * Quantum Leaps Web site:  http://www.quantum-leaps.com
00026 * e-mail:                  info@quantum-leaps.com
00027 *****************************************************************************/
00028 #include "qk_pkg.h"
00029 /*#include "qassert.h"*/
00030 
00031 /*Q_DEFINE_THIS_MODULE("qk_mutex")*/
00032 
00039 #ifdef QK_NO_MUTEX
00040     #error "qk_mutex.c included in the build when QK_NO_MUTEX defined"
00041 #endif
00042 
00043 /* package-scope objects ---------------------------------------------------*/
00044 uint8_t QK_ceilingPrio_;                     /* ceiling priority of a mutex */
00045 
00046 /*..........................................................................*/
00047 QMutex QK_mutexLock(uint8_t prioCeiling) {
00048     uint8_t mutex;
00049     QF_CRIT_STAT_
00050     QF_CRIT_ENTRY_();
00051     mutex = QK_ceilingPrio_;  /* the original QK priority ceiling to return */
00052     if (QK_ceilingPrio_ < prioCeiling) {
00053         QK_ceilingPrio_ = prioCeiling;     /* raise the QK priority ceiling */
00054     }
00055 
00056     QS_BEGIN_NOCRIT_(QS_QK_MUTEX_LOCK, (void *)0, (void *)0)
00057         QS_TIME_();                                            /* timestamp */
00058         QS_U8_(mutex);                             /* the original priority */
00059         QS_U8_(QK_ceilingPrio_);            /* the current priority ceiling */
00060     QS_END_NOCRIT_()
00061 
00062     QF_CRIT_EXIT_();
00063     return mutex;
00064 }
00065 /*..........................................................................*/
00066 void QK_mutexUnlock(QMutex mutex) {
00067     QF_CRIT_STAT_
00068     QF_CRIT_ENTRY_();
00069 
00070     QS_BEGIN_NOCRIT_(QS_QK_MUTEX_UNLOCK, (void *)0, (void *)0)
00071         QS_TIME_();                                            /* timestamp */
00072         QS_U8_(mutex);                             /* the original priority */
00073         QS_U8_(QK_ceilingPrio_);            /* the current priority ceiling */
00074     QS_END_NOCRIT_()
00075 
00076     if (QK_ceilingPrio_ > mutex) {
00077         QK_ceilingPrio_ = mutex;      /* restore the saved priority ceiling */
00078         mutex = QK_schedPrio_();          /* reuse 'mutex' to hold priority */
00079         if (mutex != (uint8_t)0) {
00080             QK_sched_(mutex);
00081         }
00082     }
00083     QF_CRIT_EXIT_();
00084 }