From: Adam C. Emerson Date: Tue, 22 Oct 2019 15:39:20 +0000 (-0400) Subject: common/thread: Fix race condition in make_named_thread X-Git-Tag: v13.2.7~7^2~9 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=eed8d5eabb579daad89c12ad8a5797ac5fb9876d;p=ceph.git common/thread: Fix race condition in make_named_thread The thread may well no longer exist by the time we try to set the name, so have the thread set its own name first thing. Thanks to Ilya Dryomov for pointing it out. Signed-off-by: Adam C. Emerson (cherry picked from commit 2bd106ec0da52e7fcf616d7b3cb20d570c1a5c50) --- diff --git a/src/common/Thread.h b/src/common/Thread.h index 8e7cadd14c3b..c219d7142077 100644 --- a/src/common/Thread.h +++ b/src/common/Thread.h @@ -16,6 +16,8 @@ #ifndef CEPH_THREAD_H #define CEPH_THREAD_H +#include +#include #include #include @@ -68,13 +70,14 @@ std::string get_thread_name(const std::thread& t); void kill(std::thread& t, int signal); template -std::thread make_named_thread(const std::string& s, +std::thread make_named_thread(std::string_view n, Fun&& fun, Args&& ...args) { - auto t = std::thread(std::forward(fun), - std::forward(args)...); - set_thread_name(t, s); - return t; -} + return std::thread([n = std::string(n)](auto&& fun, auto&& ...args) { + ceph_pthread_setname(pthread_self(), n.data()); + std::invoke(std::forward(fun), + std::forward(args)...); + }, std::forward(fun), std::forward(args)...); +} #endif