* @brief Attempts to lock a mutex without blocking. * * This function attempts to lock the mutex object pointed to by `mutex`. If the mutex * is already locked by another thread, the function returns immediately with an error * code instead of blocking. * * @param[in,out] mutex Pointer to the mutex to be locked. * * @return * - 0 on success (the mutex was successfully locked). * - Non-ze
| 511 | * @see pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_init |
| 512 | */ |
| 513 | int pthread_mutex_trylock(pthread_mutex_t *mutex) |
| 514 | { |
| 515 | rt_err_t result; |
| 516 | int mtype; |
| 517 | |
| 518 | if (!mutex) |
| 519 | return EINVAL; |
| 520 | if (mutex->attr == -1) |
| 521 | { |
| 522 | /* init mutex */ |
| 523 | pthread_mutex_init(mutex, RT_NULL); |
| 524 | } |
| 525 | |
| 526 | mtype = mutex->attr & MUTEXATTR_TYPE_MASK; |
| 527 | rt_enter_critical(); |
| 528 | if (mutex->lock.owner == rt_thread_self() && |
| 529 | mtype != PTHREAD_MUTEX_RECURSIVE) |
| 530 | { |
| 531 | rt_exit_critical(); |
| 532 | |
| 533 | return EDEADLK; |
| 534 | } |
| 535 | rt_exit_critical(); |
| 536 | |
| 537 | result = rt_mutex_take(&(mutex->lock), 0); |
| 538 | if (result == RT_EOK) return 0; |
| 539 | |
| 540 | return EBUSY; |
| 541 | } |
| 542 | RTM_EXPORT(pthread_mutex_trylock); |
| 543 | |
| 544 | int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling) |
no test coverage detected