xref: /aosp_15_r20/external/pigweed/pw_thread/public/pw_thread/sleep.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include "pw_chrono/system_clock.h"
17 #include "pw_preprocessor/util.h"
18 
19 #ifdef __cplusplus
20 
21 namespace pw::this_thread {
22 
23 // Blocks the execution of the current thread for at least the specified
24 // duration. This function may block for longer due to scheduling or resource
25 // contention delays.
26 //
27 // A sleep duration of 0 will at minimum yield, meaning it will provide a hint
28 // to the implementation to reschedule the execution of threads, allowing other
29 // threads to run.
30 //
31 // Precondition: This can only be called from a thread, meaning the scheduler
32 // is running.
33 void sleep_for(chrono::SystemClock::duration sleep_duration);
34 
35 // Blocks the execution of the current thread until at least the specified
36 // time has been reached. This function may block for longer due to scheduling
37 // or resource contention delays.
38 //
39 // A sleep deadline in the past up to the current time will at minimum yield
40 // meaning it will provide a hint to the implementation to reschedule the
41 // execution of threads, allowing other threads to run.
42 //
43 // Precondition: This can only be called from a thread, meaning the scheduler
44 // is running.
45 void sleep_until(chrono::SystemClock::time_point wakeup_time);
46 
47 }  // namespace pw::this_thread
48 
49 // The backend can opt to include inlined implementations.
50 #if __has_include("pw_thread_backend/sleep_inline.h")
51 #include "pw_thread_backend/sleep_inline.h"
52 #endif  // __has_include("pw_thread_backend/sleep_inline.h")
53 
54 #endif  // __cplusplus
55 
56 PW_EXTERN_C_START
57 
58 void pw_this_thread_SleepFor(pw_chrono_SystemClock_Duration sleep_duration);
59 void pw_this_thread_SleepUntil(pw_chrono_SystemClock_TimePoint wakeup_time);
60 
61 PW_EXTERN_C_END
62