mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2025-01-09 17:43:57 +00:00
1f8790050b
Original-commit: flang-compiler/f18@9fe84f45d7 Reviewed-on: https://github.com/flang-compiler/f18/pull/1094
46 lines
1.0 KiB
C++
46 lines
1.0 KiB
C++
//===-- runtime/lock.h ------------------------------------------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Wraps a mutex
|
|
|
|
#ifndef FORTRAN_RUNTIME_LOCK_H_
|
|
#define FORTRAN_RUNTIME_LOCK_H_
|
|
|
|
#include "terminator.h"
|
|
#include <mutex>
|
|
|
|
namespace Fortran::runtime {
|
|
|
|
class Lock {
|
|
public:
|
|
void Take() { mutex_.lock(); }
|
|
bool Try() { return mutex_.try_lock(); }
|
|
void Drop() { mutex_.unlock(); }
|
|
void CheckLocked(const Terminator &terminator) {
|
|
if (Try()) {
|
|
Drop();
|
|
terminator.Crash("Lock::CheckLocked() failed");
|
|
}
|
|
}
|
|
|
|
private:
|
|
std::mutex mutex_;
|
|
};
|
|
|
|
class CriticalSection {
|
|
public:
|
|
explicit CriticalSection(Lock &lock) : lock_{lock} { lock_.Take(); }
|
|
~CriticalSection() { lock_.Drop(); }
|
|
|
|
private:
|
|
Lock &lock_;
|
|
};
|
|
} // namespace Fortran::runtime
|
|
|
|
#endif // FORTRAN_RUNTIME_LOCK_H_
|