Clean up for submission

This commit is contained in:
Isaac Shoebottom 2023-11-04 23:48:13 -03:00
parent c3faf35400
commit a534f0d577

View File

@ -7,16 +7,10 @@
/*
* Marker, please read:
*
* There is a bug that I cannot track down/may be unsolvable with my current implementation.
* It's where *sometimes* the print function, after a thread finishes, it will print an extra time on one job (I think)
* There is a bug, I think it is due to the print thread reading the finish count and exiting early sometimes.
* The way to fix this would be to implement a mutex for the finish count, that works across threads.
* The incrementFinishCount() and getFinishCount() functions are too simple and don't take into account the other threads.
*
* This could probably be solved by running the "done threads" until all threads are complete, but I didn't have time to bugfix that
* implementation. I tried it, but it didn't work as is, so I reverted it.
*
* With that implementation it may have been beneficial to have an array of semaphores, one for each "CPU", that the print thread
* can for loop wait on each to complete. I think that would have worked, but I straight up didn't have time to implement it or try at all
*
* The comments may also not be the most helpful, didn't have time to go and clean them up
*/
@ -118,7 +112,6 @@ void *print(void *args) {
while (getFinishCount() < CPUS) {
// Wait for all the simulation threads to finish
for (int i = 0; i < CPUS; ++i) {
//printf("print wait cpu_id: %d\n", i);
sem_wait(&print_sem);
}
@ -152,7 +145,6 @@ void *print(void *args) {
sem_post(&sim_sems[i]);
}
//printf("print thread done\n");
return NULL;
}
@ -186,7 +178,6 @@ void *simulation(void *args) {
Queue *sim_queue = createQueue();
while (getFinishCount() < CPUS) {
// Only simulate if the time has changed
//printf("sim wait cpu_id: %d\n", cpu_id);
sem_wait(&sim_sems[cpu_id]);
if (!doneSimulating) {
time = getTime();
@ -233,7 +224,6 @@ void *simulation(void *args) {
}
// Allow the print thread to print because the simulation for this tick is done
sem_post(&print_sem);
//printf("sim post cpu_id: %d\n", cpu_id);
}
// Let the print thread one last time
sem_post(&print_sem);
@ -288,16 +278,12 @@ int main() {
}
// Wait for print thread to finish
int r = pthread_join(print_thread, NULL);
pthread_join(print_thread, NULL);
free(print_args);
//printf("joined print thread\n");
//printf("thread result: %d\n", r);
// Threads simulate, then print
for (int i = 0; i < CPUS; i++) {
r = pthread_join(threads[i], NULL);
//printf("joined sim thread: %d\n", i);
//printf("thread result: %d\n", r);
pthread_join(threads[i], NULL);
free(args[i]);
}
@ -306,24 +292,17 @@ int main() {
// Stop semaphores
for (int i = 0; i < CPUS; ++i) {
sem_destroy(&sim_sems[i]);
//printf("destroyed sim sem: %d\n", i);
}
free(sim_sems);
sem_destroy(&print_sem);
// Stop mutexes
pthread_mutex_unlock(&finish_mutex);
pthread_mutex_unlock(&summary_mutex);
pthread_mutex_unlock(&time_mutex);
pthread_mutex_destroy(&finish_mutex);
pthread_mutex_destroy(&summary_mutex);
pthread_mutex_destroy(&time_mutex);
// Free memory
stop(in_queue); // Free memory for input queue
stop(summary_queue); // Free memory for summary queue
free(print_buffer); // Free memory for print buffer