Commit c75c575b authored by Alexey Spiridonov's avatar Alexey Spiridonov Committed by Facebook Github Bot

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
parent 6c244d16
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
#include "folly/experimental/ThreadedRepeatingFunctionRunner.h" #include "folly/experimental/ThreadedRepeatingFunctionRunner.h"
#include <folly/ThreadName.h>
#include <glog/logging.h> #include <glog/logging.h>
#include <iostream> #include <iostream>
...@@ -53,13 +54,18 @@ bool ThreadedRepeatingFunctionRunner::stopImpl() { ...@@ -53,13 +54,18 @@ bool ThreadedRepeatingFunctionRunner::stopImpl() {
} }
void ThreadedRepeatingFunctionRunner::add( void ThreadedRepeatingFunctionRunner::add(
std::string name,
RepeatingFn fn, RepeatingFn fn,
std::chrono::milliseconds initialSleep) { std::chrono::milliseconds initialSleep) {
threads_.emplace_back( threads_.emplace_back([
&ThreadedRepeatingFunctionRunner::executeInLoop, name = std::move(name),
this, fn = std::move(fn),
std::move(fn), initialSleep,
initialSleep); this
]() mutable {
setThreadName(name);
executeInLoop(std::move(fn), initialSleep);
});
} }
bool ThreadedRepeatingFunctionRunner::waitFor( bool ThreadedRepeatingFunctionRunner::waitFor(
......
...@@ -120,7 +120,8 @@ class ThreadedRepeatingFunctionRunner final { ...@@ -120,7 +120,8 @@ class ThreadedRepeatingFunctionRunner final {
/** /**
* Run your noexcept function `f` in a background loop, sleeping between * Run your noexcept function `f` in a background loop, sleeping between
* calls for a duration returned by `f`. Optionally waits for * 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 * DANGER: If a non-final class has a ThreadedRepeatingFunctionRunner
* member (which, by the way, must be declared last in the class), then * member (which, by the way, must be declared last in the class), then
...@@ -130,6 +131,7 @@ class ThreadedRepeatingFunctionRunner final { ...@@ -130,6 +131,7 @@ class ThreadedRepeatingFunctionRunner final {
* your threads. * your threads.
*/ */
void add( void add(
std::string name,
RepeatingFn f, RepeatingFn f,
std::chrono::milliseconds initialSleep = std::chrono::milliseconds(0)); std::chrono::milliseconds initialSleep = std::chrono::milliseconds(0));
......
...@@ -27,7 +27,7 @@ struct Foo { ...@@ -27,7 +27,7 @@ struct Foo {
} }
void start() { void start() {
runner_.add([this]() { runner_.add("Foo", [this]() {
++data; ++data;
return std::chrono::seconds(0); return std::chrono::seconds(0);
}); });
...@@ -45,7 +45,7 @@ struct FooLongSleep { ...@@ -45,7 +45,7 @@ struct FooLongSleep {
} }
void start() { void start() {
runner_.add([this]() { runner_.add("FooLongSleep", [this]() {
data.store(1); data.store(1);
return 1000h; // Test would time out if we waited return 1000h; // Test would time out if we waited
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment