1cfca06d7SDimitry Andric //===-- LockFileBase.cpp --------------------------------------------------===//
25e95aa85SEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65e95aa85SEd Maste //
75e95aa85SEd Maste //===----------------------------------------------------------------------===//
85e95aa85SEd Maste
95e95aa85SEd Maste #include "lldb/Host/LockFileBase.h"
105e95aa85SEd Maste
115e95aa85SEd Maste using namespace lldb;
125e95aa85SEd Maste using namespace lldb_private;
135e95aa85SEd Maste
AlreadyLocked()14c0981da4SDimitry Andric static Status AlreadyLocked() { return Status("Already locked"); }
155e95aa85SEd Maste
NotLocked()16c0981da4SDimitry Andric static Status NotLocked() { return Status("Not locked"); }
175e95aa85SEd Maste
LockFileBase(int fd)1814f1b3e8SDimitry Andric LockFileBase::LockFileBase(int fd)
1914f1b3e8SDimitry Andric : m_fd(fd), m_locked(false), m_start(0), m_len(0) {}
2014f1b3e8SDimitry Andric
IsLocked() const2114f1b3e8SDimitry Andric bool LockFileBase::IsLocked() const { return m_locked; }
2214f1b3e8SDimitry Andric
WriteLock(const uint64_t start,const uint64_t len)23b76161e4SDimitry Andric Status LockFileBase::WriteLock(const uint64_t start, const uint64_t len) {
2414f1b3e8SDimitry Andric return DoLock([&](const uint64_t start,
2514f1b3e8SDimitry Andric const uint64_t len) { return DoWriteLock(start, len); },
2614f1b3e8SDimitry Andric start, len);
275e95aa85SEd Maste }
285e95aa85SEd Maste
TryWriteLock(const uint64_t start,const uint64_t len)29b76161e4SDimitry Andric Status LockFileBase::TryWriteLock(const uint64_t start, const uint64_t len) {
3014f1b3e8SDimitry Andric return DoLock([&](const uint64_t start,
3114f1b3e8SDimitry Andric const uint64_t len) { return DoTryWriteLock(start, len); },
3214f1b3e8SDimitry Andric start, len);
335e95aa85SEd Maste }
345e95aa85SEd Maste
ReadLock(const uint64_t start,const uint64_t len)35b76161e4SDimitry Andric Status LockFileBase::ReadLock(const uint64_t start, const uint64_t len) {
3614f1b3e8SDimitry Andric return DoLock([&](const uint64_t start,
3714f1b3e8SDimitry Andric const uint64_t len) { return DoReadLock(start, len); },
3814f1b3e8SDimitry Andric start, len);
395e95aa85SEd Maste }
405e95aa85SEd Maste
TryReadLock(const uint64_t start,const uint64_t len)41b76161e4SDimitry Andric Status LockFileBase::TryReadLock(const uint64_t start, const uint64_t len) {
4214f1b3e8SDimitry Andric return DoLock([&](const uint64_t start,
4314f1b3e8SDimitry Andric const uint64_t len) { return DoTryReadLock(start, len); },
4414f1b3e8SDimitry Andric start, len);
455e95aa85SEd Maste }
465e95aa85SEd Maste
Unlock()47b76161e4SDimitry Andric Status LockFileBase::Unlock() {
485e95aa85SEd Maste if (!IsLocked())
495e95aa85SEd Maste return NotLocked();
505e95aa85SEd Maste
515e95aa85SEd Maste const auto error = DoUnlock();
5214f1b3e8SDimitry Andric if (error.Success()) {
535e95aa85SEd Maste m_locked = false;
545e95aa85SEd Maste m_start = 0;
555e95aa85SEd Maste m_len = 0;
565e95aa85SEd Maste }
575e95aa85SEd Maste return error;
585e95aa85SEd Maste }
595e95aa85SEd Maste
IsValidFile() const6014f1b3e8SDimitry Andric bool LockFileBase::IsValidFile() const { return m_fd != -1; }
615e95aa85SEd Maste
DoLock(const Locker & locker,const uint64_t start,const uint64_t len)62b76161e4SDimitry Andric Status LockFileBase::DoLock(const Locker &locker, const uint64_t start,
6314f1b3e8SDimitry Andric const uint64_t len) {
645e95aa85SEd Maste if (!IsValidFile())
65b76161e4SDimitry Andric return Status("File is invalid");
665e95aa85SEd Maste
675e95aa85SEd Maste if (IsLocked())
685e95aa85SEd Maste return AlreadyLocked();
695e95aa85SEd Maste
705e95aa85SEd Maste const auto error = locker(start, len);
7114f1b3e8SDimitry Andric if (error.Success()) {
725e95aa85SEd Maste m_locked = true;
735e95aa85SEd Maste m_start = start;
745e95aa85SEd Maste m_len = len;
755e95aa85SEd Maste }
765e95aa85SEd Maste
775e95aa85SEd Maste return error;
785e95aa85SEd Maste }
79