/* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include "common/libs/fs/shared_fd.h" #include "common/libs/utils/result.h" namespace cuttlefish { class InstanceLockFileManager; enum class InUseState : char { kInUse = 'I', kNotInUse = 'N', }; // Replicates tempfile.gettempdir() in Python std::string TempDir(); // This class is not thread safe. class InstanceLockFile { public: int Instance() const; Result Status() const; Result Status(InUseState); bool operator<(const InstanceLockFile&) const; private: friend class InstanceLockFileManager; InstanceLockFile(SharedFD fd, int instance_num); SharedFD fd_; int instance_num_; }; class InstanceLockFileManager { public: INJECT(InstanceLockFileManager()); Result AcquireLock(int instance_num); Result> AcquireLocks(const std::set& nums); Result> TryAcquireLock(int instance_num); Result> TryAcquireLocks(const std::set& nums); // Best-effort attempt to find a free instance id. Result> TryAcquireUnusedLock(); }; } // namespace cuttlefish