Skip to content

Commit c235004

Browse files
QuzarDCQuzarDC
authored andcommitted
genwait: Keep queues in order by thread priority.
Currently, new waiting threads are always added to the end of their genwait tailq and the first thread in the tailq is the first woken. This means that unless a thread is being woken and readded (like can happen with a `genwait_wake_all`) they will always be awoken based on how long they have been waiting. The downside to this method is that it ignores thread priorities, which should be the controlling factor in which thread gets woken first. By inserting into the queue by priority order, wakes will now happen based on priority rather than time waiting. The mild overhead of seeking through the waiting queues should be outweighed greatly by allowing priority management via thread priorities.
1 parent 05037cc commit c235004

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

kernel/thread/genwait.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static kthread_t *tq_next(void) {
6969
}
7070

7171
int genwait_wait(void *obj, const char *mesg, int timeout, void (*callback)(void *)) {
72-
kthread_t *me;
72+
kthread_t *me, *t;
7373

7474
/* Twiddle interrupt state */
7575
if(irq_inside_int()) {
@@ -95,8 +95,17 @@ int genwait_wait(void *obj, const char *mesg, int timeout, void (*callback)(void
9595

9696
me->wait_callback = callback;
9797

98-
/* Insert us on the appropriate wait queue */
99-
TAILQ_INSERT_TAIL(&slpque[LOOKUP(obj)], me, thdq);
98+
/* Go through and find where to insert */
99+
TAILQ_FOREACH(t, &slpque[LOOKUP(obj)], thdq) {
100+
if(me->prio < t->prio) {
101+
TAILQ_INSERT_BEFORE(t, me, thdq);
102+
break;
103+
}
104+
}
105+
106+
/* We got to the end of the list, so insert at end */
107+
if(!t)
108+
TAILQ_INSERT_TAIL(&slpque[LOOKUP(obj)], me, thdq);
100109

101110
/* Block us until we're signaled */
102111
return thd_block_now(&me->context);

0 commit comments

Comments
 (0)