Skip to content

Commit

Permalink
Add emscripten implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsowa committed Dec 15, 2024
1 parent 59f88aa commit 50581ea
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/system/emscripten/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ z_result_t _z_mutex_try_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_try
z_result_t _z_mutex_unlock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_unlock(m)); }

/*------------------ Condvar ------------------*/
z_result_t _z_condvar_init(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_init(cv, 0)); }
z_result_t _z_condvar_init(_z_condvar_t *cv) {
pthread_condattr_t attr;
pthread_condattr_init(&attr);
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
_Z_CHECK_SYS_ERR(pthread_cond_init(cv, &attr));
}

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

Expand All @@ -78,6 +83,26 @@ z_result_t _z_condvar_signal(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_s
z_result_t _z_condvar_signal_all(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_broadcast(cv)); }

z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_cond_wait(cv, m)); }

z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) {
struct timespec ts;
ts.tv_sec = (time_t)(milliseconds / 1000);
ts.tv_nsec = (long)((milliseconds - (ts.tv_sec * 1000)) * 1000000);

int error = pthread_cond_timedwait(cv, m, &ts);

if (error == ETIMEDOUT) {
if (timeout != NULL) {
*timeout = true;
}
return 0;
}

if (timeout != NULL) {
*timeout = false;
}
_Z_CHECK_SYS_ERR(error);
}
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
Expand Down Expand Up @@ -111,7 +136,13 @@ unsigned long z_clock_elapsed_us(z_clock_t *instant) { return z_clock_elapsed_ms

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

unsigned long z_clock_elapsed_s(z_clock_t *instant) { return z_time_elapsed_ms(instant) * 1000; }
unsigned long z_clock_elapsed_s(z_clock_t *instant) { return z_time_elapsed_ms(instant) / 1000; }

void z_clock_advance_us(z_clock_t *clock, unsigned long duration) { *clock += (double)(duration / 1000); }

void z_clock_advance_ms(z_clock_t *clock, unsigned long duration) { *clock += (double)duration; }

void z_clock_advance_s(z_clock_t *clock, unsigned long duration) { *clock += (double)(duration * 1000); }

/*------------------ Time ------------------*/
z_time_t z_time_now(void) { return emscripten_get_now(); }
Expand Down

0 comments on commit 50581ea

Please sign in to comment.