Android-cuttlefish cvd tool
data_viewer.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <signal.h>
20#include <sys/file.h>
21
22#include <functional>
23#include <mutex>
24#include <string>
25#include <thread>
26#include <unordered_map>
27#include <utility>
28
31#include "cuttlefish/host/commands/cvd/instances/cvd_persistent_data.pb.h"
33
34namespace cuttlefish {
35
43 public:
44 DataViewer(const std::string& backing_file) : backing_file_(backing_file) {}
45
53 template <typename R>
55 std::function<Result<R>(const cvd::PersistentData&)> task) const {
56 DeadlockProtector dp(*this);
57 auto fd = CF_EXPECT(LockBackingFile(LOCK_SH));
58 auto data = CF_EXPECT(LoadData(fd));
59 return task(data);
60 }
61
70 template <typename R>
72 std::function<Result<R>(cvd::PersistentData&)> task) {
73 DeadlockProtector dp(*this);
74 auto fd = CF_EXPECT(LockBackingFile(LOCK_EX));
75 auto data = CF_EXPECT(LoadData(fd));
76 auto res = task(data);
77 if (!res.has_value()) {
78 // Don't update if there is an error
79 return res;
80 }
82 return res;
83 }
84
90 template <typename R>
92 std::function<Result<std::pair<R, bool>>(cvd::PersistentData&)> task) {
93 DeadlockProtector dp(*this);
94 auto fd = CF_EXPECT(LockBackingFile(LOCK_EX));
95 auto data = CF_EXPECT(LoadData(fd));
96 auto [value, modified] = CF_EXPECT(task(data));
97 if (modified) {
99 }
100 return std::move(value);
101 }
102
103 private:
104 // Opens and locks the backing file. The lock will be dropped when the file
105 // descriptor closes.
106 Result<SharedFD> LockBackingFile(int op) const;
107
109
110 Result<void> StoreData(SharedFD fd, const cvd::PersistentData& data);
111
119 public:
120 DeadlockProtector(const DataViewer& dv);
124
125 private:
126 std::mutex& mtx_;
127 std::unordered_map<std::thread::id, bool>& map_;
128 };
129 mutable std::mutex lock_map_mtx_;
130 mutable std::unordered_map<std::thread::id, bool> lock_held_by_;
131
132 std::string backing_file_;
133};
134
135} // namespace cuttlefish
Definition: data_viewer.h:118
DeadlockProtector(DeadlockProtector &&)=delete
~DeadlockProtector()
Definition: data_viewer.cpp:78
std::mutex & mtx_
Definition: data_viewer.h:126
std::unordered_map< std::thread::id, bool > & map_
Definition: data_viewer.h:127
DeadlockProtector(const DeadlockProtector &)=delete
DeadlockProtector(const DataViewer &dv)
Definition: data_viewer.cpp:70
Definition: data_viewer.h:42
Result< void > StoreData(SharedFD fd, const cvd::PersistentData &data)
Definition: data_viewer.cpp:53
std::string backing_file_
Definition: data_viewer.h:132
Result< R > WithExclusiveLock(std::function< Result< R >(cvd::PersistentData &)> task)
Definition: data_viewer.h:71
Result< cvd::PersistentData > LoadData(SharedFD fd) const
Definition: data_viewer.cpp:45
Result< SharedFD > LockBackingFile(int op) const
Definition: data_viewer.cpp:36
std::unordered_map< std::thread::id, bool > lock_held_by_
Definition: data_viewer.h:130
DataViewer(const std::string &backing_file)
Definition: data_viewer.h:44
Result< R > WithSharedLock(std::function< Result< R >(const cvd::PersistentData &)> task) const
Definition: data_viewer.h:54
Result< R > WithConditionalExclusiveLock(std::function< Result< std::pair< R, bool > >(cvd::PersistentData &)> task)
Definition: data_viewer.h:91
std::mutex lock_map_mtx_
Definition: data_viewer.h:129
Definition: shared_fd.h:125
#define CF_EXPECT(...)
Definition: expect.h:184
char data[Size]
Definition: incremental_server.cpp:1
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30