From 8653afffce907f6917519845706af52e02371ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Mon, 8 Jul 2019 13:37:40 +0200 Subject: [PATCH] Fixes for macOS --- lib/include/chiaki/launchspec.h | 2 +- lib/include/chiaki/log.h | 1 + lib/src/launchspec.c | 2 +- lib/src/streamconnection.c | 4 ++-- lib/src/thread.c | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/include/chiaki/launchspec.h b/lib/include/chiaki/launchspec.h index a23716c..518e6ff 100644 --- a/lib/include/chiaki/launchspec.h +++ b/lib/include/chiaki/launchspec.h @@ -34,7 +34,7 @@ typedef struct chiaki_launch_spec_t uint8_t *handshake_key; } ChiakiLaunchSpec; -CHIAKI_EXPORT ssize_t chiaki_launchspec_format(char *buf, size_t buf_size, ChiakiLaunchSpec *launch_spec); +CHIAKI_EXPORT int chiaki_launchspec_format(char *buf, size_t buf_size, ChiakiLaunchSpec *launch_spec); #ifdef __cplusplus } diff --git a/lib/include/chiaki/log.h b/lib/include/chiaki/log.h index c224e4d..31dc349 100644 --- a/lib/include/chiaki/log.h +++ b/lib/include/chiaki/log.h @@ -34,6 +34,7 @@ typedef enum { typedef struct chiaki_log_t { + uint8_t placeholder; // TODO } ChiakiLog; void chiaki_log(ChiakiLog *log, ChiakiLogLevel level, const char *fmt, ...); diff --git a/lib/src/launchspec.c b/lib/src/launchspec.c index 014fe58..5de623f 100644 --- a/lib/src/launchspec.c +++ b/lib/src/launchspec.c @@ -76,7 +76,7 @@ static const char launchspec_fmt[] = "\"handshakeKey\":\"%s\"" // 2 "}"; -CHIAKI_EXPORT ssize_t chiaki_launchspec_format(char *buf, size_t buf_size, ChiakiLaunchSpec *launch_spec) +CHIAKI_EXPORT int chiaki_launchspec_format(char *buf, size_t buf_size, ChiakiLaunchSpec *launch_spec) { char handshake_key_b64[CHIAKI_HANDSHAKE_KEY_SIZE * 2]; ChiakiErrorCode err = chiaki_base64_encode(launch_spec->handshake_key, CHIAKI_HANDSHAKE_KEY_SIZE, handshake_key_b64, sizeof(handshake_key_b64)); diff --git a/lib/src/streamconnection.c b/lib/src/streamconnection.c index ec6aa6b..aaf728f 100644 --- a/lib/src/streamconnection.c +++ b/lib/src/streamconnection.c @@ -605,9 +605,9 @@ static ChiakiErrorCode stream_connection_send_big(ChiakiStreamConnection *stream return err; } - char ecdh_pub_key[128]; + uint8_t ecdh_pub_key[128]; ChiakiPBBuf ecdh_pub_key_buf = { sizeof(ecdh_pub_key), ecdh_pub_key }; - char ecdh_sig[32]; + uint8_t ecdh_sig[32]; ChiakiPBBuf ecdh_sig_buf = { sizeof(ecdh_sig), ecdh_sig }; err = chiaki_ecdh_get_local_pub_key(&session->ecdh, ecdh_pub_key, &ecdh_pub_key_buf.size, diff --git a/lib/src/thread.c b/lib/src/thread.c index ef6e78d..8ce5fd8 100644 --- a/lib/src/thread.c +++ b/lib/src/thread.c @@ -16,6 +16,7 @@ */ #include +#include #include #include @@ -100,12 +101,14 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_init(ChiakiCond *cond) int r = pthread_condattr_init(&attr); if(r != 0) return CHIAKI_ERR_UNKNOWN; +#if !__APPLE__ r = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); if(r != 0) { pthread_condattr_destroy(&attr); return CHIAKI_ERR_UNKNOWN; } +#endif r = pthread_cond_init(&cond->cond, &attr); if(r != 0) { @@ -134,6 +137,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_wait(ChiakiCond *cond, ChiakiMutex *mu return CHIAKI_ERR_SUCCESS; } +#if !__APPLE__ static ChiakiErrorCode chiaki_cond_timedwait_abs(ChiakiCond *cond, ChiakiMutex *mutex, struct timespec *timeout) { int r = pthread_cond_timedwait(&cond->cond, &mutex->mutex, timeout); @@ -157,12 +161,26 @@ static void set_timeout(struct timespec *timeout, uint64_t ms_from_now) timeout->tv_nsec %= 1000000000; } } +#endif CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait(ChiakiCond *cond, ChiakiMutex *mutex, uint64_t timeout_ms) { struct timespec timeout; +#if __APPLE__ + timeout.tv_sec = (__darwin_time_t)(timeout_ms / 1000); + timeout.tv_nsec = (long)((timeout_ms % 1000) * 1000000); + int r = pthread_cond_timedwait_relative_np(&cond->cond, &mutex->mutex, &timeout); + if(r != 0) + { + if(r == ETIMEDOUT) + return CHIAKI_ERR_TIMEOUT; + return CHIAKI_ERR_UNKNOWN; + } + return CHIAKI_ERR_SUCCESS; +#else set_timeout(&timeout, timeout_ms); return chiaki_cond_timedwait_abs(cond, mutex, &timeout); +#endif } CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_wait_pred(ChiakiCond *cond, ChiakiMutex *mutex, ChiakiCheckPred check_pred, void *check_pred_user) @@ -178,13 +196,27 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_wait_pred(ChiakiCond *cond, ChiakiMute CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait_pred(ChiakiCond *cond, ChiakiMutex *mutex, uint64_t timeout_ms, ChiakiCheckPred check_pred, void *check_pred_user) { +#if __APPLE__ + uint64_t start_time = chiaki_time_now_monotonic_ms(); + uint64_t elapsed = 0; +#else struct timespec timeout; set_timeout(&timeout, timeout_ms); +#endif while(!check_pred(check_pred_user)) { +#if __APPLE__ + ChiakiErrorCode err = chiaki_cond_timedwait(cond, mutex, timeout_ms - elapsed); +#else ChiakiErrorCode err = chiaki_cond_timedwait_abs(cond, mutex, &timeout); +#endif if(err != CHIAKI_ERR_SUCCESS) return err; +#if __APPLE__ + elapsed = chiaki_time_now_monotonic_ms() - start_time; + if(elapsed >= timeout_ms) + return CHIAKI_ERR_TIMEOUT; +#endif } return CHIAKI_ERR_SUCCESS;