| 66 | }; |
| 67 | |
| 68 | class RpcMaybeThread { |
| 69 | public: |
| 70 | RpcMaybeThread() = default; |
| 71 | |
| 72 | template <typename Function, typename... Args> |
| 73 | RpcMaybeThread(Function&& f, Args&&... args) { |
| 74 | // std::function requires a copy-constructible closure, |
| 75 | // so we need to wrap both the function and its arguments |
| 76 | // in a shared pointer that std::function can copy internally |
| 77 | struct Vars { |
| 78 | std::decay_t<Function> f; |
| 79 | std::tuple<std::decay_t<Args>...> args; |
| 80 | |
| 81 | explicit Vars(Function&& f, Args&&... args) |
| 82 | : f(std::move(f)), args(std::move(args)...) {} |
| 83 | }; |
| 84 | auto vars = std::make_shared<Vars>(std::forward<Function>(f), std::forward<Args>(args)...); |
| 85 | mFunc = [vars]() { std::apply(std::move(vars->f), std::move(vars->args)); }; |
| 86 | } |
| 87 | |
| 88 | void join() { |
| 89 | if (mFunc) { |
| 90 | // Move mFunc into a temporary so we can clear mFunc before |
| 91 | // executing the callback. This avoids infinite recursion if |
| 92 | // the callee then calls join() again directly or indirectly. |
| 93 | decltype(mFunc) func = nullptr; |
| 94 | mFunc.swap(func); |
| 95 | func(); |
| 96 | } |
| 97 | } |
| 98 | void detach() { join(); } |
| 99 | |
| 100 | class id { |
| 101 | public: |
| 102 | bool operator==(const id&) const { return true; } |
| 103 | bool operator!=(const id&) const { return false; } |
| 104 | bool operator<(const id&) const { return false; } |
| 105 | bool operator<=(const id&) const { return true; } |
| 106 | bool operator>(const id&) const { return false; } |
| 107 | bool operator>=(const id&) const { return true; } |
| 108 | }; |
| 109 | |
| 110 | id get_id() const { return id(); } |
| 111 | |
| 112 | private: |
| 113 | std::function<void(void)> mFunc; |
| 114 | }; |
| 115 | |
| 116 | namespace rpc_this_thread { |
| 117 | static inline RpcMaybeThread::id get_id() { |
nothing calls this directly
no outgoing calls
no test coverage detected