Use string for callback location info

This commit is contained in:
Attila Uygun 2023-06-12 14:52:44 +02:00
parent fdbbb2a6fd
commit 83400a0b52
5 changed files with 15 additions and 20 deletions

View File

@ -12,12 +12,11 @@
// Helper for logging location info, e.g. LOG << LOCATION(from) // Helper for logging location info, e.g. LOG << LOCATION(from)
#define LOCATION(from) \ #define LOCATION(from) \
std::get<0>(from) << "() [" << [](const char* path) -> std::string { \ std::get<0>(from) << "() [" << [](std::string path) -> std::string { \
std::string file_name(path); \ size_t last_slash_pos = path.find_last_of("\\/"); \
size_t last_slash_pos = file_name.find_last_of("\\/"); \
if (last_slash_pos != std::string::npos) \ if (last_slash_pos != std::string::npos) \
file_name = file_name.substr(last_slash_pos + 1); \ path = path.substr(last_slash_pos + 1); \
return file_name; \ return path; \
}(std::get<1>(from)) << ":" \ }(std::get<1>(from)) << ":" \
<< std::get<2>(from) << "]" << std::get<2>(from) << "]"
@ -36,7 +35,7 @@ using Closure = std::function<void()>;
// Provides location info (function name, file name and line number) where of a // Provides location info (function name, file name and line number) where of a
// Closure was constructed. // Closure was constructed.
using Location = std::tuple<const char*, const char*, int>; using Location = std::tuple<std::string, std::string, int>;
#else #else

View File

@ -36,7 +36,7 @@ TaskRunner* TaskRunner::GetThreadLocalTaskRunner() {
return thread_local_task_runner.get(); return thread_local_task_runner.get();
} }
void TaskRunner::PostTask(const Location& from, Closure task) { void TaskRunner::PostTask(Location from, Closure task) {
DCHECK(task) << LOCATION(from); DCHECK(task) << LOCATION(from);
task_count_.fetch_add(1, std::memory_order_relaxed); task_count_.fetch_add(1, std::memory_order_relaxed);
@ -44,9 +44,7 @@ void TaskRunner::PostTask(const Location& from, Closure task) {
queue_.emplace_back(from, std::move(task)); queue_.emplace_back(from, std::move(task));
} }
void TaskRunner::PostTaskAndReply(const Location& from, void TaskRunner::PostTaskAndReply(Location from, Closure task, Closure reply) {
Closure task,
Closure reply) {
DCHECK(task) << LOCATION(from); DCHECK(task) << LOCATION(from);
DCHECK(reply) << LOCATION(from); DCHECK(reply) << LOCATION(from);
DCHECK(thread_local_task_runner) << LOCATION(from); DCHECK(thread_local_task_runner) << LOCATION(from);

View File

@ -42,12 +42,12 @@ class TaskRunner {
TaskRunner() = default; TaskRunner() = default;
~TaskRunner() = default; ~TaskRunner() = default;
void PostTask(const Location& from, Closure task); void PostTask(Location from, Closure task);
void PostTaskAndReply(const Location& from, Closure task, Closure reply); void PostTaskAndReply(Location from, Closure task, Closure reply);
template <typename ReturnType> template <typename ReturnType>
void PostTaskAndReplyWithResult(const Location& from, void PostTaskAndReplyWithResult(Location from,
std::function<ReturnType()> task, std::function<ReturnType()> task,
std::function<void(ReturnType)> reply) { std::function<void(ReturnType)> reply) {
auto* result = new ReturnType; auto* result = new ReturnType;

View File

@ -40,16 +40,14 @@ void ThreadPool::Shutdown() {
threads_.clear(); threads_.clear();
} }
void ThreadPool::PostTask(const Location& from, Closure task) { void ThreadPool::PostTask(Location from, Closure task) {
DCHECK((!threads_.empty())); DCHECK((!threads_.empty()));
task_runner_.PostTask(from, std::move(task)); task_runner_.PostTask(from, std::move(task));
semaphore_.release(); semaphore_.release();
} }
void ThreadPool::PostTaskAndReply(const Location& from, void ThreadPool::PostTaskAndReply(Location from, Closure task, Closure reply) {
Closure task,
Closure reply) {
DCHECK((!threads_.empty())); DCHECK((!threads_.empty()));
task_runner_.PostTaskAndReply(from, std::move(task), std::move(reply)); task_runner_.PostTaskAndReply(from, std::move(task), std::move(reply));

View File

@ -26,12 +26,12 @@ class ThreadPool {
void Shutdown(); void Shutdown();
void PostTask(const Location& from, Closure task); void PostTask(Location from, Closure task);
void PostTaskAndReply(const Location& from, Closure task, Closure reply); void PostTaskAndReply(Location from, Closure task, Closure reply);
template <typename ReturnType> template <typename ReturnType>
void PostTaskAndReplyWithResult(const Location& from, void PostTaskAndReplyWithResult(Location from,
std::function<ReturnType()> task, std::function<ReturnType()> task,
std::function<void(ReturnType)> reply) { std::function<void(ReturnType)> reply) {
task_runner_.PostTaskAndReplyWithResult(from, std::move(task), task_runner_.PostTaskAndReplyWithResult(from, std::move(task),