File tree Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 1
1
#define _WASI_EMULATED_PROCESS_CLOCKS
2
2
#include <time.h>
3
3
#include <sys/times.h>
4
+ #ifdef __wasilibc_use_wasip2
5
+ #include <wasi/wasip2.h>
6
+ #else
4
7
#include <wasi/api.h>
8
+ #endif
5
9
#include <common/time.h>
6
10
7
11
_Static_assert (
@@ -14,6 +18,17 @@ _Static_assert(
14
18
clock_t __clock (void );
15
19
16
20
clock_t times (struct tms * buffer ) {
21
+ #ifdef __wasilibc_use_wasip2
22
+ clock_t user = __clock ();
23
+ * buffer = (struct tms ){
24
+ .tms_utime = user ,
25
+ // WASI doesn't provide a way to spawn a new process, so always 0.
26
+ .tms_cutime = 0
27
+ };
28
+
29
+ monotonic_clock_instant_t realtime = monotonic_clock_now ();
30
+ #else
31
+
17
32
__wasi_timestamp_t user = __clock ();
18
33
* buffer = (struct tms ){
19
34
.tms_utime = user ,
@@ -23,5 +38,6 @@ clock_t times(struct tms *buffer) {
23
38
24
39
__wasi_timestamp_t realtime = 0 ;
25
40
(void )__wasi_clock_time_get (__WASI_CLOCKID_MONOTONIC , 0 , & realtime );
41
+ #endif
26
42
return realtime ;
27
43
}
Original file line number Diff line number Diff line change 4
4
5
5
#include <common/time.h>
6
6
7
+ #ifdef __wasilibc_use_wasip2
8
+ #include <wasi/wasip2.h>
9
+ #else
7
10
#include <wasi/api.h>
11
+ #endif
8
12
#include <time.h>
9
13
10
14
time_t time (time_t * tloc ) {
15
+ #ifdef __wasilibc_use_wasip2
16
+ wall_clock_datetime_t res ;
17
+ wall_clock_now (& res );
18
+ uint64_t ts = (res .seconds * NSEC_PER_SEC ) + res .nanoseconds ;
19
+ #else
11
20
__wasi_timestamp_t ts = 0 ;
12
21
(void )__wasi_clock_time_get (__WASI_CLOCKID_REALTIME , NSEC_PER_SEC , & ts );
22
+ #endif
13
23
if (tloc != NULL )
14
24
* tloc = ts / NSEC_PER_SEC ;
15
25
return ts / NSEC_PER_SEC ;
Original file line number Diff line number Diff line change
1
+ //! add-flags.py(CFLAGS): -D_WASI_EMULATED_PROCESS_CLOCKS
2
+ //! add-flags.py(LDFLAGS): -lwasi-emulated-process-clocks
3
+ #include <stdlib.h>
4
+ #include <stdio.h>
5
+ #include <time.h>
6
+ #include <string.h>
7
+ #include <errno.h>
8
+ #include <sys/times.h>
9
+ #include "test.h"
10
+
11
+ #define TEST (c , ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
12
+
13
+ int main (void )
14
+ {
15
+ time_t t ;
16
+ struct tms buffer ;
17
+
18
+ clock_t result = times (& buffer );
19
+ TEST (result > 0 );
20
+ TEST (buffer .tms_utime > 0 );
21
+ TEST (buffer .tms_cutime == 0 ); // always 0 under WASI
22
+
23
+ time_t t1 = time (& t );
24
+ TEST (t == t1 );
25
+ TEST (t > 0 );
26
+
27
+ return t_status ;
28
+ }
You can’t perform that action at this time.
0 commit comments