xref: /freebsd/contrib/llvm-project/libcxx/src/mutex_destructor.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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>
20*0fca6ea1SDimitry Andric #include <__thread/support.h>
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
29cb14a3feSDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI mutex {
300b57cec5SDimitry Andric   __libcpp_mutex_t __m_ = _LIBCPP_MUTEX_INITIALIZER;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric public:
33cb14a3feSDimitry Andric   _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI constexpr mutex() = default;
340b57cec5SDimitry Andric   mutex(const mutex&)                                           = delete;
350b57cec5SDimitry Andric   mutex& operator=(const mutex&)                                = delete;
360b57cec5SDimitry Andric   ~mutex() noexcept;
370b57cec5SDimitry Andric };
380b57cec5SDimitry Andric 
~mutex()39cb14a3feSDimitry Andric mutex::~mutex() noexcept { __libcpp_mutex_destroy(&__m_); }
405f757f3fSDimitry Andric #endif // !NEEDS_MUTEX_DESTRUCTOR
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
43