/** * @brief Tries to lock a mutex. * @details This function attempts to lock a mutex, if the mutex is already * locked by another thread then the function exits without waiting. * @post The mutex is locked and inserted in the per-thread stack of owned * mutexes. * @note This function does not have any overhead related to the * priority inheritance mechanism because it does not try to * enter a sleep state. * * @param[in] mp pointer to the @p mutex_t structure * @return The operation status. * @retval true if the mutex has been successfully acquired * @retval false if the lock attempt failed. * * @api */ bool chMtxTryLock(mutex_t *mp) { bool b; chSysLock(); b = chMtxTryLockS(mp); chSysUnlock(); return b; }
/** * @brief Tries to lock a mutex. * @details This function attempts to lock a mutex, if the mutex is already * locked by another thread then the function exits without waiting. * @post The mutex is locked and inserted in the per-thread stack of owned * mutexes. * @note This function does not have any overhead related to the * priority inheritance mechanism because it does not try to * enter a sleep state. * * @param[in] mp pointer to the @p Mutex structure * @return The operation status. * @retval TRUE if the mutex has been successfully acquired * @retval FALSE if the lock attempt failed. * * @api */ bool_t chMtxTryLock(Mutex *mp) { bool_t b; chSysLock(); b = chMtxTryLockS(mp); chSysUnlock(); return b; }
bool Mutex::tryLockS(void) { return chMtxTryLockS(&mutex); }
static void test_005_006_execute(void) { bool b; tprio_t prio; /* [5.6.1] Getting current thread priority for later checks.*/ test_set_step(1); { prio = chThdGetPriorityX(); } /* [5.6.2] Locking the mutex first time, it must be possible because it is not owned.*/ test_set_step(2); { b = chMtxTryLock(&m1); test_assert(b, "already locked"); } /* [5.6.3] Locking the mutex second time, it must be possible because it is recursive.*/ test_set_step(3); { b = chMtxTryLock(&m1); test_assert(b, "already locked"); } /* [5.6.4] Unlocking the mutex then it must be still owned because recursivity.*/ test_set_step(4); { chMtxUnlock(&m1); test_assert(m1.owner != NULL, "not owned"); } /* [5.6.5] Unlocking the mutex then it must not be owned anymore and the queue must be empty.*/ test_set_step(5); { chMtxUnlock(&m1); test_assert(m1.owner == NULL, "still owned"); test_assert(queue_isempty(&m1.queue), "queue not empty"); } /* [5.6.6] Testing that priority has not changed after operations.*/ test_set_step(6); { test_assert(chThdGetPriorityX() == prio, "wrong priority level"); } /* [5.6.7] Testing consecutive chMtxTryLock()/chMtxTryLockS() calls and a final chMtxUnlockAllS().*/ test_set_step(7); { b = chMtxTryLock(&m1); test_assert(b, "already locked"); chSysLock(); b = chMtxTryLockS(&m1); chSysUnlock(); test_assert(b, "already locked"); test_assert(m1.cnt == 2, "invalid recursion counter"); chSysLock(); chMtxUnlockAllS(); chSysUnlock(); test_assert(m1.owner == NULL, "still owned"); test_assert(queue_isempty(&m1.queue), "queue not empty"); test_assert(m1.cnt == 0, "invalid recursion counter"); } /* [5.6.8] Testing consecutive chMtxLock()/chMtxLockS() calls and a final chMtxUnlockAll().*/ test_set_step(8); { chMtxLock(&m1); test_assert(m1.owner != NULL, "not owned"); chSysLock(); chMtxLockS(&m1); chSysUnlock(); test_assert(m1.owner != NULL, "not owned"); test_assert(m1.cnt == 2, "invalid recursion counter"); chMtxUnlockAll(); test_assert(m1.owner == NULL, "still owned"); test_assert(queue_isempty(&m1.queue), "queue not empty"); test_assert(m1.cnt == 0, "invalid recursion counter"); } /* [5.6.9] Testing that priority has not changed after operations.*/ test_set_step(9); { test_assert(chThdGetPriorityX() == prio, "wrong priority level"); } }