* @brief Unlocks a mutex. * * This function unlocks the mutex object pointed to by `mutex`. If other threads * are blocked waiting for the mutex, one of them will acquire the lock once it is * released. The calling thread must hold the lock on the mutex before calling * this function. * * @param[in,out] mutex Pointer to the mutex to be unlocked. * * @return * - 0 on success. * - Non-zer
| 452 | * @see pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_init |
| 453 | */ |
| 454 | int pthread_mutex_unlock(pthread_mutex_t *mutex) |
| 455 | { |
| 456 | rt_err_t result; |
| 457 | |
| 458 | if (!mutex) |
| 459 | return EINVAL; |
| 460 | if (mutex->attr == -1) |
| 461 | { |
| 462 | /* init mutex */ |
| 463 | pthread_mutex_init(mutex, RT_NULL); |
| 464 | } |
| 465 | |
| 466 | if (mutex->lock.owner != rt_thread_self()) |
| 467 | { |
| 468 | int mtype; |
| 469 | mtype = mutex->attr & MUTEXATTR_TYPE_MASK; |
| 470 | |
| 471 | /* error check, return EPERM */ |
| 472 | if (mtype == PTHREAD_MUTEX_ERRORCHECK) |
| 473 | return EPERM; |
| 474 | |
| 475 | /* no thread waiting on this mutex */ |
| 476 | if (mutex->lock.owner == RT_NULL) |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | result = rt_mutex_release(&(mutex->lock)); |
| 481 | if (result == RT_EOK) |
| 482 | return 0; |
| 483 | |
| 484 | return EINVAL; |
| 485 | } |
| 486 | RTM_EXPORT(pthread_mutex_unlock); |
| 487 | |
| 488 | /** |