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
27
28namespace cuttlefish {
29
31 public:
33 /*
34 * Returns a line from the stdin_fd, which is the client stdin
35 *
36 * Notes:
37 * 1. Only up to one thread can call this function, so each handler
38 * should have its own copy
39 * 2. Each handler release the interrupt_lock before calling
40 * ReadLine(), get the interrupt lock again afterwards, and
41 * check the interrupted_ flag
42 *
43 */
45
46 private:
48 bool interrupted_ = false;
49 // one owner per InterruptibleTerminal
50 // also protecting interrupted_
51 std::mutex terminal_mutex_;
52 std::optional<std::thread::id> owner_tid_;
53 std::condition_variable readline_done_;
54};
55
56} // namespace cuttlefish
Definition: expected.h:86
Definition: interruptible_terminal.h:30
InterruptibleTerminal()
Definition: interruptible_terminal.cpp:30
Result< std::string > ReadLine()
Definition: interruptible_terminal.cpp:34
SharedFD interrupt_event_fd_
Definition: interruptible_terminal.h:47
std::optional< std::thread::id > owner_tid_
Definition: interruptible_terminal.h:52
bool interrupted_
Definition: interruptible_terminal.h:48
std::condition_variable readline_done_
Definition: interruptible_terminal.h:53
std::mutex terminal_mutex_
Definition: interruptible_terminal.h:51
Definition: shared_fd.h:129
Definition: alloc_utils.cpp:23