| 1 | //===-- Linux implementation of the pthread_mutex_init function -----------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "pthread_mutex_init.h" |
| 10 | #include "pthread_mutexattr.h" |
| 11 | |
| 12 | #include "src/__support/common.h" |
| 13 | #include "src/__support/macros/config.h" |
| 14 | #include "src/__support/threads/mutex.h" |
| 15 | |
| 16 | #include <pthread.h> |
| 17 | |
| 18 | namespace LIBC_NAMESPACE_DECL { |
| 19 | |
| 20 | static_assert(sizeof(Mutex) <= sizeof(pthread_mutex_t), |
| 21 | "The public pthread_mutex_t type cannot accommodate the internal " |
| 22 | "mutex type." ); |
| 23 | |
| 24 | LLVM_LIBC_FUNCTION(int, pthread_mutex_init, |
| 25 | (pthread_mutex_t * m, |
| 26 | const pthread_mutexattr_t *__restrict attr)) { |
| 27 | auto mutexattr = attr == nullptr ? DEFAULT_MUTEXATTR : *attr; |
| 28 | auto err = |
| 29 | Mutex::init(reinterpret_cast<Mutex *>(m), /*is_timed=*/true, |
| 30 | get_mutexattr_type(mutexattr) & PTHREAD_MUTEX_RECURSIVE, |
| 31 | get_mutexattr_robust(mutexattr) & PTHREAD_MUTEX_ROBUST, |
| 32 | get_mutexattr_pshared(mutexattr) & PTHREAD_PROCESS_SHARED); |
| 33 | return err == MutexError::NONE ? 0 : EAGAIN; |
| 34 | } |
| 35 | |
| 36 | } // namespace LIBC_NAMESPACE_DECL |
| 37 | |