@@ -111,7 +111,13 @@ impl Thread {
111
111
}
112
112
113
113
pub fn sleep ( dur : Duration ) {
114
- let tick_rate = unsafe { xPortGetTickRateHz ( ) } ;
114
+ use crate :: cmp;
115
+
116
+ let nanos = dur. as_nanos ( ) ;
117
+
118
+ if nanos == 0 {
119
+ return ;
120
+ }
115
121
116
122
let millis;
117
123
@@ -121,29 +127,34 @@ impl Thread {
121
127
fn ets_delay_us ( us : u32 ) ;
122
128
}
123
129
124
- let mut micros = dur. subsec_micros ( ) ;
125
- millis = micros / 1000 ;
126
- micros = micros % 1000 ;
130
+ let mut micros = cmp:: max ( 1_000 , nanos) / 1_000 ;
131
+ let amt = ( micros % 1_000 ) ;
127
132
128
- if micros > 0 {
129
- unsafe { ets_delay_us ( micros) } ;
133
+ if amt > 0 {
134
+ micros -= amt;
135
+ unsafe { ets_delay_us ( amt as u32 ) } ;
130
136
}
137
+
138
+ if micros == 0 {
139
+ return ;
140
+ }
141
+
142
+ millis = cmp:: max ( 1_000 , micros) / 1_000 ;
131
143
}
132
144
133
145
#[ cfg( not( target_arch = "xtensa" ) ) ]
134
146
{
135
- millis = dur . subsec_millis ( ) ;
147
+ millis = cmp :: max ( 1_000_000 , nanos ) / 1_000_000 ;
136
148
}
137
149
138
- let mut ticks_to_delay = u128 :: from ( dur . as_secs ( ) ) * u128:: from ( tick_rate )
139
- + u128 :: from ( ( ( millis + 1 ) * tick_rate - 1 ) ) / u128 :: from ( tick_rate) ;
150
+ let tick_rate = unsafe { xPortGetTickRateHz ( ) } as u128 ;
151
+ let mut ticks_to_delay = ( ( millis + 1 ) * tick_rate - 1 ) / tick_rate;
140
152
141
- while ticks_to_delay > u128:: from ( crate :: u32:: MAX ) {
142
- ticks_to_delay -= u128:: from ( crate :: u32:: MAX ) ;
143
- unsafe { vTaskDelay ( crate :: u32:: MAX ) } ;
153
+ while ticks_to_delay > 0 {
154
+ let amt = cmp:: min ( u32:: max_value ( ) as u128 , ticks_to_delay) ;
155
+ ticks_to_delay -= amt;
156
+ unsafe { vTaskDelay ( amt as u32 ) } ;
144
157
}
145
-
146
- unsafe { vTaskDelay ( ticks_to_delay as u32 ) } ;
147
158
}
148
159
149
160
pub fn join ( self ) {
0 commit comments