11use std:: sync:: mpsc;
22
3- use enum_as_inner:: EnumAsInner ;
4- use thread_priority:: { set_current_thread_priority, ThreadPriority , WinAPIThreadPriority } ;
5- // use windows::Win32::System::Threading::{ SetThreadPriority, GetCurrentThread, THREAD_PRIORITY };
3+ #[ cfg( target_os = "windows" ) ]
4+ use thread_priority:: { set_current_thread_priority, ThreadPriority , WinAPIThreadPriority } ;
5+ #[ cfg( target_os = "windows" ) ]
6+ use windows:: Win32 :: System :: Threading :: { GetCurrentThread , SetThreadPriority , THREAD_PRIORITY } ;
67
78use crate :: actions:: ESActionWrapper ;
8- use crate :: mechanism:: { DownCmd , Mechanism , MechanismImpl , ScheCmd , SimEnvObserve , UpCmd } ;
9+ use crate :: mechanism:: { DownCmd , Mechanism , MechanismImpl , ScheCmd , SimEnvObserve , UpCmd } ;
910
1011use crate :: util;
1112use crate :: with_env_sub:: WithEnvHelp ;
@@ -43,6 +44,8 @@ pub fn spawn(mech: MechanismImpl) -> mpsc::Sender<MechScheduleOnce> {
4344 // THREAD_PRIORITY(WinAPIThreadPriority::TimeCritical as i32)
4445 // ).unwrap();
4546 // }
47+
48+ #[ cfg( target_os = "windows" ) ]
4649 if let Err ( e) = set_current_thread_priority ( ThreadPriority :: Max ) {
4750 eprintln ! ( "设置线程优先级失败: {:?}" , e) ;
4851 }
@@ -71,7 +74,11 @@ fn mechanism_loop(rx: mpsc::Receiver<MechScheduleOnce>, mech: MechanismImpl) {
7174 // let passed_ms = measure.passed_100ns();
7275 let end_ms = util:: now_ms ( ) ;
7376 // log::info!("master mech run cpu:{:?}, total:{} ms", begin_cpu.elapsed(), end_ms - begin_ms);
74- let mech_latency = if mech. config . no_mech_latency { 0 } else { end_ms - begin_ms } ;
77+ let mech_latency = if mech. config . no_mech_latency {
78+ 0
79+ } else {
80+ end_ms - begin_ms
81+ } ;
7582 res. responser
7683 . send ( MechScheduleOnceRes :: End {
7784 mech_run_ms : mech_latency,
@@ -85,11 +92,15 @@ fn mechanism_loop(rx: mpsc::Receiver<MechScheduleOnce>, mech: MechanismImpl) {
8592pub mod tests {
8693 use std:: sync:: mpsc;
8794
88- use crate :: { actions:: ESActionWrapper , mechanism_thread:: MechScheduleOnceRes , sim_env:: SimEnv } ;
95+ use crate :: { actions:: ESActionWrapper , mechanism_thread:: MechScheduleOnceRes , sim_env:: SimEnv } ;
8996
9097 #[ test]
9198 pub fn test_algo_latency ( ) {
92- use std:: { cell:: RefCell , rc:: Rc , sync:: { atomic:: AtomicU64 , Arc } } ;
99+ use std:: {
100+ cell:: RefCell ,
101+ rc:: Rc ,
102+ sync:: { atomic:: AtomicU64 , Arc } ,
103+ } ;
93104
94105 use crate :: config:: Config ;
95106 let _ = env_logger:: try_init ( ) ;
@@ -120,24 +131,20 @@ pub mod tests {
120131 ESActionWrapper :: Int ( 0 ) ,
121132 None ,
122133 None ,
123- Some (
124- Box :: new ( move |env : & SimEnv | {
125- * begin_frame. borrow_mut ( ) = env. current_frame ( ) ;
126- } )
127- ) ,
128- Some (
129- Box :: new ( move |env : & SimEnv | {
130- // calltime = env.current_frame() - begin_frame;
131- assert ! (
132- env. current_frame( ) - * begin_frame2. borrow( ) == calltime,
133- "begin_frame:{} current_frame:{} calltime:{}" ,
134- begin_frame2. borrow( ) ,
135- env. current_frame( ) ,
136- calltime
137- ) ;
138- calltime += 1 ;
139- } )
140- )
134+ Some ( Box :: new ( move |env : & SimEnv | {
135+ * begin_frame. borrow_mut ( ) = env. current_frame ( ) ;
136+ } ) ) ,
137+ Some ( Box :: new ( move |env : & SimEnv | {
138+ // calltime = env.current_frame() - begin_frame;
139+ assert ! (
140+ env. current_frame( ) - * begin_frame2. borrow( ) == calltime,
141+ "begin_frame:{} current_frame:{} calltime:{}" ,
142+ begin_frame2. borrow( ) ,
143+ env. current_frame( ) ,
144+ calltime
145+ ) ;
146+ calltime += 1 ;
147+ } ) ) ,
141148 ) ;
142149 }
143150}
0 commit comments