|
|
@ -174,6 +174,14 @@ priority_queue_enqueue(struct priority_queue *self, void *value)
|
|
|
|
return rc;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
|
|
|
priority_queue_is_empty(struct priority_queue *self)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(self != NULL);
|
|
|
|
|
|
|
|
assert(self->first_free != 0);
|
|
|
|
|
|
|
|
return self->first_free == 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param self - the priority queue we want to add to
|
|
|
|
* @param self - the priority queue we want to add to
|
|
|
|
* @returns The head of the priority queue or NULL when empty
|
|
|
|
* @returns The head of the priority queue or NULL when empty
|
|
|
@ -183,23 +191,21 @@ priority_queue_dequeue(struct priority_queue *self)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
assert(self != NULL);
|
|
|
|
assert(self != NULL);
|
|
|
|
assert(self->get_priority != NULL);
|
|
|
|
assert(self->get_priority != NULL);
|
|
|
|
// If first_free is 1, we're empty
|
|
|
|
if (priority_queue_is_empty(self)) return NULL;
|
|
|
|
if (self->first_free == 1) return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ck_spinlock_fas_trylock(&self->lock) == false) return NULL;
|
|
|
|
if (ck_spinlock_fas_trylock(&self->lock) == false) return NULL;
|
|
|
|
// Start of Critical Section
|
|
|
|
// Start of Critical Section
|
|
|
|
void *min = self->items[1];
|
|
|
|
void *min = NULL;
|
|
|
|
self->items[1] = self->items[self->first_free - 1];
|
|
|
|
if (!priority_queue_is_empty(self)) {
|
|
|
|
self->items[self->first_free - 1] = NULL;
|
|
|
|
min = self->items[1];
|
|
|
|
self->first_free--;
|
|
|
|
self->items[1] = self->items[self->first_free - 1];
|
|
|
|
assert(self->first_free == 1 || self->items[self->first_free - 1] != NULL);
|
|
|
|
self->items[self->first_free - 1] = NULL;
|
|
|
|
// Because of 1-based indices, first_free is 2 when there is only one element
|
|
|
|
self->first_free--;
|
|
|
|
if (self->first_free > 2) priority_queue_percolate_down(self);
|
|
|
|
// Because of 1-based indices, first_free is 2 when there is only one element
|
|
|
|
|
|
|
|
if (self->first_free > 2) priority_queue_percolate_down(self);
|
|
|
|
if (self->first_free > 1) {
|
|
|
|
|
|
|
|
self->highest_priority = self->get_priority(self->items[1]);
|
|
|
|
// Update the highest priority
|
|
|
|
} else {
|
|
|
|
self->highest_priority = !priority_queue_is_empty(self) ? self->get_priority(self->items[1])
|
|
|
|
self->highest_priority = ULONG_MAX;
|
|
|
|
: ULONG_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ck_spinlock_fas_unlock(&self->lock);
|
|
|
|
ck_spinlock_fas_unlock(&self->lock);
|
|
|
|
// End of Critical Section
|
|
|
|
// End of Critical Section
|
|
|
|