@@ -90,52 +90,28 @@ mod time {
90
90
Ok ( Date :: now ( ) / 1000.0 )
91
91
}
92
92
93
- #[ cfg( not( any(
94
- target_os = "macos" ,
95
- target_os = "android" ,
96
- target_os = "dragonfly" ,
97
- target_os = "freebsd" ,
98
- target_os = "linux" ,
99
- ) ) ) ]
93
+ #[ cfg( any( windows, target_os = "wasi" ) ) ]
100
94
#[ pyfunction]
101
95
fn monotonic ( vm : & VirtualMachine ) -> PyResult < f64 > {
102
96
// TODO: implement proper monotonic time for other platforms.
103
97
Ok ( duration_since_system_now ( vm) ?. as_secs_f64 ( ) )
104
98
}
105
99
106
- #[ cfg( not( any(
107
- target_os = "macos" ,
108
- target_os = "android" ,
109
- target_os = "dragonfly" ,
110
- target_os = "freebsd" ,
111
- target_os = "linux" ,
112
- ) ) ) ]
100
+ #[ cfg( any( windows, target_os = "wasi" ) ) ]
113
101
#[ pyfunction]
114
102
fn monotonic_ns ( vm : & VirtualMachine ) -> PyResult < u128 > {
115
103
// TODO: implement proper monotonic time for other platforms.
116
104
Ok ( duration_since_system_now ( vm) ?. as_nanos ( ) )
117
105
}
118
106
119
- #[ cfg( not( any(
120
- target_os = "macos" ,
121
- target_os = "android" ,
122
- target_os = "dragonfly" ,
123
- target_os = "freebsd" ,
124
- target_os = "linux" ,
125
- ) ) ) ]
107
+ #[ cfg( any( windows, target_os = "wasi" ) ) ]
126
108
#[ pyfunction]
127
109
fn perf_counter ( vm : & VirtualMachine ) -> PyResult < f64 > {
128
110
// TODO: implement proper monotonic time for other platforms.
129
111
Ok ( duration_since_system_now ( vm) ?. as_secs_f64 ( ) )
130
112
}
131
113
132
- #[ cfg( not( any(
133
- target_os = "macos" ,
134
- target_os = "android" ,
135
- target_os = "dragonfly" ,
136
- target_os = "freebsd" ,
137
- target_os = "linux" ,
138
- ) ) ) ]
114
+ #[ cfg( any( windows, target_os = "wasi" ) ) ]
139
115
#[ pyfunction]
140
116
fn perf_counter_ns ( vm : & VirtualMachine ) -> PyResult < u128 > {
141
117
// TODO: implement proper monotonic time for other platforms.
@@ -251,8 +227,8 @@ mod time {
251
227
target_os = "dragonfly" ,
252
228
target_os = "freebsd" ,
253
229
target_os = "linux" ,
254
- target_os = "openbsd " ,
255
- target_os = "solaris " ,
230
+ target_os = "fuchsia " ,
231
+ target_os = "emscripten " ,
256
232
) ) ) ]
257
233
fn get_thread_time ( vm : & VirtualMachine ) -> PyResult < std:: time:: Duration > {
258
234
Err ( vm. new_not_implemented_error ( "thread time unsupported in this system" . to_owned ( ) ) )
@@ -420,8 +396,10 @@ mod time {
420
396
mod unix {
421
397
#[ allow( unused_imports) ]
422
398
use super :: { SEC_TO_NS , US_TO_NS } ;
399
+ #[ cfg_attr( target_os = "macos" , allow( unused_imports) ) ]
423
400
use crate :: {
424
401
builtins:: { try_bigint_to_f64, PyFloat , PyIntRef , PyNamespace , PyStrRef } ,
402
+ stdlib:: os,
425
403
utils:: Either ,
426
404
PyRef , PyResult , VirtualMachine ,
427
405
} ;
@@ -430,47 +408,42 @@ mod unix {
430
408
#[ cfg( target_os = "solaris" ) ]
431
409
#[ pyattr]
432
410
use libc:: CLOCK_HIGHRES ;
433
- #[ cfg( not( target_os = "redox" ) ) ]
411
+ #[ cfg( not( any(
412
+ target_os = "illumos" ,
413
+ target_os = "netbsd" ,
414
+ target_os = "solaris" ,
415
+ target_os = "openbsd" ,
416
+ ) ) ) ]
417
+ #[ pyattr]
434
418
use libc:: CLOCK_PROCESS_CPUTIME_ID ;
419
+ #[ cfg( not( any(
420
+ target_os = "illumos" ,
421
+ target_os = "netbsd" ,
422
+ target_os = "solaris" ,
423
+ target_os = "openbsd" ,
424
+ target_os = "redox" ,
425
+ ) ) ) ]
426
+ #[ pyattr]
427
+ use libc:: CLOCK_THREAD_CPUTIME_ID ;
435
428
#[ cfg( target_os = "linux" ) ]
436
429
#[ pyattr]
437
430
use libc:: { CLOCK_BOOTTIME , CLOCK_MONOTONIC_RAW , CLOCK_TAI } ;
438
431
#[ pyattr]
439
- use libc:: { CLOCK_MONOTONIC , CLOCK_REALTIME , CLOCK_THREAD_CPUTIME_ID } ;
440
- #[ cfg( target_os = "redox" ) ]
441
- // TODO: will be upstreamed to libc sometime soon
442
- const CLOCK_PROCESS_CPUTIME_ID : libc:: clockid_t = 2 ;
443
- #[ cfg( any( target_os = "freebsd" , target_os = "openbsd" ) ) ]
432
+ use libc:: { CLOCK_MONOTONIC , CLOCK_REALTIME } ;
433
+ #[ cfg( any( target_os = "freebsd" , target_os = "openbsd" , target_os = "dragonfly" ) ) ]
444
434
#[ pyattr]
445
435
use libc:: { CLOCK_PROF , CLOCK_UPTIME } ;
446
436
447
- #[ cfg( any(
448
- target_os = "macos" ,
449
- target_os = "android" ,
450
- target_os = "dragonfly" ,
451
- target_os = "freebsd" ,
452
- target_os = "linux" ,
453
- ) ) ]
454
437
fn get_clock_time ( clk_id : PyIntRef , vm : & VirtualMachine ) -> PyResult < Duration > {
455
438
let mut timespec = std:: mem:: MaybeUninit :: uninit ( ) ;
456
439
let ts: libc:: timespec = unsafe {
457
440
if libc:: clock_gettime ( clk_id. try_to_primitive ( vm) ?, timespec. as_mut_ptr ( ) ) == -1 {
458
- return Err ( vm . new_os_error ( "Invalid argument" . to_owned ( ) ) ) ;
441
+ return Err ( os :: errno_err ( vm ) ) ;
459
442
}
460
443
timespec. assume_init ( )
461
444
} ;
462
445
Ok ( Duration :: new ( ts. tv_sec as u64 , ts. tv_nsec as u32 ) )
463
446
}
464
- #[ cfg( not( any(
465
- target_os = "macos" ,
466
- target_os = "android" ,
467
- target_os = "dragonfly" ,
468
- target_os = "freebsd" ,
469
- target_os = "linux" ,
470
- ) ) ) ]
471
- fn get_clock_time ( _clk_id : PyIntRef , vm : & VirtualMachine ) -> PyResult < Duration > {
472
- Err ( vm. new_not_implemented_error ( "clock_gettime unsupported on this system" . to_owned ( ) ) )
473
- }
474
447
475
448
#[ pyfunction]
476
449
fn clock_gettime ( clk_id : PyIntRef , vm : & VirtualMachine ) -> PyResult < f64 > {
@@ -482,66 +455,33 @@ mod unix {
482
455
get_clock_time ( clk_id, vm) . map ( |d| d. as_nanos ( ) )
483
456
}
484
457
485
- #[ cfg( any(
486
- target_os = "macos" ,
487
- target_os = "android" ,
488
- target_os = "dragonfly" ,
489
- target_os = "freebsd" ,
490
- target_os = "linux" ,
491
- ) ) ]
458
+ #[ cfg( not( target_os = "redox" ) ) ]
492
459
#[ pyfunction]
493
460
fn clock_getres ( clk_id : PyIntRef , vm : & VirtualMachine ) -> PyResult < f64 > {
494
461
let mut timespec = std:: mem:: MaybeUninit :: uninit ( ) ;
495
462
let ts: libc:: timespec = unsafe {
496
463
if libc:: clock_getres ( clk_id. try_to_primitive ( vm) ?, timespec. as_mut_ptr ( ) ) == -1 {
497
- return Err ( vm . new_os_error ( "Invalid argument" . to_owned ( ) ) ) ;
464
+ return Err ( os :: errno_err ( vm ) ) ;
498
465
}
499
466
timespec. assume_init ( )
500
467
} ;
501
468
Ok ( Duration :: new ( ts. tv_sec as u64 , ts. tv_nsec as u32 ) . as_secs_f64 ( ) )
502
469
}
503
470
504
- #[ cfg( not( any(
505
- target_os = "macos" ,
506
- target_os = "android" ,
507
- target_os = "dragonfly" ,
508
- target_os = "freebsd" ,
509
- target_os = "linux" ,
510
- ) ) ) ]
511
- #[ pyfunction]
512
- fn clock_getres ( _clk_id : PyIntRef , vm : & VirtualMachine ) -> PyResult < f64 > {
513
- Err ( vm. new_not_implemented_error ( "clock_getres unsupported on this system" . to_owned ( ) ) )
514
- }
515
-
516
- #[ cfg( any(
517
- target_os = "macos" ,
518
- target_os = "android" ,
519
- target_os = "dragonfly" ,
520
- target_os = "freebsd" ,
521
- target_os = "linux" ,
522
- ) ) ]
471
+ #[ cfg( not( any( target_os = "macos" , target_os = "redox" ) ) ) ]
523
472
fn set_clock_time (
524
473
clk_id : PyIntRef ,
525
474
timespec : libc:: timespec ,
526
475
vm : & VirtualMachine ,
527
476
) -> PyResult < ( ) > {
528
477
let res = unsafe { libc:: clock_settime ( clk_id. try_to_primitive ( vm) ?, & timespec) } ;
529
478
if res == -1 {
530
- return Err ( vm . new_os_error ( "Invalid argument" . to_owned ( ) ) ) ;
479
+ return Err ( os :: errno_err ( vm ) ) ;
531
480
}
532
481
Ok ( ( ) )
533
482
}
534
- #[ cfg( not( any(
535
- target_os = "macos" ,
536
- target_os = "android" ,
537
- target_os = "dragonfly" ,
538
- target_os = "freebsd" ,
539
- target_os = "linux" ,
540
- ) ) ) ]
541
- fn set_clock_time ( _clk_id : PyIntRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
542
- Err ( vm. new_not_implemented_error ( "clock_settime unsupported on this system" . to_owned ( ) ) )
543
- }
544
483
484
+ #[ cfg( not( any( target_os = "macos" , target_os = "redox" ) ) ) ]
545
485
#[ pyfunction]
546
486
fn clock_settime (
547
487
clk_id : PyIntRef ,
@@ -560,21 +500,25 @@ mod unix {
560
500
set_clock_time ( clk_id, ts, vm)
561
501
}
562
502
503
+ #[ cfg( not( any( target_os = "macos" , target_os = "redox" ) ) ) ]
563
504
#[ pyfunction]
564
505
fn clock_settime_ns ( clk_id : PyIntRef , time : PyIntRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
565
- let time: i64 = time. try_to_primitive ( vm) ?;
506
+ let time: libc :: time_t = time. try_to_primitive ( vm) ?;
566
507
let ts = libc:: timespec {
567
- tv_sec : time / SEC_TO_NS as libc:: time_t ,
568
- tv_nsec : time. rem_euclid ( SEC_TO_NS ) as _ ,
508
+ tv_sec : time / ( SEC_TO_NS as libc:: time_t ) ,
509
+ tv_nsec : time. rem_euclid ( SEC_TO_NS as libc :: time_t ) as _ ,
569
510
} ;
570
511
set_clock_time ( clk_id, ts, vm)
571
512
}
572
513
514
+ // Requires all CLOCK constants available and clock_getres
573
515
#[ cfg( any(
574
516
target_os = "macos" ,
575
517
target_os = "android" ,
576
518
target_os = "dragonfly" ,
577
519
target_os = "freebsd" ,
520
+ target_os = "fuchsia" ,
521
+ target_os = "emscripten" ,
578
522
target_os = "linux" ,
579
523
) ) ]
580
524
#[ pyfunction]
@@ -620,56 +564,30 @@ mod unix {
620
564
target_os = "android" ,
621
565
target_os = "dragonfly" ,
622
566
target_os = "freebsd" ,
567
+ target_os = "fuchsia" ,
568
+ target_os = "emscripten" ,
623
569
target_os = "linux" ,
624
570
) ) ) ]
625
571
#[ pyfunction]
626
572
fn get_clock_info ( _name : PyStrRef , vm : & VirtualMachine ) -> PyResult < PyNamespace > {
627
573
Err ( vm. new_not_implemented_error ( "get_clock_info unsupported on this system" . to_owned ( ) ) )
628
574
}
629
575
630
- #[ cfg( any(
631
- target_os = "macos" ,
632
- target_os = "android" ,
633
- target_os = "dragonfly" ,
634
- target_os = "freebsd" ,
635
- target_os = "linux" ,
636
- ) ) ]
637
576
#[ pyfunction]
638
577
fn monotonic ( vm : & VirtualMachine ) -> PyResult < f64 > {
639
578
clock_gettime ( vm. ctx . new_int ( CLOCK_MONOTONIC ) , vm)
640
579
}
641
580
642
- #[ cfg( any(
643
- target_os = "macos" ,
644
- target_os = "android" ,
645
- target_os = "dragonfly" ,
646
- target_os = "freebsd" ,
647
- target_os = "linux" ,
648
- ) ) ]
649
581
#[ pyfunction]
650
582
fn monotonic_ns ( vm : & VirtualMachine ) -> PyResult < u128 > {
651
583
clock_gettime_ns ( vm. ctx . new_int ( CLOCK_MONOTONIC ) , vm)
652
584
}
653
585
654
- #[ cfg( any(
655
- target_os = "macos" ,
656
- target_os = "android" ,
657
- target_os = "dragonfly" ,
658
- target_os = "freebsd" ,
659
- target_os = "linux" ,
660
- ) ) ]
661
586
#[ pyfunction]
662
587
fn perf_counter ( vm : & VirtualMachine ) -> PyResult < f64 > {
663
588
clock_gettime ( vm. ctx . new_int ( CLOCK_MONOTONIC ) , vm)
664
589
}
665
590
666
- #[ cfg( any(
667
- target_os = "macos" ,
668
- target_os = "android" ,
669
- target_os = "dragonfly" ,
670
- target_os = "freebsd" ,
671
- target_os = "linux" ,
672
- ) ) ]
673
591
#[ pyfunction]
674
592
fn perf_counter_ns ( vm : & VirtualMachine ) -> PyResult < u128 > {
675
593
clock_gettime_ns ( vm. ctx . new_int ( CLOCK_MONOTONIC ) , vm)
@@ -693,14 +611,12 @@ mod unix {
693
611
Ok ( ( ) )
694
612
}
695
613
696
- #[ cfg( any(
697
- target_os = "macos" ,
698
- target_os = "android" ,
699
- target_os = "dragonfly" ,
700
- target_os = "freebsd" ,
701
- target_os = "linux" ,
614
+ #[ cfg( not( any(
615
+ target_os = "illumos" ,
616
+ target_os = "netbsd" ,
702
617
target_os = "openbsd" ,
703
- ) ) ]
618
+ target_os = "redox"
619
+ ) ) ) ]
704
620
pub ( super ) fn get_thread_time ( vm : & VirtualMachine ) -> PyResult < Duration > {
705
621
let time: libc:: timespec = unsafe {
706
622
let mut time = std:: mem:: MaybeUninit :: uninit ( ) ;
@@ -717,14 +633,12 @@ mod unix {
717
633
Ok ( Duration :: from_nanos ( unsafe { libc:: gethrvtime ( ) } ) )
718
634
}
719
635
720
- #[ cfg( any(
721
- target_os = "macos" ,
722
- target_os = "android" ,
723
- target_os = "dragonfly" ,
724
- target_os = "freebsd" ,
725
- target_os = "linux" ,
726
- target_os = "redox" ,
727
- ) ) ]
636
+ #[ cfg( not( any(
637
+ target_os = "illumos" ,
638
+ target_os = "netbsd" ,
639
+ target_os = "solaris" ,
640
+ target_os = "openbsd" ,
641
+ ) ) ) ]
728
642
pub ( super ) fn get_process_time ( vm : & VirtualMachine ) -> PyResult < Duration > {
729
643
let time: libc:: timespec = unsafe {
730
644
let mut time = std:: mem:: MaybeUninit :: uninit ( ) ;
0 commit comments