-#ifndef _NBODY_H
-#define _NBODY_H
+#ifndef _NBODY_MTHREAD_H
+#define _NBODY_MTHREAD_H
-/**
- * @file nbody.h
- * @author Sam Moore (205030628)
- * @purpose N-Body simulator: declarations of simulation related parameters
- */
-
-
-#define NUM_THREADS 5 //The number of *worker* threads (not including the computation thread) to use.
- // If this is 1, the computation thread will not spawn any workers (faster to just do the work itself),
- // otherwise it will spawn NUM_THREADS workers.
+#include "../single-thread/nbody.h" //Use original simulation code
#include <pthread.h>
-#include <stdbool.h>
-
-#define M_PI 3.14159265358979323846264338327950288 /* pi */
-#define G 6.67428E-11
-#define DELTA_T 0.05
-#define DIMENSIONS 3
-#define square(x) ((x)*(x))
-
-
-/**
- * Structure to represent a single Body
- * @param mass - Mass of the body
- * @param x - Position vector (array)
- * @param v - Velocity vector (array)
- * @param F - Net force vector (array)
- */
-typedef struct
-{
-
- double mass;
- double x[DIMENSIONS];
- double v[DIMENSIONS];
- double F[DIMENSIONS];
-
-} Body;
-
-/**
- * Structure to store an array of bodies, along with the size of the array.
- * The universe is represented in a single System.
- * @param N - Size of the array
- * @param body - The array of bodies
- */
-typedef struct
-{
- unsigned N;
- Body * body;
-} System;
+#undef SINGLE_THREADED
+#define PTHREADED
-void Body_Print(Body * a); //Print body a
-void Body_Force(Body * a, System * s); //Compute force on body a due to system of bodies s
-void Body_Velocity(Body * a); //Compute velocity of body a
-void Body_Position(Body * a); //Compute position of body a
-
-void System_Init(System * s, char * fileName); //Initialise System (array of bodies) from a text file
-
-void System_Forces(System * s1, System * s2); //Compute forces for bodies in s1 due to bodies in s2 (also updates velocities)
-void System_Positions(System * s); //Update positions for bodies in s1
-void Universe_Cleanup();
-
-extern System universe; // The main array of bodies; global variable.
+#define DEFAULT_WORKING_THREADS 2
+//Undefine default macros, replace with functions
+#undef Simulation_Run
+void Simulation_Run(void);
+#undef QuitProgram
+void QuitProgram(bool error);
-/**
- * Multithreading stuff below here
- */
-extern pthread_t compute_thread; // ID of the thread that runs Compute_Thread. Set in main()
void * Compute_Thread(void * system); //Thread - Continuously perform computations for a System of bodies. May spawn additional worker threads.
void * Force_Thread(void * system); //Thread - Compute forces for all objects in a system
void * Position_Thread(void * system); //Thread - Compute positions for all objects in a system
-typedef enum {RUN, QUIT, QUIT_ERROR} RUNSTATE;
-extern RUNSTATE runstate;
-extern pthread_mutex_t mutex_runstate; //Mutex around the "runstate" variable
-void QuitProgram(bool error);
+void Thread_Cleanup(void); //Called at program exit to safely join computation thread
+
+extern pthread_t compute_thread; // ID of the thread that runs Compute_Thread.
+extern pthread_mutex_t mutex_runstate; //Mutex around the "runstate" variable
+extern pthread_t * worker_thread; //Array of worker threads responsible for Force and Position updates
+extern System * sub_system; //Array of Systems used to divide up the main "universe" System for worker threads
+extern pthread_mutex_t mutex_workers;
+extern pthread_cond_t workers_done_cv;
+extern unsigned workers_busy;
-#endif //_NBODY_H
+#endif //_NBODY_MTHREAD_H