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/threads/mutex.h"
14
15#include <errno.h>
16#include <pthread.h>
17
18namespace LIBC_NAMESPACE {
19
20static_assert(sizeof(Mutex) <= sizeof(pthread_mutex_t),
21 "The public pthread_mutex_t type cannot accommodate the internal "
22 "mutex type.");
23
24LLVM_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(mutex: reinterpret_cast<Mutex *>(m), istimed: false,
30 isrecur: get_mutexattr_type(mutexattr) & PTHREAD_MUTEX_RECURSIVE,
31 isrobust: get_mutexattr_robust(mutexattr) & PTHREAD_MUTEX_ROBUST);
32 return err == MutexError::NONE ? 0 : EAGAIN;
33}
34
35} // namespace LIBC_NAMESPACE
36

source code of libc/src/pthread/pthread_mutex_init.cpp