/* * 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 #include "host/commands/cvd/lock_file.h" namespace cuttlefish { // This class is not thread safe. class InstanceLockFile { friend class InstanceLockFileManager; using LockFile = cvd_impl::LockFile; public: int Instance() const; Result Status() const; Result Status(InUseState); bool operator<(const InstanceLockFile&) const; private: InstanceLockFile(LockFile&& lock_file, const int instance_num); LockFile lock_file_; const int instance_num_; }; class InstanceLockFileManager { using LockFile = cvd_impl::LockFile; using LockFileManager = cvd_impl::LockFileManager; 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(); Result> LockAllAvailable(); private: /* * Generate value to initialize */ Result> FindPotentialInstanceNumsFromNetDevices(); static Result LockFilePath(int instance_num); std::optional> all_instance_nums_; LockFileManager lock_file_manager_; }; } // namespace cuttlefish