Path: blob/main/contrib/llvm-project/libc/src/__support/CPP/mutex.h
213799 views
//===--- A self contained equivalent of std::mutex --------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_MUTEX_H9#define LLVM_LIBC_SRC___SUPPORT_CPP_MUTEX_H1011#include "src/__support/macros/config.h"1213namespace LIBC_NAMESPACE_DECL {14namespace cpp {1516// Assume the calling thread has already obtained mutex ownership.17struct adopt_lock_t {18explicit adopt_lock_t() = default;19};2021// Tag used to make a scoped lock take ownership of a locked mutex.22constexpr adopt_lock_t adopt_lock{};2324// An RAII class for easy locking and unlocking of mutexes.25template <typename MutexType> class lock_guard {26MutexType &mutex;2728public:29// Calls `m.lock()` upon resource acquisition.30explicit lock_guard(MutexType &m) : mutex(m) { mutex.lock(); }3132// Acquires ownership of the mutex object `m` without attempting to lock33// it. The behavior is undefined if the current thread does not hold the34// lock on `m`. Does not call `m.lock()` upon resource acquisition.35lock_guard(MutexType &m, adopt_lock_t /* t */) : mutex(m) {}3637~lock_guard() { mutex.unlock(); }3839// non-copyable40lock_guard &operator=(const lock_guard &) = delete;41lock_guard(const lock_guard &) = delete;42};4344// Deduction guide for lock_guard to suppress CTAD warnings.45template <typename T> lock_guard(T &) -> lock_guard<T>;4647} // namespace cpp48} // namespace LIBC_NAMESPACE_DECL4950#endif // LLVM_LIBC_SRC___SUPPORT_CPP_MUTEX_H515253