mutex.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #ifndef _LIBCPP___MUTEX_MUTEX_H
  9. #define _LIBCPP___MUTEX_MUTEX_H
  10. #include <__config>
  11. #include <__thread/support.h>
  12. #include <__type_traits/is_nothrow_constructible.h>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. #ifndef _LIBCPP_HAS_NO_THREADS
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_THREAD_SAFETY_ANNOTATION(capability("mutex")) mutex {
  19. __libcpp_mutex_t __m_ = _LIBCPP_MUTEX_INITIALIZER;
  20. public:
  21. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR mutex() = default;
  22. mutex(const mutex&) = delete;
  23. mutex& operator=(const mutex&) = delete;
  24. # if defined(_LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION)
  25. _LIBCPP_HIDE_FROM_ABI ~mutex() = default;
  26. # else
  27. ~mutex() _NOEXCEPT;
  28. # endif
  29. void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability());
  30. bool try_lock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(try_acquire_capability(true));
  31. void unlock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability());
  32. typedef __libcpp_mutex_t* native_handle_type;
  33. _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__m_; }
  34. };
  35. static_assert(is_nothrow_default_constructible<mutex>::value, "the default constructor for std::mutex must be nothrow");
  36. _LIBCPP_END_NAMESPACE_STD
  37. #endif // _LIBCPP_HAS_NO_THREADS
  38. #endif // _LIBCPP___MUTEX_MUTEX_H