54 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___MUTEX_MUTEX_H10#define _LIBCPP___MUTEX_MUTEX_H11 12#include <__config>13#include <__thread/support.h>14#include <__type_traits/is_nothrow_constructible.h>15 16#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)17# pragma GCC system_header18#endif19 20#if _LIBCPP_HAS_THREADS21 22_LIBCPP_BEGIN_NAMESPACE_STD23 24class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_CAPABILITY("mutex") mutex {25 __libcpp_mutex_t __m_ = _LIBCPP_MUTEX_INITIALIZER;26 27public:28 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR mutex() = default;29 30 mutex(const mutex&) = delete;31 mutex& operator=(const mutex&) = delete;32 33# if _LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION34 _LIBCPP_HIDE_FROM_ABI ~mutex() = default;35# else36 ~mutex() _NOEXCEPT;37# endif38 39 _LIBCPP_ACQUIRE_CAPABILITY() void lock();40 [[__nodiscard__]] _LIBCPP_TRY_ACQUIRE_CAPABILITY(true) bool try_lock() _NOEXCEPT;41 _LIBCPP_RELEASE_CAPABILITY void unlock() _NOEXCEPT;42 43 typedef __libcpp_mutex_t* native_handle_type;44 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__m_; }45};46 47static_assert(is_nothrow_default_constructible<mutex>::value, "the default constructor for std::mutex must be nothrow");48 49_LIBCPP_END_NAMESPACE_STD50 51#endif // _LIBCPP_HAS_THREADS52 53#endif // _LIBCPP___MUTEX_MUTEX_H54