Skip to content

Commit

Permalink
ThreadedRepeatingFunctionRunner: Name all the threads
Browse files Browse the repository at this point in the history
Summary: Name all repeating function threads for ease of debugging.

Reviewed By: yfeldblum

Differential Revision: D5065281

fbshipit-source-id: e875e654dfa644a265e44416baf5fbf23c9da434
  • Loading branch information
snarkmaster authored and facebook-github-bot committed May 17, 2017
1 parent 6c244d1 commit c75c575
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
16 changes: 11 additions & 5 deletions folly/experimental/ThreadedRepeatingFunctionRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
#include "folly/experimental/ThreadedRepeatingFunctionRunner.h"

#include <folly/ThreadName.h>
#include <glog/logging.h>
#include <iostream>

Expand Down Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion folly/experimental/ThreadedRepeatingFunctionRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -130,6 +131,7 @@ class ThreadedRepeatingFunctionRunner final {
* your threads.
*/
void add(
std::string name,
RepeatingFn f,
std::chrono::milliseconds initialSleep = std::chrono::milliseconds(0));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Foo {
}

void start() {
runner_.add([this]() {
runner_.add("Foo", [this]() {
++data;
return std::chrono::seconds(0);
});
Expand All @@ -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
});
Expand Down

0 comments on commit c75c575

Please sign in to comment.