Skip to content

Commit 56c09b8

Browse files
author
Alan Ngo
committed
- make naming and comments more clear
- revert unnecessary const's to literals w/improved comments
1 parent 09877f8 commit 56c09b8

2 files changed

Lines changed: 17 additions & 37 deletions

File tree

general/echo/kmdf/driver/DriverSync/src/device.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,22 @@ pub fn echo_device_create(mut device_init: &mut WDFDEVICE_INIT) -> NTSTATUS {
148148
///
149149
/// * `NTSTATUS` - Failures will result in the device stack being torn down.
150150
extern "C" fn echo_evt_device_self_managed_io_start(device: WDFDEVICE) -> NTSTATUS {
151-
/// 100ms relative time (in 100-nanosecond units). The negative sign marks
152-
/// the value as a relative (rather than absolute) timeout.
151+
/// 100ms relative due time for a WDF timer, in system time units.
153152
#[allow(
154153
clippy::cast_possible_truncation,
155154
reason = "100ms in 100-nanosecond units is known to fit in i64"
156155
)]
157-
const WDF_REL_TIMEOUT_100_MS: i64 = {
158-
const UNITS: u128 = Duration::from_millis(100).as_nanos() / 100;
156+
const WDF_TIMER_DUE_TIME_100_MS: i64 = {
157+
// System time units are 100-nanosecond intervals.
158+
const SYSTEM_TIME_UNITS: u128 = Duration::from_millis(100).as_nanos() / 100;
159159
const {
160-
assert!(UNITS <= i64::MAX as u128, "1,000,000 should fit in i64");
160+
assert!(
161+
SYSTEM_TIME_UNITS <= i64::MAX as u128,
162+
"SYSTEM_TIME_UNITS should fit in i64"
163+
);
161164
};
162-
-(UNITS as i64)
165+
// Negative marks the value as a relative (rather than absolute) timeout.
166+
-(SYSTEM_TIME_UNITS as i64)
163167
};
164168

165169
// Restart the queue and the periodic timer. We stopped them before going
@@ -178,7 +182,7 @@ extern "C" fn echo_evt_device_self_managed_io_start(device: WDFDEVICE) -> NTSTAT
178182
// into low power state.
179183
unsafe { call_unsafe_wdf_function_binding!(WdfIoQueueStart, queue) };
180184

181-
let due_time: i64 = WDF_REL_TIMEOUT_100_MS;
185+
let due_time: i64 = WDF_TIMER_DUE_TIME_100_MS;
182186

183187
let _ = unsafe { (*queue_context).timer.start(due_time) };
184188

general/echo/kmdf/driver/DriverSync/src/queue.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// License: MIT OR Apache-2.0
33

4-
use core::{sync::atomic::Ordering, time::Duration};
4+
use core::sync::atomic::Ordering;
55

66
use wdk::{nt_success, paged_code, println, wdf};
77
use wdk_sys::{
@@ -44,15 +44,6 @@ use crate::{
4444
WDF_TIMER_CONFIG_SIZE,
4545
};
4646

47-
/// Initial cancel/completion ownership count assigned to a new request. A
48-
/// claimant takes ownership by decrementing the count down to zero.
49-
const INITIAL_CANCEL_OWNERSHIP_COUNT: i32 = 1;
50-
51-
/// Total ownership count held by the timer DPC once it has claimed completion
52-
/// of a request: the initial count plus the single increment it acquired via
53-
/// `echo_increment_request_cancel_ownership_count`.
54-
const TIMER_CLAIMED_OWNERSHIP_COUNT: i32 = INITIAL_CANCEL_OWNERSHIP_COUNT + 1;
55-
5647
/// This routine will interlock increment a value only if the current value
5748
/// is greater then the floor value.
5849
///
@@ -133,19 +124,6 @@ fn echo_interlocked_increment_gtzero(target: &AtomicI32) -> i32 {
133124
/// * `NTSTATUS`
134125
#[link_section = "PAGE"]
135126
pub unsafe fn echo_queue_initialize(device: WDFDEVICE) -> NTSTATUS {
136-
/// Timer period of 10 seconds in ms
137-
#[allow(
138-
clippy::cast_possible_truncation,
139-
reason = "10 seconds in millisecond units is known to fit in u32"
140-
)]
141-
const TIMER_PERIOD_10_S: u32 = {
142-
const MILLIS: u128 = Duration::from_secs(10).as_millis();
143-
const {
144-
assert!(MILLIS <= u32::MAX as u128, "10,000 should fit in u32");
145-
};
146-
MILLIS as u32
147-
};
148-
149127
paged_code!();
150128

151129
let mut queue = WDF_NO_HANDLE as WDFQUEUE;
@@ -222,7 +200,7 @@ pub unsafe fn echo_queue_initialize(device: WDFDEVICE) -> NTSTATUS {
222200
let mut timer_config = WDF_TIMER_CONFIG {
223201
Size: WDF_TIMER_CONFIG_SIZE,
224202
EvtTimerFunc: Some(echo_evt_timer_func),
225-
Period: TIMER_PERIOD_10_S,
203+
Period: 10_000, // 10 seconds, in milliseconds
226204
AutomaticSerialization: u8::from(true),
227205
TolerableDelay: 0,
228206
..WDF_TIMER_CONFIG::default()
@@ -372,8 +350,7 @@ fn echo_set_current_request(request: WDFREQUEST, queue: WDFQUEUE) {
372350
// they will interlock decrement the count. When the count reaches zero,
373351
// ownership has been acquired and the caller may complete the request.
374352
unsafe {
375-
(*request_context).cancel_completion_ownership_count =
376-
AtomicI32::new(INITIAL_CANCEL_OWNERSHIP_COUNT);
353+
(*request_context).cancel_completion_ownership_count = AtomicI32::new(1);
377354
}
378355

379356
// Defer the completion to another thread from the timer dpc
@@ -722,12 +699,11 @@ unsafe extern "C" fn echo_evt_timer_func(timer: WDFTIMER) {
722699
// currently racing with it), there is no need to use an interlocked
723700
// decrement to lower the cancel ownership count.
724701

725-
// TIMER_CLAIMED_OWNERSHIP_COUNT is the initial count we set when we
726-
// initialized CancelCompletionOwnershipCount plus the call to
727-
// EchoIncrementRequestCancelOwnershipCount()
702+
// 2 = the initial ownership count (1) plus the one increment
703+
// acquired via echo_increment_request_cancel_ownership_count.
728704
(*request_context)
729705
.cancel_completion_ownership_count
730-
.fetch_sub(TIMER_CLAIMED_OWNERSHIP_COUNT, Ordering::SeqCst);
706+
.fetch_sub(2, Ordering::SeqCst);
731707
complete_request = true;
732708
}
733709
}

0 commit comments

Comments
 (0)