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

Function pthread_mutex_lock

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

* @brief Locks a mutex. * * This function locks the mutex object pointed to by `mutex`. If the mutex is * already locked by another thread, the calling thread will block until the * mutex becomes available. * * @param[in,out] mutex Pointer to the mutex to be locked. * * @return * - 0 on success. * - Non-zero error code on failure, including: * - `EDEADLK`: A deadlock condition was det

Source from the content-addressed store, hash-verified

392 * @see pthread_mutex_unlock, pthread_mutex_trylock, pthread_mutex_init
393 */
394int pthread_mutex_lock(pthread_mutex_t *mutex)
395{
396 int mtype;
397 rt_err_t result;
398
399 if (!mutex)
400 return EINVAL;
401
402 if (mutex->attr == -1)
403 {
404 /* init mutex */
405 pthread_mutex_init(mutex, RT_NULL);
406 }
407
408 mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
409 rt_enter_critical();
410 if (mutex->lock.owner == rt_thread_self() &&
411 mtype != PTHREAD_MUTEX_RECURSIVE)
412 {
413 rt_exit_critical();
414
415 return EDEADLK;
416 }
417 rt_exit_critical();
418
419 result = rt_mutex_take(&(mutex->lock), RT_WAITING_FOREVER);
420 if (result == RT_EOK)
421 return 0;
422
423 return EINVAL;
424}
425RTM_EXPORT(pthread_mutex_lock);
426
427/**

Callers 15

emutls_get_indexFunction · 0.85
pthread_barrier_destroyFunction · 0.85
pthread_barrier_waitFunction · 0.85
_pthread_cond_timedwaitFunction · 0.85
pthread_rwlock_destroyFunction · 0.85
pthread_rwlock_rdlockFunction · 0.85
pthread_rwlock_tryrdlockFunction · 0.85
pthread_rwlock_trywrlockFunction · 0.85
pthread_rwlock_unlockFunction · 0.85
pthread_rwlock_wrlockFunction · 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 1

test_threadFunction · 0.68