Android-cuttlefish cvd tool
host_utils.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 <functional>
20#include <map>
21#include <mutex>
22#include <optional>
23#include <sstream>
24#include <string>
25#include <thread>
26
31
32namespace cuttlefish {
33namespace confui {
34
35namespace thread {
36/* thread id to name
37 * these three functions internally uses the singleton ThreadTracer object.
38 *
39 * When running a thread, use the global RunThread function
40 */
41std::string GetName(std::thread::id tid = std::this_thread::get_id());
42std::optional<std::thread::id> GetId(const std::string& name);
43void Set(const std::string& name, std::thread::id tid);
44
45/*
46 * This is wrapping std::thread. However, we keep the bidirectional map
47 * between the given thread name and the thread id. The main purpose is
48 * to help debugging.
49 *
50 */
51template <typename F, typename... Args>
52std::thread RunThread(const std::string& name, F&& f, Args&&... args);
53
54class ThreadTracer;
55ThreadTracer& GetThreadTracer();
56
59 friend std::string GetName(std::thread::id tid);
60 friend std::optional<std::thread::id> GetId(const std::string& name);
61 friend void Set(const std::string& name, std::thread::id tid);
62
63 template <typename F, typename... Args>
64 friend std::thread RunThread(const std::string& name, F&& f, Args&&... args);
65
66 private:
67 template <typename F, typename... Args>
68 std::thread RunThread(const std::string& name, F&& f, Args&&... args) {
69 auto th = std::thread(std::forward<F>(f), std::forward<Args>(args)...);
70 if (Contains(name2id_, name)) {
71 ConfUiLog(FATAL) << "Thread name is duplicated";
72 }
73 name2id_[name] = th.get_id();
74 id2name_[th.get_id()] = name;
75 ConfUiLogDebug << name << "thread started.";
76 return th;
77 }
78 std::string Get(std::thread::id id = std::this_thread::get_id());
79 std::optional<std::thread::id> Get(const std::string& name);
80
81 // add later on even though it wasn't started with RunThread
82 // if tid is already added, update the name only
83 void Set(const std::string& name, std::thread::id tid);
84
85 ThreadTracer() = default;
86 std::map<std::thread::id, std::string> id2name_;
87 std::map<std::string, std::thread::id> name2id_;
88 std::mutex mtx_;
89};
90
91template <typename F, typename... Args>
92std::thread RunThread(const std::string& name, F&& f, Args&&... args) {
93 auto& tracer = GetThreadTracer();
94 return tracer.RunThread(name, std::forward<F>(f),
95 std::forward<Args>(args)...);
96}
97
98} // namespace thread
99} // namespace confui
100} // namespace cuttlefish
Definition: host_utils.h:57
friend std::thread RunThread(const std::string &name, F &&f, Args &&... args)
Definition: host_utils.h:92
std::mutex mtx_
Definition: host_utils.h:88
std::thread RunThread(const std::string &name, F &&f, Args &&... args)
Definition: host_utils.h:68
std::map< std::string, std::thread::id > name2id_
Definition: host_utils.h:87
friend std::optional< std::thread::id > GetId(const std::string &name)
Definition: host_utils.cc:67
friend ThreadTracer & GetThreadTracer()
Definition: host_utils.cc:58
std::string Get(std::thread::id id=std::this_thread::get_id())
Definition: host_utils.cc:22
friend std::string GetName(std::thread::id tid)
Definition: host_utils.cc:63
std::map< std::thread::id, std::string > id2name_
Definition: host_utils.h:86
friend void Set(const std::string &name, std::thread::id tid)
Definition: host_utils.cc:71
#define ConfUiLogDebug
Definition: utils.h:61
#define ConfUiLog(LOG_LEVEL)
Definition: utils.h:60
#define FATAL(x...)
Definition: image.h:31
uint32_t id
Definition: file_sync_protocol.h:0
char * name
Definition: libf2fs.c:1403
std::string GetName(const std::thread::id tid)
Definition: host_utils.cc:63
std::thread RunThread(const std::string &name, F &&f, Args &&... args)
Definition: host_utils.h:92
ThreadTracer & GetThreadTracer()
Definition: host_utils.cc:58
std::optional< std::thread::id > GetId(const std::string &name)
Definition: host_utils.cc:67
void Set(const std::string &name, const std::thread::id tid)
Definition: host_utils.cc:71
Definition: alloc_driver.h:20
constexpr bool Contains(Container &&container, U &&u)
Definition: contains.h:98
std::vector< std::string_view > Args
Definition: incremental.h:28