From e4fd38ebf4c88c828b15f71b81ccbdb452aca8c5 Mon Sep 17 00:00:00 2001 From: Mike Miller Date: Sun, 26 Nov 2023 12:39:02 -0800 Subject: [PATCH] o More stability fixes --- app/AppDelegate.m | 6 +----- kernel/log.c | 18 ++++++++++-------- kernel/task.c | 7 ++++++- util/sync.c | 7 +++++-- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/app/AppDelegate.m b/app/AppDelegate.m index e76eac8214..b50d68b054 100644 --- a/app/AppDelegate.m +++ b/app/AppDelegate.m @@ -59,11 +59,7 @@ static void ios_handle_exit(struct task *task, int code) { } // pid should be saved now since task would be freed pid_t pid = task->pid; - // if(pids_lock.pid == pid) - // unlock(&pids_lock); -// while((critical_region_count(task)) || (locks_held_count(task))) { // Wait for now, task is in one or more critical sections, and/or has locks -// nanosleep(&lock_pause, NULL); -// } + unlock(&pids_lock); unlock(&task->general_lock); dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/kernel/log.c b/kernel/log.c index f98c6b40a0..0b59397431 100644 --- a/kernel/log.c +++ b/kernel/log.c @@ -232,39 +232,40 @@ void die(const char *msg, ...) { // fun little utility function int current_pid(void) { - //modify_critical_region_counter(current, 1, __FILE__, __LINE__); + modify_critical_region_counter(current, 1, __FILE__, __LINE__); if(current != NULL) { if (current->exiting != true) { - //modify_critical_region_counter(current, -1, __FILE__, __LINE__); + modify_critical_region_counter(current, -1, __FILE__, __LINE__); return current->pid; } else { - //modify_critical_region_counter(current, -1, __FILE__, __LINE__); + modify_critical_region_counter(current, -1, __FILE__, __LINE__); return -1; } } - //modify_critical_region_counter(current, -1, __FILE__, __LINE__); + modify_critical_region_counter(current, -1, __FILE__, __LINE__); return -1; } int current_uid(void) { - //modify_critical_region_counter(current, 1, __FILE__, __LINE__); + modify_critical_region_counter(current, 1, __FILE__, __LINE__); if(current != NULL) { if (current->exiting != true) { - //modify_critical_region_counter(current, -1, __FILE__, __LINE__); + modify_critical_region_counter(current, -1, __FILE__, __LINE__); return current->uid; } else { - //modify_critical_region_counter(current, -1, __FILE__, __LINE__); + modify_critical_region_counter(current, -1, __FILE__, __LINE__); return -1; } } - //modify_critical_region_counter(current, -1, __FILE__, __LINE__); + modify_critical_region_counter(current, -1, __FILE__, __LINE__); return -1; } char * current_comm(void) { static char comm[16]; + modify_critical_region_counter(current, 1, __FILE__, __LINE__); if(current != NULL) { if(strcmp(current->comm, "")) { strncpy(comm, current->comm, 16); @@ -277,6 +278,7 @@ char * current_comm(void) { return ""; } } + modify_critical_region_counter(current, -1, __FILE__, __LINE__); return ""; } diff --git a/kernel/task.c b/kernel/task.c index 09a2d76975..071cb2be2b 100644 --- a/kernel/task.c +++ b/kernel/task.c @@ -197,8 +197,13 @@ void task_destroy(struct task *task, int caller) { unlock(&pids_lock); } +retry: // Free the task's resources. - free(task); + if (!critical_region_count(task)) { + free(task); + } else { + goto retry; + } } void run_at_boot(void) { // Stuff we run only once, at boot time. diff --git a/util/sync.c b/util/sync.c index 0510dfced5..18ea97203e 100644 --- a/util/sync.c +++ b/util/sync.c @@ -63,6 +63,9 @@ void modify_critical_region_counter(struct task *task, int value, __attribute__( printk("ERROR: Attempt to decrement critical_region count to be negative, ignoring(%s:%d) (%d - %d) (%s:%d)\n", task->comm, task->pid, task->critical_region.count, value, file, line); if(ilocked == true) unlock(&task->general_lock); + + pthread_mutex_unlock(&task->critical_region.lock); + return; } @@ -209,11 +212,11 @@ void sigusr1_handler(void) { // Because sometimes we can't #include "kernel/task.h" -mke unsigned critical_region_count(struct task *task) { unsigned tmp = 0; -// pthread_mutex_lock(task->critical_region.lock); // This would make more + pthread_mutex_lock(&task->critical_region.lock); // This would make more tmp = task->critical_region.count; if(tmp > 1000) // Not likely tmp = 0; - // pthread_mutex_unlock(task->critical_region.lock); + pthread_mutex_unlock(&task->critical_region.lock); return tmp; }