Android-cuttlefish cvd tool
host_mode_ctrl.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 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 <atomic>
20#include <condition_variable>
21#include <cstdint>
22#include <functional>
23#include <mutex>
24
25#include <fruit/fruit.h>
26
29
30namespace cuttlefish {
39 public:
40 enum class ModeType : std::uint8_t { kAndroidMode = 55, kConfUI_Mode = 77 };
64 << "checking atomic Android mode";
67 << "returns as it is already Android mode";
68 return;
69 }
70 auto check = [this]() -> bool {
72 };
73 std::unique_lock<std::mutex> lock(mode_mtx_);
74 and_mode_cv_.wait(lock, check);
76 << "awakes from cond var waiting for Android mode";
77 }
78
79 void SetMode(const ModeType mode) {
81 << " tries to acquire the lock in SetMode";
82 std::lock_guard<std::mutex> lock(mode_mtx_);
84 << " acquired the lock in SetMode";
85 atomic_mode_ = mode;
88 << " signals kAndroidMode in SetMode";
89 and_mode_cv_.notify_all();
90 return;
91 }
93 << "signals kConfUI_Mode in SetMode";
94 confui_mode_cv_.notify_all();
95 }
96
97 auto GetMode() {
98 ModeType ret_val = atomic_mode_;
99 return ret_val;
100 }
101
104 }
105
107
108 static HostModeCtrl& Get() {
109 static HostModeCtrl host_mode_controller;
110 return host_mode_controller;
111 }
112
113 private:
114 std::mutex mode_mtx_;
115 std::condition_variable and_mode_cv_;
116 std::condition_variable confui_mode_cv_;
117 std::atomic<ModeType> atomic_mode_;
118};
119} // end of namespace cuttlefish
Definition: host_mode_ctrl.h:38
auto GetMode()
Definition: host_mode_ctrl.h:97
void WaitAndroidMode()
Definition: host_mode_ctrl.h:62
ModeType
Definition: host_mode_ctrl.h:40
auto IsAndroidMode()
Definition: host_mode_ctrl.h:106
static HostModeCtrl & Get()
Definition: host_mode_ctrl.h:108
std::condition_variable confui_mode_cv_
Definition: host_mode_ctrl.h:116
void SetMode(const ModeType mode)
Definition: host_mode_ctrl.h:79
auto IsConfirmatioUiMode()
Definition: host_mode_ctrl.h:102
std::condition_variable and_mode_cv_
Definition: host_mode_ctrl.h:115
std::atomic< ModeType > atomic_mode_
Definition: host_mode_ctrl.h:117
std::mutex mode_mtx_
Definition: host_mode_ctrl.h:114
INJECT(HostModeCtrl())
Definition: host_mode_ctrl.h:41
#define ConfUiLog(LOG_LEVEL)
Definition: utils.h:61
#define check(p)
Definition: fec_private.h:203
@ DEBUG
Definition: logging.h:89
std::string GetName(const std::thread::id tid)
Definition: host_utils.cc:63
Definition: alloc_utils.cpp:23