[NFC][Py Reformat] Added more commits to .git-blame-ignore-revs
[llvm-project.git] / libc / src / __support / threads / mutex.h
blob3c2291d8f41fd1aeb8c2d73d100be10280437c24
1 //===--- A platform independent abstraction layer for mutexes ---*- 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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_THREAD_MUTEX_H
10 #define LLVM_LIBC_SRC_SUPPORT_THREAD_MUTEX_H
12 #include "src/__support/macros/properties/architectures.h"
14 // Platform independent code will include this header file which pulls
15 // the platfrom specific specializations using platform macros.
17 // The platform specific specializations should define a class by name
18 // Mutex with non-static methods having the following signature:
20 // MutexError lock();
21 // MutexError trylock();
22 // MutexError timedlock(...);
23 // MutexError unlock();
24 // MutexError reset(); // Used to reset inconsistent robust mutexes.
26 // Apart from the above non-static methods, the specializations should
27 // also provide few static methods with the following signature:
29 // static MutexError init(mtx_t *);
30 // static MutexError destroy(mtx_t *);
32 // All of the static and non-static methods should ideally be implemented
33 // as inline functions so that implementations of public functions can
34 // call them without a function call overhead.
36 // Another point to keep in mind that is that the libc internally needs a
37 // few global locks. So, to avoid static initialization order fiasco, we
38 // want the constructors of the Mutex classes to be constexprs.
40 #if defined(__unix__)
41 #include "linux/mutex.h"
42 #elif defined(LIBC_TARGET_ARCH_IS_GPU)
43 #include "gpu/mutex.h"
44 #endif // __unix__
46 namespace __llvm_libc {
48 // An RAII class for easy locking and unlocking of mutexes.
49 class MutexLock {
50 Mutex *mutex;
52 public:
53 explicit MutexLock(Mutex *m) : mutex(m) { mutex->lock(); }
55 ~MutexLock() { mutex->unlock(); }
58 } // namespace __llvm_libc
60 #endif // LLVM_LIBC_SRC_SUPPORT_THREAD_MUTEX_H