Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __mutex / lock_guard.h
blobc075512fb97a95bdb9f13710ece20c50532101b7
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___MUTEX_LOCK_GUARD_H
10 #define _LIBCPP___MUTEX_LOCK_GUARD_H
12 #include <__config>
13 #include <__mutex/tag_types.h>
15 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16 # pragma GCC system_header
17 #endif
19 #ifndef _LIBCPP_HAS_NO_THREADS
21 _LIBCPP_BEGIN_NAMESPACE_STD
23 template <class _Mutex>
24 class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) lock_guard {
25 public:
26 typedef _Mutex mutex_type;
28 private:
29 mutex_type& __m_;
31 public:
32 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI explicit lock_guard(mutex_type& __m)
33 _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
34 : __m_(__m) {
35 __m_.lock();
38 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI lock_guard(mutex_type& __m, adopt_lock_t)
39 _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
40 : __m_(__m) {}
41 _LIBCPP_HIDE_FROM_ABI ~lock_guard() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) { __m_.unlock(); }
43 private:
44 lock_guard(lock_guard const&) = delete;
45 lock_guard& operator=(lock_guard const&) = delete;
47 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(lock_guard);
49 _LIBCPP_END_NAMESPACE_STD
51 #endif // _LIBCPP_HAS_NO_THREADS
53 #endif // _LIBCPP___MUTEX_LOCK_GUARD_H