royguo commented on code in PR #3168:
URL: https://github.com/apache/brpc/pull/3168#discussion_r2605190680
##########
src/bthread/task_group.h:
##########
@@ -115,6 +115,15 @@ class TaskGroup {
static void sched_to(TaskGroup** pg, bthread_t next_tid);
static void exchange(TaskGroup** pg, TaskMeta* next_meta);
+ typedef bool (*OnWorkerIdleFn)(void* user_ctx);
+ // Set a callback to run when a worker has no task to run.
+ // If the callback returns true, it means some work is done and the worker
+ // should check the runqueue again immediately.
+ // |timeout_us|: The timeout for waiting if the callback returns false.
+ // 0 is not acceptable.
+ static bool SetWorkerIdleCallback(OnWorkerIdleFn fn, void* user_ctx,
Review Comment:
refine the code by using brpc's design pattern, providing a bthread_set_xxx
function for user.
##########
src/bthread/task_group.cpp:
##########
@@ -78,6 +79,40 @@ BAIDU_VOLATILE_THREAD_LOCAL(void*, tls_unique_user_ptr,
NULL);
const TaskStatistics EMPTY_STAT = { 0, 0, 0 };
+TaskGroup::WorkerIdleConf TaskGroup::_worker_idle_conf = {NULL, NULL, 1000};
+butil::atomic<bool> TaskGroup::_worker_idle_conf_inited(false);
+
+static std::once_flag g_worker_idle_once;
+
+// Set the global static idle task, we can use thread-local variables to
distinct different
+// task group's target resource (for example, users can init thread-local
iouring per task group).
+bool TaskGroup::SetWorkerIdleCallback(OnWorkerIdleFn fn, void* user_ctx,
+ uint64_t timeout_us) {
+ // 0 timeout is not acceptable, because it will not trigger idle task.
+ if (timeout_us == 0) {
+ return false;
+ }
+
+ std::call_once(g_worker_idle_once, [fn, user_ctx, timeout_us]() {
Review Comment:
refine the code by using brpc's design pattern, providing a bthread_set_xxx
function for user.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]