-void *detect_termination(void *_args){
- thread_args_t *args = _args;
- int id = args->id;
- int processes = args->processes;
- bool initiator = args->initiator;
-
- while (true){
- pthread_mutex_lock(&state_mutex);
-
- // CHECK GLOBAL TERMINATION CONDITION (only happens at root)
- if (initiator && state == PASSIVE_STATE && deficit == 0)
- {
- trace("%d: [CONTROL] GLOBAL TERMINATION DETECTED!\n", id);
- pthread_mutex_unlock(&state_mutex);
- break; // Breaks loop, returns NULL, tells main() to shut down
- }
-
- pthread_mutex_unlock(&state_mutex);
-
- // Check network for incoming child signals (non-blocking with MPI_Iprobe)
- int flag = 0;
- MPI_Status status;
- MPI_Iprobe(MPI_ANY_SOURCE, CONTROL_SIGNAL, MPI_COMM_WORLD, &flag, &status);
-
- if (flag){
- control_message_t sig;
- MPI_Recv(&sig, sizeof(control_message_t), MPI_BYTE, status.MPI_SOURCE,
- CONTROL_SIGNAL, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
-
- pthread_mutex_lock(&state_mutex);
- deficit--;
- trace("%d: [CONTROL] Got signal from %d (New Deficit: %d)\n",
- id, status.MPI_SOURCE, deficit);
-
- try_resolve_tree(id);
- pthread_mutex_unlock(&state_mutex);
- }
- else{
- // Sleep for 1 millisecond to prevent this while(true) loop
- // from pinning the CPU core at 100% usage
- usleep(1000);
- }
- }
-
- return NULL;
+void *detect_termination(void *_args)
+{
+ thread_args_t *args = _args;
+ int id = args->id;
+ is_initiator = args->initiator;
+
+ // (Removed unused 'processes' variable to fix compiler warning)
+
+ while (true)
+ {
+ pthread_mutex_lock(&state_mutex);
+
+ // CHECK GLOBAL TERMINATION CONDITION
+ if (is_initiator && state == PASSIVE_STATE && deficit == 0)
+ {
+ trace("%d: [CONTROL] GLOBAL TERMINATION DETECTED!\n", id);
+ pthread_mutex_unlock(&state_mutex);
+ break; // Breaks loop, tells main() to shut down
+ }
+
+ pthread_mutex_unlock(&state_mutex);
+
+ // Check network for incoming child signals (non-blocking)
+ int flag = 0;
+ MPI_Status status;
+ MPI_Iprobe(MPI_ANY_SOURCE, CONTROL_SIGNAL, MPI_COMM_WORLD, &flag, &status);
+
+ if (flag)
+ {
+ control_message_t sig;
+ MPI_Recv(&sig, sizeof(control_message_t), MPI_BYTE, status.MPI_SOURCE,
+ CONTROL_SIGNAL, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
+
+ pthread_mutex_lock(&state_mutex);
+ deficit--; // Rule D: A child finished its work and reported back
+ trace("%d: [CONTROL] Got signal from %d (New Deficit: %d)\n",
+ id, status.MPI_SOURCE, deficit);
+
+ try_resolve_tree(id);
+ pthread_mutex_unlock(&state_mutex);
+ }
+ else
+ {
+ // Sleep briefly to prevent pinning the CPU core at 100% usage
+ usleep(1000);
+ }
+ }
+
+ return NULL;