xref: /llvm-project/libc/src/pthread/pthread_mutex_init.cpp (revision 46944b0cbc9a9d8daad0182c40fcd3560bc9ca35)
12a5d5078SSiva Chandra Reddy //===-- Linux implementation of the pthread_mutex_init function -----------===//
22a5d5078SSiva Chandra Reddy //
32a5d5078SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42a5d5078SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
52a5d5078SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62a5d5078SSiva Chandra Reddy //
72a5d5078SSiva Chandra Reddy //===----------------------------------------------------------------------===//
82a5d5078SSiva Chandra Reddy 
92a5d5078SSiva Chandra Reddy #include "pthread_mutex_init.h"
102a5d5078SSiva Chandra Reddy #include "pthread_mutexattr.h"
112a5d5078SSiva Chandra Reddy 
122a5d5078SSiva Chandra Reddy #include "src/__support/common.h"
13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
142a5d5078SSiva Chandra Reddy #include "src/__support/threads/mutex.h"
152a5d5078SSiva Chandra Reddy 
162a5d5078SSiva Chandra Reddy #include <pthread.h>
172a5d5078SSiva Chandra Reddy 
18*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
192a5d5078SSiva Chandra Reddy 
202a5d5078SSiva Chandra Reddy static_assert(sizeof(Mutex) <= sizeof(pthread_mutex_t),
212a5d5078SSiva Chandra Reddy               "The public pthread_mutex_t type cannot accommodate the internal "
222a5d5078SSiva Chandra Reddy               "mutex type.");
232a5d5078SSiva Chandra Reddy 
242a5d5078SSiva Chandra Reddy LLVM_LIBC_FUNCTION(int, pthread_mutex_init,
252a5d5078SSiva Chandra Reddy                    (pthread_mutex_t * m,
262a5d5078SSiva Chandra Reddy                     const pthread_mutexattr_t *__restrict attr)) {
272a5d5078SSiva Chandra Reddy   auto mutexattr = attr == nullptr ? DEFAULT_MUTEXATTR : *attr;
282a5d5078SSiva Chandra Reddy   auto err =
29142afde0SSchrodinger ZHU Yifan       Mutex::init(reinterpret_cast<Mutex *>(m), /*is_timed=*/true,
302a5d5078SSiva Chandra Reddy                   get_mutexattr_type(mutexattr) & PTHREAD_MUTEX_RECURSIVE,
31142afde0SSchrodinger ZHU Yifan                   get_mutexattr_robust(mutexattr) & PTHREAD_MUTEX_ROBUST,
32142afde0SSchrodinger ZHU Yifan                   get_mutexattr_pshared(mutexattr) & PTHREAD_PROCESS_SHARED);
332a5d5078SSiva Chandra Reddy   return err == MutexError::NONE ? 0 : EAGAIN;
342a5d5078SSiva Chandra Reddy }
352a5d5078SSiva Chandra Reddy 
36*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
37