From c75c575be400dce6ccd0c5403e0aaa143d6a858f Mon Sep 17 00:00:00 2001 From: Alexey Spiridonov Date: Wed, 17 May 2017 15:56:39 -0700 Subject: [PATCH] ThreadedRepeatingFunctionRunner: Name all the threads Summary: Name all repeating function threads for ease of debugging. Reviewed By: yfeldblum Differential Revision: D5065281 fbshipit-source-id: e875e654dfa644a265e44416baf5fbf23c9da434 --- .../ThreadedRepeatingFunctionRunner.cpp | 16 +++++++++++----- .../ThreadedRepeatingFunctionRunner.h | 4 +++- .../test/ThreadedRepeatingFunctionRunnerTest.cpp | 4 ++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/folly/experimental/ThreadedRepeatingFunctionRunner.cpp b/folly/experimental/ThreadedRepeatingFunctionRunner.cpp index a309f01a..82ab4081 100644 --- a/folly/experimental/ThreadedRepeatingFunctionRunner.cpp +++ b/folly/experimental/ThreadedRepeatingFunctionRunner.cpp @@ -15,6 +15,7 @@ */ #include "folly/experimental/ThreadedRepeatingFunctionRunner.h" +#include #include #include @@ -53,13 +54,18 @@ bool ThreadedRepeatingFunctionRunner::stopImpl() { } void ThreadedRepeatingFunctionRunner::add( + std::string name, RepeatingFn fn, std::chrono::milliseconds initialSleep) { - threads_.emplace_back( - &ThreadedRepeatingFunctionRunner::executeInLoop, - this, - std::move(fn), - initialSleep); + threads_.emplace_back([ + name = std::move(name), + fn = std::move(fn), + initialSleep, + this + ]() mutable { + setThreadName(name); + executeInLoop(std::move(fn), initialSleep); + }); } bool ThreadedRepeatingFunctionRunner::waitFor( diff --git a/folly/experimental/ThreadedRepeatingFunctionRunner.h b/folly/experimental/ThreadedRepeatingFunctionRunner.h index 75122dae..52c7461b 100644 --- a/folly/experimental/ThreadedRepeatingFunctionRunner.h +++ b/folly/experimental/ThreadedRepeatingFunctionRunner.h @@ -120,7 +120,8 @@ class ThreadedRepeatingFunctionRunner final { /** * Run your noexcept function `f` in a background loop, sleeping between * calls for a duration returned by `f`. Optionally waits for - * `initialSleep` before calling `f` for the first time. + * `initialSleep` before calling `f` for the first time. Names the thread + * using up to the first 15 chars of `name`. * * DANGER: If a non-final class has a ThreadedRepeatingFunctionRunner * member (which, by the way, must be declared last in the class), then @@ -130,6 +131,7 @@ class ThreadedRepeatingFunctionRunner final { * your threads. */ void add( + std::string name, RepeatingFn f, std::chrono::milliseconds initialSleep = std::chrono::milliseconds(0)); diff --git a/folly/experimental/test/ThreadedRepeatingFunctionRunnerTest.cpp b/folly/experimental/test/ThreadedRepeatingFunctionRunnerTest.cpp index 28c79f39..41d6e243 100644 --- a/folly/experimental/test/ThreadedRepeatingFunctionRunnerTest.cpp +++ b/folly/experimental/test/ThreadedRepeatingFunctionRunnerTest.cpp @@ -27,7 +27,7 @@ struct Foo { } void start() { - runner_.add([this]() { + runner_.add("Foo", [this]() { ++data; return std::chrono::seconds(0); }); @@ -45,7 +45,7 @@ struct FooLongSleep { } void start() { - runner_.add([this]() { + runner_.add("FooLongSleep", [this]() { data.store(1); return 1000h; // Test would time out if we waited }); -- 2.34.1