Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/LinuxHalSwiftIO/swift_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))
Expand Down
18 changes: 15 additions & 3 deletions Sources/LinuxHalSwiftIO/swift_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, &param);
if (err == 0) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 6 additions & 2 deletions Sources/LinuxHalSwiftIO/swift_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <unistd.h>
#include <errno.h>
#include <inttypes.h>
#include <time.h>
#include <sys/random.h>
#ifdef __linux__
#include <sys/sysinfo.h>
Expand Down Expand Up @@ -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));
Expand All @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion Sources/LinuxHalSwiftIO/swift_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
Loading