diff --git a/Sources/LinuxHalSwiftIO/swift_fs.c b/Sources/LinuxHalSwiftIO/swift_fs.c index be3afef..cbde714 100644 --- a/Sources/LinuxHalSwiftIO/swift_fs.c +++ b/Sources/LinuxHalSwiftIO/swift_fs.c @@ -92,8 +92,8 @@ int swifthal_fs_rename(const char *from, char *to) { int swifthal_fs_write(void *fp, const void *buf, ssize_t size) { size_t nbytes; - nbytes = fwrite(buf, size, 1, fp); - if (nbytes != 1) { + nbytes = fwrite(buf, 1, size, fp); + if (nbytes == 0) { if (ferror(fp)) return -errno; return -EIO; @@ -105,8 +105,8 @@ int swifthal_fs_write(void *fp, const void *buf, ssize_t size) { int swifthal_fs_read(void *fp, void *buf, ssize_t size) { size_t nbytes; - nbytes = fread(buf, size, 1, fp); - if (nbytes != 1) { + nbytes = fread(buf, 1, size, fp); + if (nbytes == 0) { if (ferror(fp)) return -errno; if (feof(fp)) diff --git a/Sources/LinuxHalSwiftIO/swift_os.c b/Sources/LinuxHalSwiftIO/swift_os.c index e55120d..be50481 100644 --- a/Sources/LinuxHalSwiftIO/swift_os.c +++ b/Sources/LinuxHalSwiftIO/swift_os.c @@ -69,6 +69,8 @@ void *swifthal_os_task_create(char *name, task->p3 = p3; err = pthread_attr_init(&attr); + if (err == 0) + err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (err == 0) err = pthread_attr_getschedparam(&attr, ¶m); if (err == 0) { @@ -151,8 +153,13 @@ static struct timespec *swifthal_timeout_to_timespec(int timeout, if (timeout == -1) return NULL; - ts->tv_sec = timeout / 1000; - ts->tv_nsec = (timeout % 1000) * NSEC_PER_MSEC; + clock_gettime(CLOCK_REALTIME, ts); + ts->tv_sec += timeout / 1000; + ts->tv_nsec += (timeout % 1000) * NSEC_PER_MSEC; + if (ts->tv_nsec >= 1000000000L) { + ts->tv_sec += ts->tv_nsec / 1000000000L; + ts->tv_nsec %= 1000000000L; + } return ts; } @@ -367,7 +374,12 @@ int swifthal_os_sem_reset(const void *arg) { if (sem_getvalue(&sem->sem, &value) != 0) return -errno; while (value > sem->init_cnt) { - if (sem_trywait(&sem->sem) != 0 || sem_getvalue(&sem->sem, &value) != 0) + if (sem_trywait(&sem->sem) != 0) { + if (errno == EAGAIN) + break; + return -errno; + } + if (sem_getvalue(&sem->sem, &value) != 0) return -errno; } diff --git a/Sources/LinuxHalSwiftIO/swift_platform.c b/Sources/LinuxHalSwiftIO/swift_platform.c index bf63b5a..59e786c 100644 --- a/Sources/LinuxHalSwiftIO/swift_platform.c +++ b/Sources/LinuxHalSwiftIO/swift_platform.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #ifdef __linux__ #include @@ -46,7 +47,7 @@ uint32_t swifthal_hwcycle_get(void) { #if __x86_64__ unsigned a, d; asm volatile("rdtsc" : "=a"(a), "=d"(d)); - return ((uint64_t)a) | (((uint64_t)d) << 32); + return a; #elif defined(__aarch64__) uint64_t val; asm volatile("mrs %0, cntvct_el0" : "=r"(val)); @@ -61,7 +62,10 @@ uint32_t swifthal_hwcycle_get(void) { } uint32_t swifthal_hwcycle_to_ns(unsigned int cycles) { - return (uint32_t)sysconf(_SC_CLK_TCK); + struct timespec tp; + if (clock_getres(CLOCK_MONOTONIC, &tp) < 0) + return 0; + return (uint32_t)((uint64_t)cycles * (tp.tv_sec * 1000000000ULL + tp.tv_nsec)); } void swiftHal_randomGet(uint8_t *buf, ssize_t length) { diff --git a/Sources/LinuxHalSwiftIO/swift_timer.c b/Sources/LinuxHalSwiftIO/swift_timer.c index 011ffe1..8077dcc 100644 --- a/Sources/LinuxHalSwiftIO/swift_timer.c +++ b/Sources/LinuxHalSwiftIO/swift_timer.c @@ -52,6 +52,7 @@ int swifthal_timer_close(void *arg) { if (timer) { dispatch_source_cancel(timer->source); + dispatch_resume(timer->source); dispatch_release(timer->source); free(timer); return 0; @@ -61,15 +62,17 @@ int swifthal_timer_close(void *arg) { } int swifthal_timer_start(void *arg, swift_timer_type_t type, ssize_t period) { - const struct swifthal_timer *timer = arg; + struct swifthal_timer *timer = arg; if (timer) { uint64_t interval = (uint64_t)period * NSEC_PER_MSEC; + timer->type = type; dispatch_source_set_timer( timer->source, dispatch_time(DISPATCH_TIME_NOW, interval), (type == SWIFT_TIMER_TYPE_ONESHOT) ? DISPATCH_TIME_FOREVER : interval, 0); + dispatch_resume(timer->source); return 0; }