AmendHub

Download:

jcs

/

subtext

/

amendments

/

10

uthread: Add uthread_sleep, sleeping in milliseconds (approximately)


jcs made amendment 10 over 2 years ago
--- uthread.c Tue Nov 30 08:25:37 2021 +++ uthread.c Sun Dec 5 22:06:12 2021 @@ -66,7 +66,9 @@ uthread_yield(void) { volatile long magic = CANARY; - uthread_current->state = UTHREAD_STATE_YIELDING; + if (uthread_current->state != UTHREAD_STATE_SLEEPING) + uthread_current->state = UTHREAD_STATE_YIELDING; + if (setjmp(uthread_current->env) == 0) longjmp(uthread_coord_env, UTHREAD_SETJMP_YIELDED); /* will not return */ @@ -79,6 +81,17 @@ uthread_yield(void) } void +uthread_sleep(unsigned long millis) +{ + /* Ticks is in 1/60 of a second, but it's easier to think in millis */ + unsigned long ts = millis / ((double)1000 / (double)60); + + uthread_current->state = UTHREAD_STATE_SLEEPING; + uthread_current->sleeping_until = Ticks + ts; + uthread_yield(); +} + +void uthread_begin(struct uthread *uthread) { register long stack = STACK_SIZE * (uthread->id + 2); @@ -103,9 +116,11 @@ uthread_coordinate(void) short i; for (i = 0; i < NUM_UTHREADS; i++) { - if (uthreads[i].state == UTHREAD_STATE_DEAD || - uthreads[i].state == UTHREAD_STATE_SLEEPING) + if (uthreads[i].state == UTHREAD_STATE_DEAD) continue; + if (uthreads[i].state == UTHREAD_STATE_SLEEPING && + Ticks < uthreads[i].sleeping_until) + continue; switch (setjmp(uthread_coord_env)) { case UTHREAD_SETJMP_DEFAULT: @@ -117,6 +132,7 @@ uthread_coordinate(void) uthread_begin(uthread_current); break; case UTHREAD_STATE_YIELDING: + case UTHREAD_STATE_SLEEPING: uthread_current->state = UTHREAD_STATE_RUNNING; longjmp(uthread_current->env, UTHREAD_SETJMP_YIELDED); break; --- uthread.h Thu Nov 25 14:02:21 2021 +++ uthread.h Sun Dec 5 21:48:01 2021 @@ -40,11 +40,13 @@ struct uthread { char *stack; void (*func)(struct uthread *, void *); void *arg; + unsigned long sleeping_until; }; void uthread_init(void); struct uthread *uthread_add(void *func, void *arg); void uthread_yield(void); void uthread_coordinate(void); +void uthread_sleep(unsigned long millis); #endif /* __UTHREAD_H__ */