1 //===-- Declarations related mutex attribute objects  -----------*- C++ -*-===//
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 #ifndef LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_H
10 #define LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_H
11 
12 #include <pthread.h>
13 
14 namespace __llvm_libc {
15 
16 enum class PThreadMutexAttrPos : unsigned int {
17   TYPE_SHIFT = 0,
18   TYPE_MASK = 0x3 << TYPE_SHIFT, // Type is encoded in 2 bits
19 
20   ROBUST_SHIFT = 2,
21   ROBUST_MASK = 0x1 << ROBUST_SHIFT,
22 
23   PSHARED_SHIFT = 3,
24   PSHARED_MASK = 0x1 << PSHARED_SHIFT,
25 
26   // TODO: Add a mask for protocol and prioceiling when it is supported.
27 };
28 
29 constexpr pthread_mutexattr_t DEFAULT_MUTEXATTR =
30     PTHREAD_MUTEX_DEFAULT << unsigned(PThreadMutexAttrPos::TYPE_SHIFT) |
31     PTHREAD_MUTEX_STALLED << unsigned(PThreadMutexAttrPos::ROBUST_SHIFT) |
32     PTHREAD_PROCESS_PRIVATE << unsigned(PThreadMutexAttrPos::PSHARED_SHIFT);
33 
get_mutexattr_type(pthread_mutexattr_t attr)34 static inline int get_mutexattr_type(pthread_mutexattr_t attr) {
35   return (attr & unsigned(PThreadMutexAttrPos::TYPE_MASK)) >>
36          unsigned(PThreadMutexAttrPos::TYPE_SHIFT);
37 }
38 
get_mutexattr_robust(pthread_mutexattr_t attr)39 static inline int get_mutexattr_robust(pthread_mutexattr_t attr) {
40   return (attr & unsigned(PThreadMutexAttrPos::ROBUST_MASK)) >>
41          unsigned(PThreadMutexAttrPos::ROBUST_SHIFT);
42 }
43 
44 } // namespace __llvm_libc
45 
46 #endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_H
47