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//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H
10#define LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H
11
12#include "src/__support/macros/properties/architectures.h"
13
14// Platform independent code will include this header file which pulls
15// the platfrom specific specializations using platform macros.
16//
17// The platform specific specializations should define a class by name
18// Mutex with non-static methods having the following signature:
19//
20// MutexError lock();
21// MutexError trylock();
22// MutexError timedlock(...);
23// MutexError unlock();
24// MutexError reset(); // Used to reset inconsistent robust mutexes.
25//
26// Apart from the above non-static methods, the specializations should
27// also provide few static methods with the following signature:
28//
29// static MutexError init(mtx_t *);
30// static MutexError destroy(mtx_t *);
31//
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.
35//
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.
39
40#if defined(__linux__)
41#include "linux/mutex.h"
42#elif defined(LIBC_TARGET_ARCH_IS_GPU)
43#include "gpu/mutex.h"
44#endif // __linux__
45
46namespace LIBC_NAMESPACE {
47
48// An RAII class for easy locking and unlocking of mutexes.
49class MutexLock {
50 Mutex *mutex;
51
52public:
53 explicit MutexLock(Mutex *m) : mutex(m) { mutex->lock(); }
54
55 ~MutexLock() { mutex->unlock(); }
56};
57
58} // namespace LIBC_NAMESPACE
59
60#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H
61

source code of libc/src/__support/threads/mutex.h