diff --git a/runtime/src/priority_queue.c b/runtime/src/priority_queue.c index 6f2949a..7a36141 100644 --- a/runtime/src/priority_queue.c +++ b/runtime/src/priority_queue.c @@ -174,6 +174,14 @@ priority_queue_enqueue(struct priority_queue *self, void *value) 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 * @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->get_priority != NULL); - // If first_free is 1, we're empty - if (self->first_free == 1) return NULL; - + if (priority_queue_is_empty(self)) return NULL; if (ck_spinlock_fas_trylock(&self->lock) == false) return NULL; // Start of Critical Section - void *min = self->items[1]; - self->items[1] = self->items[self->first_free - 1]; - self->items[self->first_free - 1] = NULL; - self->first_free--; - assert(self->first_free == 1 || self->items[self->first_free - 1] != NULL); - // 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]); - } else { - self->highest_priority = ULONG_MAX; + void *min = NULL; + if (!priority_queue_is_empty(self)) { + min = self->items[1]; + self->items[1] = self->items[self->first_free - 1]; + self->items[self->first_free - 1] = NULL; + self->first_free--; + // 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); + + // Update the highest priority + self->highest_priority = !priority_queue_is_empty(self) ? self->get_priority(self->items[1]) + : ULONG_MAX; } ck_spinlock_fas_unlock(&self->lock); // End of Critical Section diff --git a/runtime/src/runtime.c b/runtime/src/runtime.c index ba9d0b2..15c4599 100644 --- a/runtime/src/runtime.c +++ b/runtime/src/runtime.c @@ -20,7 +20,8 @@ #include #include #include -#include +// #include +#include #include #include @@ -45,7 +46,8 @@ runtime_initialize(void) assert(runtime_epoll_file_descriptor >= 0); // Allocate and Initialize the global deque - sandbox_request_scheduler_fifo_initialize(); + // sandbox_request_scheduler_fifo_initialize(); + sandbox_request_scheduler_ps_initialize(); // Mask Signals software_interrupt_mask_signal(SIGUSR1); diff --git a/runtime/src/sandbox_request_scheduler_ps.c b/runtime/src/sandbox_request_scheduler_ps.c index 0f9e6f9..9e90100 100644 --- a/runtime/src/sandbox_request_scheduler_ps.c +++ b/runtime/src/sandbox_request_scheduler_ps.c @@ -12,8 +12,9 @@ static struct priority_queue sandbox_request_scheduler_ps; static sandbox_request_t * sandbox_request_scheduler_ps_add(void *sandbox_request_raw) { - // TODO - return NULL; + int return_code = priority_queue_enqueue(&sandbox_request_scheduler_ps, sandbox_request_raw); + + return return_code == 0 ? sandbox_request_raw : NULL; } /** @@ -23,10 +24,17 @@ sandbox_request_scheduler_ps_add(void *sandbox_request_raw) static sandbox_request_t * sandbox_request_scheduler_ps_remove(void) { - // TODO - return NULL; + return (sandbox_request_t *)priority_queue_dequeue(&sandbox_request_scheduler_ps); } +unsigned long long int +sandbox_request_get_priority(void *element) +{ + sandbox_request_t *sandbox_request = (sandbox_request_t *)element; + return sandbox_request->absolute_deadline; +}; + + /** * **/ @@ -34,6 +42,7 @@ void sandbox_request_scheduler_ps_initialize() { // Initialize local state + priority_queue_initialize(&sandbox_request_scheduler_ps, sandbox_request_get_priority); // TODO