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
21#include <functional>
22#include <mutex>
23#include <string>
24#include <thread>
25#include <unordered_map>
26
29#include "cuttlefish/host/commands/cvd/instances/cvd_persistent_data.pb.h"
31
32namespace cuttlefish {
33
41 public:
42 DataViewer(const std::string& backing_file) : backing_file_(backing_file) {}
43
51 template <typename R>
53 std::function<Result<R>(const cvd::PersistentData&)> task) const {
54 DeadlockProtector dp(*this);
55 auto fd = CF_EXPECT(LockBackingFile(LOCK_SH));
56 auto data = CF_EXPECT(LoadData(fd));
57 return task(data);
58 }
59
68 template <typename R>
70 std::function<Result<R>(cvd::PersistentData&)> task) {
71 DeadlockProtector dp(*this);
72 auto fd = CF_EXPECT(LockBackingFile(LOCK_SH));
73 auto data = CF_EXPECT(LoadData(fd));
74 auto res = task(data);
75 if (!res.has_value()) {
76 // Don't update if there is an error
77 return res;
78 }
79 // Block signals while writing to the instance database file. This reduces
80 // the chances of corrupting the file.
81 sigset_t all_signals;
82 sigfillset(&all_signals);
83 SignalMasker blocker(all_signals);
84 // Overwrite the file contents, don't append
85 CF_EXPECTF(fd->Truncate(0), "Failed to truncate fd: {}", fd->StrError());
86 CF_EXPECTF(fd->LSeek(0, SEEK_SET) >= 0, "Failed to seek to 0: {}",
87 fd->StrError());
88 CF_EXPECT(StoreData(fd, std::move(data)));
89 return res;
90 }
91
92 private:
93 // Opens and locks the backing file. The lock will be dropped when the file
94 // descriptor closes.
96
98
99 Result<void> StoreData(SharedFD fd, cvd::PersistentData data);
100
108 public:
109 DeadlockProtector(const DataViewer& dv);
113
114 private:
115 std::mutex& mtx_;
116 std::unordered_map<std::thread::id, bool>& map_;
117 };
118 mutable std::mutex lock_map_mtx_;
119 mutable std::unordered_map<std::thread::id, bool> lock_held_by_;
120
121 std::string backing_file_;
122};
123
124} // namespace cuttlefish
Definition: data_viewer.h:107
DeadlockProtector(DeadlockProtector &&)=delete
~DeadlockProtector()
Definition: data_viewer.cpp:64
std::mutex & mtx_
Definition: data_viewer.h:115
std::unordered_map< std::thread::id, bool > & map_
Definition: data_viewer.h:116
DeadlockProtector(const DeadlockProtector &)=delete
DeadlockProtector(const DataViewer &dv)
Definition: data_viewer.cpp:56
Definition: data_viewer.h:40
std::string backing_file_
Definition: data_viewer.h:121
Result< R > WithExclusiveLock(std::function< Result< R >(cvd::PersistentData &)> task)
Definition: data_viewer.h:69
Result< void > StoreData(SharedFD fd, cvd::PersistentData data)
Definition: data_viewer.cpp:47
Result< cvd::PersistentData > LoadData(SharedFD fd) const
Definition: data_viewer.cpp:39
Result< SharedFD > LockBackingFile(int op) const
Definition: data_viewer.cpp:31
std::unordered_map< std::thread::id, bool > lock_held_by_
Definition: data_viewer.h:119
DataViewer(const std::string &backing_file)
Definition: data_viewer.h:42
Result< R > WithSharedLock(std::function< Result< R >(const cvd::PersistentData &)> task) const
Definition: data_viewer.h:52
std::mutex lock_map_mtx_
Definition: data_viewer.h:118
Definition: shared_fd.h:124
Definition: signals.h:30
#define data
Definition: dict.c:56
#define CF_EXPECT(...)
Definition: expect.h:149
#define CF_EXPECTF(RESULT, MSG,...)
Definition: expect.h:152
Definition: alloc_driver.h:20
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
UnitT< Unit::DP > dp
Definition: utils.h:177