-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathbthread_executor.h
54 lines (46 loc) · 1.56 KB
/
bthread_executor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include "babylon/executor.h"
#include "babylon/logging/logger.h"
#include "bthread/bthread.h"
#include "butex_interface.h"
BABYLON_NAMESPACE_BEGIN
// 使用bthread线程池执行的executor
class BthreadExecutor : public Executor {
public:
inline static BthreadExecutor& instance() noexcept {
static BthreadExecutor bthread_executor;
return bthread_executor;
}
protected:
// 启动bthread,并运行function
virtual int invoke(
MoveOnlyFunction<void(void)>&& function) noexcept override {
auto args = new MoveOnlyFunction<void(void)>(::std::move(function));
::bthread_t th;
if (0 != ::bthread_start_background(&th, NULL, run_function, args)) {
BABYLON_LOG(WARNING) << "start bthread to execute failed";
function = ::std::move(*args);
delete args;
return -1;
}
return 0;
}
private:
// 线程函数
inline static void* run_function(void* args) noexcept {
auto function = reinterpret_cast<MoveOnlyFunction<void(void)>*>(args);
(*function)();
delete function;
return nullptr;
}
};
// 使用bthread线程池的async
template <typename S = ::babylon::ButexInterface, typename C, typename... Args>
inline Future<typename ::std::result_of<typename ::std::decay<C>::type(
typename ::std::decay<Args>::type...)>::type,
S>
bthread_async(C&& callable, Args&&... args) noexcept {
return BthreadExecutor::instance().execute<S>(::std::forward<C>(callable),
::std::forward<Args>(args)...);
}
BABYLON_NAMESPACE_END