Android-cuttlefish cvd tool
interruptible_terminal.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2023 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 <condition_variable>
20#include <mutex>
21#include <optional>
22#include <string>
23#include <thread>
24
26
27namespace cuttlefish {
28
30 public:
31 /*
32 * Returns a line from the stdin_fd, which is the client stdin
33 *
34 * Notes:
35 * 1. Only up to one thread can call this function, so each handler
36 * should have its own copy
37 * 2. Each handler release the interrupt_lock before calling
38 * ReadLine(), get the interrupt lock again afterwards, and
39 * check the interrupted_ flag
40 *
41 */
43
44 private:
45 bool interrupted_ = false;
46 // one owner per InterruptibleTerminal
47 // also protecting interrupted_
48 std::mutex terminal_mutex_;
49 std::optional<std::thread::id> owner_tid_;
50 std::condition_variable readline_done_;
51};
52
53} // namespace cuttlefish
Definition: interruptible_terminal.h:29
Result< std::string > ReadLine()
Definition: interruptible_terminal.cpp:37
std::optional< std::thread::id > owner_tid_
Definition: interruptible_terminal.h:49
bool interrupted_
Definition: interruptible_terminal.h:45
std::condition_variable readline_done_
Definition: interruptible_terminal.h:50
std::mutex terminal_mutex_
Definition: interruptible_terminal.h:48
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30