diff --git a/source/coroed/api/clock.h b/source/coroed/api/clock.h deleted file mode 100644 index c7bb0ee..0000000 --- a/source/coroed/api/clock.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -uint64_t clock_now(); - -void clock_delay(uint64_t milliseconds); diff --git a/source/coroed/api/clock.c b/source/coroed/api/sleep.c similarity index 75% rename from source/coroed/api/clock.c rename to source/coroed/api/sleep.c index fa8227f..9cfaf19 100644 --- a/source/coroed/api/clock.c +++ b/source/coroed/api/sleep.c @@ -1,16 +1,18 @@ -#include "clock.h" +#include "sleep.h" #include #include #include #include +#include "coroed/api/task.h" + static unsigned long long timespec2ms(const struct timespec* spec) { const time_t ms_in_s = 1000; return (unsigned long long)spec->tv_sec * ms_in_s + spec->tv_sec / (ms_in_s * ms_in_s); } -uint64_t clock_now() { +static uint64_t clock_now() { struct timespec spec; int code = clock_gettime(CLOCK_MONOTONIC, &spec); @@ -19,9 +21,9 @@ uint64_t clock_now() { return timespec2ms(&spec); } -void clock_delay(uint64_t milliseconds) { +void task_sleep(struct task* caller, uint64_t milliseconds) { const unsigned long long start = clock_now(); while (clock_now() - start < milliseconds) { - // Do nothing + task_yield(caller); } } diff --git a/source/coroed/api/sleep.h b/source/coroed/api/sleep.h new file mode 100644 index 0000000..7e38d1e --- /dev/null +++ b/source/coroed/api/sleep.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +#include "task.h" + +#define SLEEP(ms) task_sleep(__self, (ms)) + +void task_sleep(struct task* caller, uint64_t milliseconds); diff --git a/source/main.c b/source/main.c index da99d26..e5aaeb4 100644 --- a/source/main.c +++ b/source/main.c @@ -8,9 +8,9 @@ #include #include -#include "coroed/api/clock.h" #include "coroed/api/event.h" #include "coroed/api/log.h" +#include "coroed/api/sleep.h" #include "coroed/api/task.h" enum { @@ -25,7 +25,7 @@ static atomic_int_least64_t state = 0; static struct event event; TASK_DEFINE(eventer, void, ignored) { - sleep(1); + SLEEP(1000); state = 1; event_fire(&event); } @@ -69,7 +69,7 @@ TASK_DEFINE(print_loop, const char, message) { YIELD; free(msg); - clock_delay(delay); + SLEEP(delay); } }