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
30#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.ok()) {
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) >= 0, "Failed to truncate fd: {}",
86 fd->StrError());
87 CF_EXPECTF(fd->LSeek(0, SEEK_SET) >= 0, "Failed to seek to 0: {}",
88 fd->StrError());
89 CF_EXPECT(StoreData(fd, std::move(data)));
90 return res;
91 }
92
93 private:
94 // Opens and locks the backing file. The lock will be dropped when the file
95 // descriptor closes.
97
99
100 Result<void> StoreData(SharedFD fd, cvd::PersistentData data);
101
109 public:
110 DeadlockProtector(const DataViewer& dv);
114
115 private:
116 std::mutex& mtx_;
117 std::unordered_map<std::thread::id, bool>& map_;
118 };
119 mutable std::mutex lock_map_mtx_;
120 mutable std::unordered_map<std::thread::id, bool> lock_held_by_;
121
122 std::string backing_file_;
123};
124
125} // namespace cuttlefish
126
Definition: expected.h:86
Definition: data_viewer.h:108
DeadlockProtector(DeadlockProtector &&)=delete
~DeadlockProtector()
Definition: data_viewer.cpp:63
std::mutex & mtx_
Definition: data_viewer.h:116
std::unordered_map< std::thread::id, bool > & map_
Definition: data_viewer.h:117
DeadlockProtector(const DeadlockProtector &)=delete
DeadlockProtector(const DataViewer &dv)
Definition: data_viewer.cpp:55
Definition: data_viewer.h:40
std::string backing_file_
Definition: data_viewer.h:122
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:46
Result< cvd::PersistentData > LoadData(SharedFD fd) const
Definition: data_viewer.cpp:36
Result< SharedFD > LockBackingFile(int op) const
Definition: data_viewer.cpp:26
std::unordered_map< std::thread::id, bool > lock_held_by_
Definition: data_viewer.h:120
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:119
Definition: shared_fd.h:129
Definition: signals.h:30
#define CF_EXPECT(...)
Definition: result.h:414
#define CF_EXPECTF(RESULT, MSG,...)
Definition: result.h:417
char data[Size]
Definition: incremental_server.cpp:1
Definition: alloc_utils.cpp:23
UnitT< Unit::DP > dp
Definition: utils.h:177