/*
  Parallel Programming - 2026/2027

  Skeleton of an implementation of a termination detection algorithm
*/

#include <mpi.h>
#include <pthread.h>

#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>

#include "global.h"
#include "control.h"
#include "util.h"

// the tag(s) of the control messages
//#define ...

// type for control messages
//typedef struct {
//  ...
//} control_message_t;

// local variables
//static ...

/*
  Main loop of the termination detection algorithm.

  When termination is detected, it must end (returning NULL or another
  appropriate value), which signals the main thread that the basic
  algorithm has terminated.

  The arguments of the termination detection algorithm are the ID of
  the process, the number of PROCESSES running the basic algorithm,
  and whether the process is an initiator.
*/
void *detect_termination(void *_args)
{
  thread_args_t *args = _args;
  int id = args->id;
  int processes = args->processes;
  bool initiator = args->initiator;

  // THE FOLLOWING TWO LINES OF CODE MUST BE REPLACED BY THE CODE OF
  // THE TERMINATION DETECTION ALGORITHM

  // let the basic algorithm run for 10 seconds
  sleep(10);

  return NULL;
}

/*
  Called at startup of the basic algorithm in process ID.
*/
void control_basic_start_hook(int id)
{
}

/*
  Called when the basic algorithm process ID becomes passive.
*/
void control_become_passive_hook(int id)
{
}

/*
  Called when the basic algorithm process ID becomes active.
*/
void control_become_active_hook(int id)
{
}

/*
  Called when the basic algorithm process ID sends a basic message to
  process PEER.
*/
void control_basic_send_hook(int id, int peer)
{
}

/*
  Called when the basic algorithm process ID receives a basic message
  from process PEER.
*/
void control_basic_receive_hook(int id, int peer)
{
}
