Skip to content

Commit ff7dc08

Browse files
committed
Add emscripten implementation
1 parent 59f88aa commit ff7dc08

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/system/emscripten/system.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//
1414

1515
#include <emscripten/html5.h>
16+
#include <errno.h>
1617
#include <pthread.h>
1718
#include <stddef.h>
1819
#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
6970
z_result_t _z_mutex_unlock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_unlock(m)); }
7071

7172
/*------------------ 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+
}
7379

7480
z_result_t _z_condvar_drop(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_destroy(cv)); }
7581

@@ -78,6 +84,26 @@ z_result_t _z_condvar_signal(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_s
7884
z_result_t _z_condvar_signal_all(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_broadcast(cv)); }
7985

8086
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+
}
81107
#endif // Z_FEATURE_MULTI_THREAD == 1
82108

83109
/*------------------ Sleep ------------------*/
@@ -111,7 +137,13 @@ unsigned long z_clock_elapsed_us(z_clock_t *instant) { return z_clock_elapsed_ms
111137

112138
unsigned long z_clock_elapsed_ms(z_clock_t *instant) { return z_time_elapsed_ms(instant); }
113139

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); }
115147

116148
/*------------------ Time ------------------*/
117149
z_time_t z_time_now(void) { return emscripten_get_now(); }

0 commit comments

Comments
 (0)