@@ -28,6 +28,7 @@ mod time {
28
28
naive:: { NaiveDate , NaiveDateTime , NaiveTime } ,
29
29
Datelike , Timelike ,
30
30
} ;
31
+ use std:: time:: Duration ;
31
32
32
33
#[ allow( dead_code) ]
33
34
pub ( super ) const SEC_TO_MS : i64 = 1000 ;
@@ -46,7 +47,7 @@ mod time {
46
47
#[ allow( dead_code) ]
47
48
pub ( super ) const NS_TO_US : i64 = 1000 ;
48
49
49
- fn duration_since_system_now ( vm : & VirtualMachine ) -> PyResult < std :: time :: Duration > {
50
+ fn duration_since_system_now ( vm : & VirtualMachine ) -> PyResult < Duration > {
50
51
use std:: time:: { SystemTime , UNIX_EPOCH } ;
51
52
52
53
SystemTime :: now ( )
@@ -56,19 +57,19 @@ mod time {
56
57
57
58
// TODO: implement proper monotonic time for wasm/wasi.
58
59
#[ cfg( not( any( unix, windows) ) ) ]
59
- fn get_monotonic_time ( vm : & VirtualMachine ) -> PyResult < std :: time :: Duration > {
60
+ fn get_monotonic_time ( vm : & VirtualMachine ) -> PyResult < Duration > {
60
61
duration_since_system_now ( vm)
61
62
}
62
63
63
64
// TODO: implement proper perf time for wasm/wasi.
64
65
#[ cfg( not( any( unix, windows) ) ) ]
65
- fn get_perf_time ( vm : & VirtualMachine ) -> PyResult < std :: time :: Duration > {
66
+ fn get_perf_time ( vm : & VirtualMachine ) -> PyResult < Duration > {
66
67
duration_since_system_now ( vm)
67
68
}
68
69
69
70
#[ cfg( not( unix) ) ]
70
71
#[ pyfunction]
71
- fn sleep ( dur : std :: time :: Duration ) {
72
+ fn sleep ( dur : Duration ) {
72
73
std:: thread:: sleep ( dur) ;
73
74
}
74
75
@@ -233,7 +234,7 @@ mod time {
233
234
target_os = "fuchsia" ,
234
235
target_os = "emscripten" ,
235
236
) ) ) ]
236
- fn get_thread_time ( vm : & VirtualMachine ) -> PyResult < std :: time :: Duration > {
237
+ fn get_thread_time ( vm : & VirtualMachine ) -> PyResult < Duration > {
237
238
Err ( vm. new_not_implemented_error ( "thread time unsupported in this system" . to_owned ( ) ) )
238
239
}
239
240
@@ -247,34 +248,43 @@ mod time {
247
248
Ok ( get_thread_time ( vm) ?. as_nanos ( ) as u64 )
248
249
}
249
250
250
- #[ cfg( any( windows, all( target_arch = "wasm32" , not ( target_os = "unknown" ) ) ) ) ]
251
+ #[ cfg( any( windows, all( target_arch = "wasm32" , target_arch = "emscripten" ) ) ) ]
251
252
pub ( super ) fn time_muldiv ( ticks : i64 , mul : i64 , div : i64 ) -> u64 {
252
253
let intpart = ticks / div;
253
254
let ticks = ticks % div;
254
255
let remaining = ( ticks * mul) / div;
255
256
( intpart * mul + remaining) as u64
256
257
}
257
258
258
- #[ cfg( all( target_arch = "wasm32" , not ( target_os = "unknown" ) ) ) ]
259
- fn get_process_time ( vm : & VirtualMachine ) -> PyResult < std :: time :: Duration > {
259
+ #[ cfg( all( target_arch = "wasm32" , target_os = "emscripten" ) ) ]
260
+ fn get_process_time ( vm : & VirtualMachine ) -> PyResult < Duration > {
260
261
let t: libc:: tms = unsafe {
261
262
let mut t = std:: mem:: MaybeUninit :: uninit ( ) ;
262
263
if libc:: times ( t. as_mut_ptr ( ) ) == -1 {
263
264
return Err ( vm. new_os_error ( "Failed to get clock time" . to_owned ( ) ) ) ;
264
265
}
265
266
t. assume_init ( )
266
267
} ;
267
-
268
- #[ cfg( target_os = "wasi" ) ]
269
- let freq = 60 ;
270
- #[ cfg( not( target_os = "wasi" ) ) ]
271
268
let freq = unsafe { libc:: sysconf ( libc:: _SC_CLK_TCK) } ;
272
269
273
- Ok ( std :: time :: Duration :: from_nanos (
270
+ Ok ( Duration :: from_nanos (
274
271
time_muldiv ( t. tms_utime , SEC_TO_NS , freq) + time_muldiv ( t. tms_stime , SEC_TO_NS , freq) ,
275
272
) )
276
273
}
277
274
275
+ // same as the get_process_time impl for most unixes
276
+ #[ cfg( all( target_arch = "wasm32" , target_os = "wasi" ) ) ]
277
+ pub ( super ) fn get_process_time ( vm : & VirtualMachine ) -> PyResult < Duration > {
278
+ let time: libc:: timespec = unsafe {
279
+ let mut time = std:: mem:: MaybeUninit :: uninit ( ) ;
280
+ if libc:: clock_gettime ( libc:: CLOCK_PROCESS_CPUTIME_ID , time. as_mut_ptr ( ) ) == -1 {
281
+ return Err ( vm. new_os_error ( "Failed to get clock time" . to_owned ( ) ) ) ;
282
+ }
283
+ time. assume_init ( )
284
+ } ;
285
+ Ok ( Duration :: new ( time. tv_sec as u64 , time. tv_nsec as u32 ) )
286
+ }
287
+
278
288
#[ cfg( not( any(
279
289
windows,
280
290
target_os = "macos" ,
@@ -289,7 +299,7 @@ mod time {
289
299
target_os = "redox" ,
290
300
all( target_arch = "wasm32" , not( target_os = "unknown" ) )
291
301
) ) ) ]
292
- fn get_process_time ( vm : & VirtualMachine ) -> PyResult < std :: time :: Duration > {
302
+ fn get_process_time ( vm : & VirtualMachine ) -> PyResult < Duration > {
293
303
Err ( vm. new_not_implemented_error ( "process time unsupported in this system" . to_owned ( ) ) )
294
304
}
295
305
0 commit comments