+// Tag for control messages. Distinct from basic algorithm's tags.
+#define CONTROL_SIGNAL 99
+
+// Type for control messages (empty payload, just a signal)
+typedef struct {
+ int dummy;
+} control_message_t;
+
+// Node operational states
+typedef enum {
+ ACTIVE_STATE,
+ PASSIVE_STATE
+} node_state_t;
+
+// --- SHARED LOCAL STATE ---
+// Must be protected by mutex because basic_thread and control_thread access it concurrently
+static pthread_mutex_t state_mutex = PTHREAD_MUTEX_INITIALIZER;
+static node_state_t state = PASSIVE_STATE;
+static int parent = -1; // -1 indicates null/no parent
+static int deficit = 0; // C_i counter (unacknowledged messages)
+static bool initiator = false;
+
+// --- HELPER FUNCTION ---
+// MUST BE DECLARED ABOVE THE OTHER FUNCTIONS!
+// Evaluates if the node can detach from the tree. Caller must hold state_mutex.
+static void try_resolve_tree(int my_id)
+{
+ if (state == PASSIVE_STATE && deficit == 0)
+ {
+ if (initiator) {
+ // Root is passive and deficit is 0 -> Global Termination!
+ return;
+ }
+
+ if (parent != -1)
+ {
+ control_message_t sig = {0};
+ // Send acknowledgment signal up the tree to our parent
+ MPI_Send(&sig, sizeof(control_message_t), MPI_BYTE, parent,
+ CONTROL_SIGNAL, MPI_COMM_WORLD);
+
+ trace("%d: [CONTROL] Sent tree-signal to parent %d\n", my_id, parent);
+ parent = -1; // Detach from tree
+ }
+ }
+}