From c062eaeea09592cbdf7e5d732e992d0cdd8eedc5 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 1 Jun 2023 17:33:56 +0200 Subject: More scheduling --- src/lib/pqueue.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/lib/pqueue.c') diff --git a/src/lib/pqueue.c b/src/lib/pqueue.c index c080bb2..cf0611f 100644 --- a/src/lib/pqueue.c +++ b/src/lib/pqueue.c @@ -35,7 +35,8 @@ #define parent(i) ((i) >> 1) struct pqueue *pqueue_init(size_t n, pqueue_cmp_pri_f cmppri, - pqueue_get_pri_f getpri, pqueue_set_pos_f setpos) + pqueue_get_pri_f getpri, pqueue_set_pri_f setpri, + pqueue_get_pos_f getpos, pqueue_set_pos_f setpos) { struct pqueue *q; @@ -51,7 +52,9 @@ struct pqueue *pqueue_init(size_t n, pqueue_cmp_pri_f cmppri, q->size = 1; q->avail = q->step = (n + 1); /* see comment above about n+1 */ q->cmppri = cmppri; + q->setpri = setpri; q->getpri = getpri; + q->getpos = getpos; q->setpos = setpos; return q; @@ -144,6 +147,19 @@ int pqueue_insert(struct pqueue *q, void *d) return 0; } +void pqueue_change_priority(struct pqueue *q, pqueue_pri_t new_pri, void *d) +{ + size_t posn; + pqueue_pri_t old_pri = q->getpri(d); + + q->setpri(d, new_pri); + posn = q->getpos(d); + if (q->cmppri(old_pri, new_pri)) + bubble_up(q, posn); + else + percolate_down(q, posn); +} + void *pqueue_pop(struct pqueue *q) { void *head; -- cgit v1.2.3