|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // License: MIT OR Apache-2.0 |
3 | 3 |
|
4 | | -use core::{sync::atomic::Ordering, time::Duration}; |
| 4 | +use core::sync::atomic::Ordering; |
5 | 5 |
|
6 | 6 | use wdk::{nt_success, paged_code, println, wdf}; |
7 | 7 | use wdk_sys::{ |
@@ -44,15 +44,6 @@ use crate::{ |
44 | 44 | WDF_TIMER_CONFIG_SIZE, |
45 | 45 | }; |
46 | 46 |
|
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 | | - |
56 | 47 | /// This routine will interlock increment a value only if the current value |
57 | 48 | /// is greater then the floor value. |
58 | 49 | /// |
@@ -133,19 +124,6 @@ fn echo_interlocked_increment_gtzero(target: &AtomicI32) -> i32 { |
133 | 124 | /// * `NTSTATUS` |
134 | 125 | #[link_section = "PAGE"] |
135 | 126 | 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 | | - |
149 | 127 | paged_code!(); |
150 | 128 |
|
151 | 129 | let mut queue = WDF_NO_HANDLE as WDFQUEUE; |
@@ -222,7 +200,7 @@ pub unsafe fn echo_queue_initialize(device: WDFDEVICE) -> NTSTATUS { |
222 | 200 | let mut timer_config = WDF_TIMER_CONFIG { |
223 | 201 | Size: WDF_TIMER_CONFIG_SIZE, |
224 | 202 | EvtTimerFunc: Some(echo_evt_timer_func), |
225 | | - Period: TIMER_PERIOD_10_S, |
| 203 | + Period: 10_000, // 10 seconds, in milliseconds |
226 | 204 | AutomaticSerialization: u8::from(true), |
227 | 205 | TolerableDelay: 0, |
228 | 206 | ..WDF_TIMER_CONFIG::default() |
@@ -372,8 +350,7 @@ fn echo_set_current_request(request: WDFREQUEST, queue: WDFQUEUE) { |
372 | 350 | // they will interlock decrement the count. When the count reaches zero, |
373 | 351 | // ownership has been acquired and the caller may complete the request. |
374 | 352 | 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); |
377 | 354 | } |
378 | 355 |
|
379 | 356 | // Defer the completion to another thread from the timer dpc |
@@ -722,12 +699,11 @@ unsafe extern "C" fn echo_evt_timer_func(timer: WDFTIMER) { |
722 | 699 | // currently racing with it), there is no need to use an interlocked |
723 | 700 | // decrement to lower the cancel ownership count. |
724 | 701 |
|
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. |
728 | 704 | (*request_context) |
729 | 705 | .cancel_completion_ownership_count |
730 | | - .fetch_sub(TIMER_CLAIMED_OWNERSHIP_COUNT, Ordering::SeqCst); |
| 706 | + .fetch_sub(2, Ordering::SeqCst); |
731 | 707 | complete_request = true; |
732 | 708 | } |
733 | 709 | } |
|
0 commit comments