You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by zy...@apache.org on 2019/04/19 03:31:59 UTC
[incubator-brpc] branch master updated: support custom executor in
execution_queue
This is an automated email from the ASF dual-hosted git repository.
zychen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-brpc.git
The following commit(s) were added to refs/heads/master by this push:
new 8c1d522 support custom executor in execution_queue
new 6a54545 Merge pull request #667 from mikeyang2015/master
8c1d522 is described below
commit 8c1d522809a329de35996de82eebaf68eae27a32
Author: Mengmeng Yang <mi...@gmail.com>
AuthorDate: Sat Feb 23 11:49:41 2019 +0800
support custom executor in execution_queue
---
src/bthread/execution_queue.cpp | 25 ++++++++++++++++---------
src/bthread/execution_queue.h | 13 +++++++++++++
src/bthread/execution_queue_inl.h | 2 +-
3 files changed, 30 insertions(+), 10 deletions(-)
diff --git a/src/bthread/execution_queue.cpp b/src/bthread/execution_queue.cpp
index 4685056..a87362c 100644
--- a/src/bthread/execution_queue.cpp
+++ b/src/bthread/execution_queue.cpp
@@ -101,15 +101,22 @@ void ExecutionQueueBase::start_execute(TaskNode* node) {
}
}
- bthread_t tid;
- // We start the execution thread in background instead of foreground as
- // we can't determine whether the code after execute() is urgent (like
- // unlock a pthread_mutex_t) in which case implicit context switch may
- // cause undefined behavior (e.g. deadlock)
- if (bthread_start_background(&tid, &_options.bthread_attr,
- _execute_tasks, node) != 0) {
- PLOG(FATAL) << "Fail to start bthread";
- _execute_tasks(node);
+ if (nullptr == _options.executor) {
+ bthread_t tid;
+ // We start the execution thread in background instead of foreground as
+ // we can't determine whether the code after execute() is urgent (like
+ // unlock a pthread_mutex_t) in which case implicit context switch may
+ // cause undefined behavior (e.g. deadlock)
+ if (bthread_start_background(&tid, &_options.bthread_attr,
+ _execute_tasks, node) != 0) {
+ PLOG(FATAL) << "Fail to start bthread";
+ _execute_tasks(node);
+ }
+ } else {
+ if (_options.executor->submit(_execute_tasks, node) != 0) {
+ PLOG(FATAL) << "Fail to submit task";
+ _execute_tasks(node);
+ }
}
}
diff --git a/src/bthread/execution_queue.h b/src/bthread/execution_queue.h
index c463645..a9515f2 100644
--- a/src/bthread/execution_queue.h
+++ b/src/bthread/execution_queue.h
@@ -128,11 +128,24 @@ const static TaskOptions TASK_OPTIONS_NORMAL = TaskOptions(false, false);
const static TaskOptions TASK_OPTIONS_URGENT = TaskOptions(true, false);
const static TaskOptions TASK_OPTIONS_INPLACE = TaskOptions(false, true);
+class Executor {
+public:
+ virtual ~Executor() {}
+
+ // Return 0 on success.
+ virtual int submit(void * (*fn)(void*), void* args) = 0;
+};
+
struct ExecutionQueueOptions {
ExecutionQueueOptions();
// Attribute of the bthread which execute runs on
// default: BTHREAD_ATTR_NORMAL
bthread_attr_t bthread_attr;
+
+ // Executor that tasks run on. bthread will be used when executor = NULL.
+ // Note that TaskOptions.in_place_if_possible = false will not work, if implementation of
+ // Executor is in-place(synchronous).
+ Executor * executor;
};
// Start a ExecutionQueue. If |options| is NULL, the queue will be created with
diff --git a/src/bthread/execution_queue_inl.h b/src/bthread/execution_queue_inl.h
index 75d9935..429ad25 100644
--- a/src/bthread/execution_queue_inl.h
+++ b/src/bthread/execution_queue_inl.h
@@ -317,7 +317,7 @@ public:
};
inline ExecutionQueueOptions::ExecutionQueueOptions()
- : bthread_attr(BTHREAD_ATTR_NORMAL)
+ : bthread_attr(BTHREAD_ATTR_NORMAL), executor(NULL)
{}
template <typename T>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org