Apollo  6.0
Open source self driving car software
task.h
Go to the documentation of this file.
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 
25 namespace apollo {
26 namespace cyber {
27 
29 
30 template <typename F, typename... Args>
31 static 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 
41 static inline void Yield() {
44  } else {
45  std::this_thread::yield();
46  }
47 }
48 
49 template <typename Rep, typename Period>
50 static 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 
59 static 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_
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
static CRoutine * GetCurrentRoutine()
Definition: croutine.h:135
static void Yield()
Definition: croutine.h:131
std::chrono::microseconds Duration
Definition: croutine.h:36
Definition: global_data.h:40