1 #ifndef _NBODY_MTHREAD_H
2 #define _NBODY_MTHREAD_H
5 #include "../single-thread/nbody.h" //Use original simulation code
11 #define DEFAULT_WORKING_THREADS 2
13 #define PERSISTENT_THREADS //If defined, threads will not be continually destroyed and then respawned
17 //Undefine default macros, replace with functions
19 void Simulation_Run(int argc, char ** argv);
21 void QuitProgram(bool error);
29 void * Compute_Thread(void * system); //Thread - Continuously perform computations for a System of bodies. May spawn additional worker threads.
32 System * Split_System(System * s, unsigned n); // Splits one system into a number of other systems, returns an array of size n
33 pthread_t * Allocate_Threads(unsigned n); // Allocates space for threads - handles errors
35 #ifdef PERSISTENT_THREADS
36 void * Worker_Thread(void * arg);
37 #endif //PERSISTENT_THREADS
38 void * Force_Thread(void * system); //Thread - Compute forces for all objects in a system
39 void * Position_Thread(void * system); //Thread - Compute positions for all objects in a system
41 void Thread_Cleanup(void); //Called at program exit to safely join computation thread
44 * Structure to represent a barrier for multiple threads
45 * @param mutex - Mutex around the counter
46 * @param busy - Counter of threads within the barrier
47 * @param threads_done_cv - Condition to wake up threads waiting on barrier once all working threads have left it
51 pthread_mutex_t mutex;
52 unsigned threads_busy; // Counter of threads which are busy
53 pthread_cond_t threads_done_cv;
57 * Structure to represent a pair of Systems; passed to Force_Thread
58 * @param A - System to calculate forces for
59 * @param B - System causing forces on System A
67 void Barrier_Init(Barrier * b);
68 void Barrier_Enter(Barrier * b);
69 void Barrier_Leave(Barrier * b);
70 void Barrier_Wait(Barrier * b);
72 #endif //_NBODY_MTHREAD_H