MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / pthread_mutex_trylock

Function pthread_mutex_trylock

components/libc/posix/pthreads/pthread_mutex.c:513–541  ·  view source on GitHub ↗

* @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

Source from the content-addressed store, hash-verified

511 * @see pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_init
512 */
513int 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}
542RTM_EXPORT(pthread_mutex_trylock);
543
544int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling)

Callers 2

mainthread_schedulerFunction · 0.85
searchFunction · 0.85

Calls 5

pthread_mutex_initFunction · 0.85
rt_thread_selfFunction · 0.85
rt_mutex_takeFunction · 0.85
rt_enter_criticalFunction · 0.50
rt_exit_criticalFunction · 0.50

Tested by

no test coverage detected