-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use ThreadFactory to auto set thread attributes #2496
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
203bcd1
update
627faf3
update
fuzhe1989 595ea0e
update
fuzhe1989 6779214
update
fuzhe1989 df1aeb5
update
fuzhe1989 1d6f4d7
address comment
fuzhe1989 e07a765
Merge branch 'master' into fuzhe/thread_creator
fuzhe1989 26ff5d2
Merge branch 'master' into fuzhe/thread_creator
ti-srebot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#pragma once | ||
|
||
#include <Common/MemoryTracker.h> | ||
#include <Common/setThreadName.h> | ||
#include <common/ThreadPool.h> | ||
#include <thread> | ||
|
||
namespace DB | ||
{ | ||
|
||
/// ThreadCreator helps to set attributes on new threads or threadpool's jobs. | ||
/// Current supported attributes: | ||
/// 1. MemoryTracker | ||
/// 2. ThreadName | ||
/// | ||
/// ThreadCreator should only be constructed on stack. | ||
class ThreadCreator | ||
{ | ||
public: | ||
/// force_overwrite_thread_attribute is only used for ThreadPool's jobs. | ||
/// For new threads it is treated as always true. | ||
explicit ThreadCreator(bool force_overwrite_thread_attribute = false, std::string thread_name_ = "") | ||
: force_overwrite(force_overwrite_thread_attribute), thread_name(thread_name_) {} | ||
|
||
ThreadCreator(const ThreadCreator &) = delete; | ||
ThreadCreator & operator=(const ThreadCreator &) = delete; | ||
|
||
ThreadCreator(ThreadCreator &&) = default; | ||
ThreadCreator & operator=(ThreadCreator &&) = default; | ||
|
||
template <typename F, typename ... Args> | ||
std::thread newThread(F && f, Args &&... args) | ||
{ | ||
auto memory_tracker = current_memory_tracker; | ||
auto wrapped_func = [memory_tracker, thread_name = thread_name, f = std::move(f)](Args &&... args) | ||
{ | ||
setAttributes(memory_tracker, thread_name, true); | ||
return std::invoke(f, std::forward<Args>(args)...); | ||
}; | ||
return std::thread(wrapped_func, std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename F, typename ... Args> | ||
ThreadPool::Job newJob(F && f, Args &&... args) | ||
{ | ||
auto memory_tracker = current_memory_tracker; | ||
/// Use std::tuple to workaround the limit on the lambda's init-capture of C++17. | ||
/// See https://stackoverflow.com/questions/47496358/c-lambdas-how-to-capture-variadic-parameter-pack-from-the-upper-scope | ||
return [force_overwrite = force_overwrite, memory_tracker, thread_name = thread_name, f = std::move(f), args = std::make_tuple(std::move(args)...)] | ||
{ | ||
setAttributes(memory_tracker, thread_name, force_overwrite); | ||
std::apply(f, std::move(args)); | ||
}; | ||
} | ||
|
||
void scheduleJob(ThreadPool & pool, ThreadPool::Job job) | ||
{ | ||
pool.schedule(newJob(job)); | ||
} | ||
private: | ||
static void setAttributes(MemoryTracker * memory_tracker, const std::string & thread_name, bool force_overwrite) | ||
{ | ||
if (force_overwrite || !current_memory_tracker) | ||
{ | ||
current_memory_tracker = memory_tracker; | ||
if (!thread_name.empty()) | ||
setThreadName(thread_name.c_str()); | ||
} | ||
} | ||
|
||
bool force_overwrite = false; | ||
std::string thread_name; | ||
}; | ||
|
||
} // namespace DB | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe name with
ThreadFactory
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done