Apollo 10.0
自动驾驶开放平台
task.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#ifndef CYBER_TASK_TASK_H_
18#define CYBER_TASK_TASK_H_
19
20#include <future>
21#include <utility>
22
24
25namespace apollo {
26namespace cyber {
27
29
30template <typename F, typename... Args>
31static auto Async(F&& f, Args&&... args)
32 -> std::future<typename std::result_of<F(Args...)>::type> {
33 return GlobalData::Instance()->IsRealityMode()
34 ? TaskManager::Instance()->Enqueue(std::forward<F>(f),
35 std::forward<Args>(args)...)
36 : std::async(
37 std::launch::async,
38 std::bind(std::forward<F>(f), std::forward<Args>(args)...));
39}
40
41static inline void Yield() {
44 } else {
45 std::this_thread::yield();
46 }
47}
48
49template <typename Rep, typename Period>
50static void SleepFor(const std::chrono::duration<Rep, Period>& sleep_duration) {
52 if (routine == nullptr) {
53 std::this_thread::sleep_for(sleep_duration);
54 } else {
55 routine->Sleep(sleep_duration);
56 }
57}
58
59static inline void USleep(useconds_t usec) {
61 if (routine == nullptr) {
62 std::this_thread::sleep_for(std::chrono::microseconds{usec});
63 } else {
64 routine->Sleep(croutine::Duration(usec));
65 }
66}
67
68} // namespace cyber
69} // namespace apollo
70
71#endif // CYBER_TASK_TASK_H_
double f
static CRoutine * GetCurrentRoutine()
Definition croutine.h:135
std::chrono::microseconds Duration
Definition croutine.h:36
class register implement
Definition arena_queue.h:37