brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.7 KiB · 8a31024 Raw
52 lines · c
1//===--- A self contained equivalent of std::mutex --------------*- C++ -*-===//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 LLVM_LIBC_SRC___SUPPORT_CPP_MUTEX_H10#define LLVM_LIBC_SRC___SUPPORT_CPP_MUTEX_H11 12#include "src/__support/macros/config.h"13 14namespace LIBC_NAMESPACE_DECL {15namespace cpp {16 17// Assume the calling thread has already obtained mutex ownership.18struct adopt_lock_t {19  explicit adopt_lock_t() = default;20};21 22// Tag used to make a scoped lock take ownership of a locked mutex.23constexpr adopt_lock_t adopt_lock{};24 25// An RAII class for easy locking and unlocking of mutexes.26template <typename MutexType> class lock_guard {27  MutexType &mutex;28 29public:30  // Calls `m.lock()` upon resource acquisition.31  explicit lock_guard(MutexType &m) : mutex(m) { mutex.lock(); }32 33  // Acquires ownership of the mutex object `m` without attempting to lock34  // it. The behavior is undefined if the current thread does not hold the35  // lock on `m`. Does not call `m.lock()` upon resource acquisition.36  lock_guard(MutexType &m, adopt_lock_t /* t */) : mutex(m) {}37 38  ~lock_guard() { mutex.unlock(); }39 40  // non-copyable41  lock_guard &operator=(const lock_guard &) = delete;42  lock_guard(const lock_guard &) = delete;43};44 45// Deduction guide for lock_guard to suppress CTAD warnings.46template <typename T> lock_guard(T &) -> lock_guard<T>;47 48} // namespace cpp49} // namespace LIBC_NAMESPACE_DECL50 51#endif // LLVM_LIBC_SRC___SUPPORT_CPP_MUTEX_H52