From bf6b5d3c61636e5e25b72b6fe0033205e71adadf Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 24 Aug 2020 16:41:11 -0400 Subject: [PATCH] fix: cleanup lock API --- runtime/include/lock.h | 23 +++++++++++------------ runtime/src/local_runqueue_minheap.c | 2 ++ runtime/src/priority_queue.c | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/runtime/include/lock.h b/runtime/include/lock.h index 721baae..2b88250 100644 --- a/runtime/include/lock.h +++ b/runtime/include/lock.h @@ -24,24 +24,23 @@ typedef ck_spinlock_mcs_t lock_t; /** * Locks a lock, keeping track of overhead * @param lock - the address of the lock - * @param hygienic_prefix - a unique prefix to hygienically namespace an associated lock/unlock pair + * @param unique_variable_name - a unique prefix to hygienically namespace an associated lock/unlock pair */ -#define LOCK_LOCK_WITH_BOOKKEEPING(lock, hygienic_prefix) \ - bool hygienic_prefix##_is_interruptable = software_interrupt_is_enabled(); \ - if (hygienic_prefix##_is_interruptable) software_interrupt_disable(); \ - struct ck_spinlock_mcs hygienic_prefix##_node; \ - uint64_t hygienic_prefix##_pre = __getcycles(); \ - ck_spinlock_mcs_lock((lock), &(hygienic_prefix##_node)); \ - worker_thread_lock_duration += (__getcycles() - hygienic_prefix##_pre); +#define LOCK_LOCK_WITH_BOOKKEEPING(lock, unique_variable_name) \ + assert(!runtime_is_worker() || !software_interrupt_is_enabled()); \ + struct ck_spinlock_mcs unique_variable_name##_node; \ + uint64_t unique_variable_name##_pre = __getcycles(); \ + ck_spinlock_mcs_lock((lock), &(unique_variable_name##_node)); \ + worker_thread_lock_duration += (__getcycles() - unique_variable_name##_pre); /** * Unlocks a lock * @param lock - the address of the lock - * @param hygienic_prefix - a unique prefix to hygienically namespace an associated lock/unlock pair + * @param unique_variable_name - a unique prefix to hygienically namespace an associated lock/unlock pair */ -#define LOCK_UNLOCK_WITH_BOOKKEEPING(lock, hygienic_prefix) \ - ck_spinlock_mcs_unlock(lock, &(hygienic_prefix##_node)); \ - if (hygienic_prefix##_is_interruptable) software_interrupt_enable(); +#define LOCK_UNLOCK_WITH_BOOKKEEPING(lock, unique_variable_name) \ + assert(!runtime_is_worker() || !software_interrupt_is_enabled()); \ + ck_spinlock_mcs_unlock(lock, &(unique_variable_name##_node)); /** * Locks a lock, keeping track of overhead diff --git a/runtime/src/local_runqueue_minheap.c b/runtime/src/local_runqueue_minheap.c index 5132b2a..5c9d75f 100644 --- a/runtime/src/local_runqueue_minheap.c +++ b/runtime/src/local_runqueue_minheap.c @@ -217,7 +217,9 @@ void local_runqueue_minheap_initialize() { /* Initialize local state */ + software_interrupt_disable(); priority_queue_initialize(&local_runqueue_minheap, sandbox_get_priority); + software_interrupt_enable(); /* Register Function Pointers for Abstract Scheduling API */ struct local_runqueue_config config = { .add_fn = local_runqueue_minheap_add, diff --git a/runtime/src/priority_queue.c b/runtime/src/priority_queue.c index 8d03255..49a627b 100644 --- a/runtime/src/priority_queue.c +++ b/runtime/src/priority_queue.c @@ -22,6 +22,8 @@ static inline int priority_queue_append(struct priority_queue *self, void *new_item) { assert(self != NULL); + assert(new_item != NULL); + assert(LOCK_IS_LOCKED(&self->lock)); int rc; @@ -105,6 +107,8 @@ priority_queue_percolate_down(struct priority_queue *self, int parent_index) assert(self != NULL); assert(self->get_priority_fn != NULL); assert(LOCK_IS_LOCKED(&self->lock)); + assert(runtime_is_worker()); + assert(!software_interrupt_is_enabled()); int left_child_index = 2 * parent_index; while (left_child_index >= 2 && left_child_index < self->first_free) { @@ -133,6 +137,8 @@ priority_queue_is_empty_locked(struct priority_queue *self) { assert(self != NULL); assert(LOCK_IS_LOCKED(&self->lock)); + assert(!runtime_is_worker() || !software_interrupt_is_enabled()); + return self->first_free == 1; } @@ -150,6 +156,7 @@ priority_queue_initialize(struct priority_queue *self, priority_queue_get_priori { assert(self != NULL); assert(get_priority_fn != NULL); + assert(!runtime_is_worker() || !software_interrupt_is_enabled()); memset(self->items, 0, sizeof(void *) * MAX); @@ -169,6 +176,8 @@ int priority_queue_length(struct priority_queue *self) { assert(self != NULL); + assert(runtime_is_worker()); + assert(!software_interrupt_is_enabled()); LOCK_LOCK(&self->lock); int length = self->first_free - 1; @@ -185,6 +194,8 @@ int priority_queue_enqueue(struct priority_queue *self, void *value) { assert(self != NULL); + assert(value != NULL); + assert(!runtime_is_worker() || !software_interrupt_is_enabled()); int rc; @@ -218,6 +229,9 @@ int priority_queue_delete(struct priority_queue *self, void *value) { assert(self != NULL); + assert(value != NULL); + assert(runtime_is_worker()); + assert(!software_interrupt_is_enabled()); LOCK_LOCK(&self->lock); @@ -260,6 +274,8 @@ priority_queue_dequeue_if_earlier(struct priority_queue *self, void **dequeued_e assert(self != NULL); assert(dequeued_element != NULL); assert(self->get_priority_fn != NULL); + assert(runtime_is_worker()); + assert(!software_interrupt_is_enabled()); int return_code; @@ -303,6 +319,8 @@ priority_queue_top(struct priority_queue *self, void **dequeued_element) assert(self != NULL); assert(dequeued_element != NULL); assert(self->get_priority_fn != NULL); + assert(runtime_is_worker()); + assert(!software_interrupt_is_enabled()); int return_code;