Skip to content

Commit 01752bd

Browse files
committed
Added: Descriptions to better explain APIs
1 parent e469619 commit 01752bd

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

av1an-core/src/sleep_guard.rs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,16 @@ mod linux_impl {
9090

9191
use super::*;
9292

93-
/// Holds the D-Bus-owned inhibition file descriptor.
94-
/// The FD is closed automatically when this value is dropped.
93+
/// See https://www.freedesktop.org/wiki/Software/systemd/inhibit/
94+
/// The Inhibit method takes four arguments:
95+
/// - what: "sleep" indicates we want to prevent sleep/suspend
96+
/// - who: The application name requesting the inhibition
97+
/// - why: Human-readable reason for the inhibition
98+
/// - mode: "block" completely blocks sleep, "delay" only delays it
99+
///
100+
/// Returns a file descriptor that must be kept open to maintain the
101+
/// inhibition. The FD is closed automatically when this value is
102+
/// dropped.
95103
pub struct LinuxGuard {
96104
_fd: OwnedFd,
97105
}
@@ -125,15 +133,30 @@ mod linux_impl {
125133
mod windows_impl {
126134
use super::*;
127135

136+
/// Informs the system that the state being set should remain in effect
137+
/// until the next call that uses ES_CONTINUOUS and one of the other
138+
/// state flags is cleared.
139+
const ES_CONTINUOUS: u32 = 0x80000000;
140+
141+
/// Forces the system to be in the working state by resetting the system
142+
/// idle timer.
143+
const ES_SYSTEM_REQUIRED: u32 = 0x00000001;
144+
128145
pub struct WindowsGuard;
129146

147+
/// See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
148+
/// SetThreadExecutionState modifies the system's sleep timer behavior.
149+
/// Parameters used:
150+
/// - ES_CONTINUOUS (0x80000000): State remains in effect until the next
151+
/// call
152+
/// - ES_SYSTEM_REQUIRED (0x00000001): Forces the system to stay in working
153+
/// state
154+
///
155+
/// The system is automatically allowed to sleep again when this guard is
156+
/// dropped by clearing ES_SYSTEM_REQUIRED while maintaining
157+
/// ES_CONTINUOUS.
130158
impl WindowsGuard {
131159
pub fn new(_app: &str, _reason: &str) -> Result<Self, SleepInhibitError> {
132-
// Map scope to execution state flags.
133-
// ES_CONTINUOUS is always set to make the request sticky for this call.
134-
const ES_CONTINUOUS: u32 = 0x80000000;
135-
const ES_SYSTEM_REQUIRED: u32 = 0x00000001;
136-
137160
let flags: u32 = ES_CONTINUOUS | ES_SYSTEM_REQUIRED;
138161

139162
// SAFETY: Calling documented Windows API with constant flags.
@@ -150,7 +173,6 @@ mod windows_impl {
150173
impl Drop for WindowsGuard {
151174
fn drop(&mut self) {
152175
// Clear the requirement and keep ES_CONTINUOUS.
153-
const ES_CONTINUOUS: u32 = 0x80000000;
154176
// SAFETY: Restoring to a benign state.
155177
unsafe {
156178
windows_sys::Win32::System::Power::SetThreadExecutionState(ES_CONTINUOUS);
@@ -191,9 +213,20 @@ mod mac_impl {
191213
id: IOPMAssertionID,
192214
}
193215

216+
/// See https://developer.apple.com/documentation/iokit/1557134-IOPMAssertionCreateWithName
217+
/// IOPMAssertionCreateWithName creates a power assertion that prevents
218+
/// system sleep. Parameters:
219+
/// - assertion_type: "PreventSystemSleep" prevents the entire system from
220+
/// sleeping
221+
/// - level: 255 (kIOPMAssertionLevelOn) activates the assertion
222+
/// - assertion_name: A human-readable reason for the assertion
223+
/// - assertion_id: Returns an ID that must be released to remove the
224+
/// assertion
225+
///
226+
/// The assertion is automatically released when this guard is dropped.
194227
impl MacGuard {
195228
pub fn new(_app: &str, why: &str) -> anyhow::Result<Self> {
196-
let assertion_type = "NoIdleSleepAssertion";
229+
let assertion_type = "PreventSystemSleep";
197230

198231
let mut id: IOPMAssertionID = 0;
199232
// SAFETY: FFI call with well-formed CFStrings that live across the call.

0 commit comments

Comments
 (0)