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: v15.1.0~1181^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=2bd106ec0da52e7fcf616d7b3cb20d570c1a5c50;p=ceph-ci.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 --- diff --git a/src/common/Thread.h b/src/common/Thread.h index bc32755c30e..0ab65fca52f 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