From fef7aed8fa7ed2d2b7a6de10d6860b1341026b9c Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Sat, 20 Jan 2018 13:51:21 -0500 Subject: [PATCH] BeesNote: if thread name was not set, get it from Task or pthread_getname_np Threads from the Task module in libcrucible don't set BeesNote::tl_name. Even if they did, in Task context the thread name is unspecific to the point of meaninglessness. Use the Task::print method as the name for such threads, and be sure that future Task print functions are designed for that usage. The extra complexity in BeesNote::get_name() seems preferable to bombarding pthread_setname_np hundreds or thousands of times per second. FIXME: we are now calling Task::print() on every BeesNote, which is effectively unconditionally. Maybe we should have Task::print() and get_name() return a closure, or just evaluate Task::print() once and cache it in TaskState, or define Task's constructor with a string argument instead of the current print_fn closure. Signed-off-by: Zygo Blaxell --- src/bees.cc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/bees.cc b/src/bees.cc index d66ddaa..1c4c165 100644 --- a/src/bees.cc +++ b/src/bees.cc @@ -120,7 +120,7 @@ BeesNote::~BeesNote() BeesNote::BeesNote(function f) : m_func(f) { - m_name = tl_name; + m_name = get_name(); m_prev = tl_next; tl_next = this; unique_lock lock(s_mutex); @@ -137,10 +137,16 @@ string BeesNote::get_name() { if (tl_name.empty()) { - return "bees"; - } else { - return tl_name; + char buf[100]; + memset(buf, '\0', sizeof(buf)); + pthread_getname_np(pthread_self(), buf, sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + tl_name = buf; + if (tl_name.empty()) { + tl_name = "bees"; + } } + return tl_name; } BeesNote::ThreadStatusMap