Skip to main content

pw_thread/
pw_thread.rs

1// Copyright 2026 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#![no_std]
15
16//! `pw_thread` provides thread execution control utilities.
17//!
18//!  * [`sleep`] - Blocks the execution of the current thread for at least the specified duration.
19//!  * [`sleep_until`] - Blocks the execution of the current thread until the specified instant.
20//!  * [`yield_now`] - Cooperatively gives up a timeslice to the OS scheduler.
21
22use pw_time::{Duration, Instant, SystemClock};
23
24/// Blocks the execution of the current thread for at least the specified
25/// duration. This function may block for longer due to scheduling or resource
26/// contention delays.
27pub fn sleep(sleep_duration: Duration<SystemClock>) {
28    pw_thread_backend::sleep(sleep_duration);
29}
30
31/// Blocks the execution of the current thread until at least the specified
32/// time has been reached. This function may block for longer due to scheduling
33/// or resource contention delays.
34pub fn sleep_until(wakeup_time: Instant<SystemClock>) {
35    pw_thread_backend::sleep_until(wakeup_time);
36}
37
38/// Cooperatively gives up a timeslice to the OS scheduler, allowing other
39/// threads to run.
40pub fn yield_now() {
41    pw_thread_backend::yield_now();
42}