+/**
+ * @function BeforeDraw
+ * @purpose Called in graphics thread before the draw loop
+ * When --pedantic-graphics enabled, will wait for position computations to finish before drawing
+ * Otherwise does nothing
+ *
+ * This originally seemed like a good place to put the code now in StepFunction(), since only one thread runs this
+ * But then I realised that the graphics might be disabled,
+ * and there was no point having a thread that only existed to call that code.
+ *
+ * So I changed it to the horrible solution that I currently have.
+ */
+void BeforeDraw()
+{
+
+ //printf("BEFORE DRAW\n");
+ if (!options.pedantic_graphics)
+ return;
+
+ //printf("Graphics thread waits on position barrier\n");
+ Barrier_Wait(&position_barrier);
+ //printf("\tGraphics thread wakes up\n");
+ Barrier_Enter(&graphics_barrier);
+}
+
+/**
+ * @function AfterDraw
+ * @purpose Called in graphics thread after the draw loop
+ * When --pedantic-graphics is supplied, will signal computation threads that drawing is finished
+ * So that positions can be safely altered
+ * Otherwise does nothing
+ */
+void AfterDraw()
+{
+ //universe.steps += 1;
+ if (!options.pedantic_graphics)
+ return;
+ Barrier_Join(&graphics_barrier);
+
+}
+
+#ifdef PERSISTENT_THREADS
+
+/**
+ * @function Worker_Thread
+ * @purpose Thread - A self contained worker thread to compute a particular sub system of bodies
+ *
+ * This is the "smart" way to do it, because threads are only created once, and compute both force and position.
+ * The greatest difficulty with pthreads is getting a *single* thread from the team to execute certain code
+ * (ie: The stuff in StepFunction()).
+ * With the "continuously respawning threads of stupidity" approach,
+ * because there is one "master" thread (not necessarilly the main thread... don't get confused now)
+ * to keep respawning the workers, the single threaded code can just be executed in the master thread.
+ *
+ * With this approach, I have created a hacky solution so that the *last* thread to leave the position barrier gets to call StepFunction.
+ *
+ */
+void * Worker_Thread(void * arg)
+{
+ System * s = (System*)(arg); // This is mainly to save typing the RHS a lot of times
+
+ // Each thread runs until the whole program is supposed to end
+ while (!ExitCondition())
+ {
+
+
+ System_Forces(s, &universe); // Each thread computes the forces for its share of bodies
+
+ // Do not confuse with "Barrier_Wait".
+ // Barrier_Wait does not affect the barrier; it just waits for it
+ // Barrier_Join actively updates the state of the barrier, and wakes up sleeping threads if required.
+
+ Barrier_Join(&force_barrier); // All threads must reach here before moving on.
+ if (ExitCondition()) return NULL;
+
+
+ //fprintf(stderr,"Thread %p - force barrier finished\n", arg);
+ //printf("Computed ALL forces\n");
+
+
+ // If required, wait for the graphics to finish drawing stuff
+ if (options.draw_graphics && options.pedantic_graphics)
+ {
+ //printf("Worker %p waits on graphics barrier\n", arg);
+ Barrier_Wait(&graphics_barrier);
+ //printf("\tWorker %p wakes up after graphics barrier\n", arg);
+ if (ExitCondition()) return NULL;
+ }
+
+
+
+ Barrier_Enter(&position_barrier);
+ System_Positions(s); // Each thread updates the positions for its share of bodies
+
+
+ // Barrier_JoinCall behaves in the same way as Barrier_Join, except the *last* thread
+ // (ie: the one that wakes up the others) also calls the function with arguments given.
+ Barrier_JoinCall(&position_barrier, StepFunction, (void*)(&universe));
+ if (ExitCondition()) return NULL;
+ //Barrier_Join(&position_barrier);
+
+ // All threads have computed positions, and *one* thread calls StepFunction()
+