13
13
//
14
14
15
15
#include <emscripten/html5.h>
16
+ #include <errno.h>
16
17
#include <pthread.h>
17
18
#include <stddef.h>
18
19
#include <stdio.h>
@@ -69,7 +70,12 @@ z_result_t _z_mutex_try_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_try
69
70
z_result_t _z_mutex_unlock (_z_mutex_t * m ) { _Z_CHECK_SYS_ERR (pthread_mutex_unlock (m )); }
70
71
71
72
/*------------------ Condvar ------------------*/
72
- z_result_t _z_condvar_init (_z_condvar_t * cv ) { _Z_CHECK_SYS_ERR (pthread_cond_init (cv , 0 )); }
73
+ z_result_t _z_condvar_init (_z_condvar_t * cv ) {
74
+ pthread_condattr_t attr ;
75
+ pthread_condattr_init (& attr );
76
+ pthread_condattr_setclock (& attr , CLOCK_MONOTONIC );
77
+ _Z_CHECK_SYS_ERR (pthread_cond_init (cv , & attr ));
78
+ }
73
79
74
80
z_result_t _z_condvar_drop (_z_condvar_t * cv ) { _Z_CHECK_SYS_ERR (pthread_cond_destroy (cv )); }
75
81
@@ -78,6 +84,26 @@ z_result_t _z_condvar_signal(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_s
78
84
z_result_t _z_condvar_signal_all (_z_condvar_t * cv ) { _Z_CHECK_SYS_ERR (pthread_cond_broadcast (cv )); }
79
85
80
86
z_result_t _z_condvar_wait (_z_condvar_t * cv , _z_mutex_t * m ) { _Z_CHECK_SYS_ERR (pthread_cond_wait (cv , m )); }
87
+
88
+ z_result_t _z_condvar_wait_until (_z_condvar_t * cv , _z_mutex_t * m , const z_clock_t * abstime , bool * timeout ) {
89
+ struct timespec ts ;
90
+ ts .tv_sec = (time_t )(* abstime / 1000 );
91
+ ts .tv_nsec = (long )((* abstime - (ts .tv_sec * 1000 )) * 1000000 );
92
+
93
+ int error = pthread_cond_timedwait (cv , m , & ts );
94
+
95
+ if (error == ETIMEDOUT ) {
96
+ if (timeout != NULL ) {
97
+ * timeout = true;
98
+ }
99
+ return 0 ;
100
+ }
101
+
102
+ if (timeout != NULL ) {
103
+ * timeout = false;
104
+ }
105
+ _Z_CHECK_SYS_ERR (error );
106
+ }
81
107
#endif // Z_FEATURE_MULTI_THREAD == 1
82
108
83
109
/*------------------ Sleep ------------------*/
@@ -111,7 +137,13 @@ unsigned long z_clock_elapsed_us(z_clock_t *instant) { return z_clock_elapsed_ms
111
137
112
138
unsigned long z_clock_elapsed_ms (z_clock_t * instant ) { return z_time_elapsed_ms (instant ); }
113
139
114
- unsigned long z_clock_elapsed_s (z_clock_t * instant ) { return z_time_elapsed_ms (instant ) * 1000 ; }
140
+ unsigned long z_clock_elapsed_s (z_clock_t * instant ) { return z_time_elapsed_ms (instant ) / 1000 ; }
141
+
142
+ void z_clock_advance_us (z_clock_t * clock , unsigned long duration ) { * clock += (double )(duration / 1000 ); }
143
+
144
+ void z_clock_advance_ms (z_clock_t * clock , unsigned long duration ) { * clock += (double )duration ; }
145
+
146
+ void z_clock_advance_s (z_clock_t * clock , unsigned long duration ) { * clock += (double )(duration * 1000 ); }
115
147
116
148
/*------------------ Time ------------------*/
117
149
z_time_t z_time_now (void ) { return emscripten_get_now (); }
0 commit comments