1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @file sleep.h
18  *
19  * @brief Declares the sleep and yield interfaces in C++.
20  *
21  * @since 10
22  * @version 1.0
23  */
24 #ifndef FFRT_API_CPP_SLEEP_H
25 #define FFRT_API_CPP_SLEEP_H
26 #include <chrono>
27 #include "c/sleep.h"
28 
29 namespace ffrt {
30 namespace this_task {
yield()31 static inline void yield()
32 {
33     ffrt_yield();
34 }
35 
36 template <class _Rep, class _Period>
sleep_for(const std::chrono::duration<_Rep,_Period> & d)37 inline void sleep_for(const std::chrono::duration<_Rep, _Period>& d)
38 {
39     ffrt_usleep(std::chrono::duration_cast<std::chrono::microseconds>(d).count());
40 }
41 
42 template<class _Clock, class _Duration>
sleep_until(const std::chrono::time_point<_Clock,_Duration> & abs_time)43 inline void sleep_until(
44     const std::chrono::time_point<_Clock, _Duration>& abs_time)
45 {
46     sleep_for(abs_time.time_since_epoch() - _Clock::now().time_since_epoch());
47 }
48 } // namespace this_task
49 } // namespace ffrt
50 #endif
51