xref: /freebsd/contrib/llvm-project/libcxx/src/mutex_destructor.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric // Define ~mutex.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // On some platforms ~mutex has been made trivial and the definition is only
120b57cec5SDimitry Andric // provided for ABI compatibility.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric // In order to avoid ODR violations within libc++ itself, we need to ensure
150b57cec5SDimitry Andric // that *nothing* sees the non-trivial mutex declaration. For this reason
160b57cec5SDimitry Andric // we re-declare the entire class in this file instead of using
170b57cec5SDimitry Andric // _LIBCPP_BUILDING_LIBRARY to change the definition in the headers.
180b57cec5SDimitry Andric 
1981ad6265SDimitry Andric #include <__config>
2081ad6265SDimitry Andric #include <__threading_support>
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #if _LIBCPP_ABI_VERSION == 1 || !defined(_LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION)
230b57cec5SDimitry Andric #  define NEEDS_MUTEX_DESTRUCTOR
240b57cec5SDimitry Andric #endif
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric #ifdef NEEDS_MUTEX_DESTRUCTOR
2906c3fb27SDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI mutex
300b57cec5SDimitry Andric {
310b57cec5SDimitry Andric     __libcpp_mutex_t __m_ = _LIBCPP_MUTEX_INITIALIZER;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric public:
34*5f757f3fSDimitry Andric     _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI
350b57cec5SDimitry Andric     constexpr mutex() = default;
360b57cec5SDimitry Andric     mutex(const mutex&) = delete;
370b57cec5SDimitry Andric     mutex& operator=(const mutex&) = delete;
380b57cec5SDimitry Andric     ~mutex() noexcept;
390b57cec5SDimitry Andric };
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric 
42fe6060f1SDimitry Andric mutex::~mutex() noexcept
430b57cec5SDimitry Andric {
440b57cec5SDimitry Andric     __libcpp_mutex_destroy(&__m_);
450b57cec5SDimitry Andric }
46*5f757f3fSDimitry Andric #endif // !NEEDS_MUTEX_DESTRUCTOR
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
49