1//===----------------------------------------------------------------------===//
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// Define ~mutex.
10//
11// On some platforms ~mutex has been made trivial and the definition is only
12// provided for ABI compatibility.
13//
14// In order to avoid ODR violations within libc++ itself, we need to ensure
15// that *nothing* sees the non-trivial mutex declaration. For this reason
16// we re-declare the entire class in this file instead of using
17// _LIBCPP_BUILDING_LIBRARY to change the definition in the headers.
18
19#include <__config>
20#include <__thread/support.h>
21
22#if _LIBCPP_AVAILABILITY_MINIMUM_HEADER_VERSION < 9 || !_LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION
23# define NEEDS_MUTEX_DESTRUCTOR
24#endif
25
26_LIBCPP_BEGIN_NAMESPACE_STD
27_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
28
29#ifdef NEEDS_MUTEX_DESTRUCTOR
30class _LIBCPP_EXPORTED_FROM_ABI mutex {
31 __libcpp_mutex_t __m_ = _LIBCPP_MUTEX_INITIALIZER;
32
33public:
34 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI constexpr mutex() = default;
35 mutex(const mutex&) = delete;
36 mutex& operator=(const mutex&) = delete;
37 ~mutex() noexcept;
38};
39
40mutex::~mutex() noexcept { __libcpp_mutex_destroy(m: &__m_); }
41#endif // !NEEDS_MUTEX_DESTRUCTOR
42
43_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
44_LIBCPP_END_NAMESPACE_STD
45