pthread_mutex_t mutex_runstate; // Mutex around the runstate
-
+pthread_mutex_t mutex_graphics; //Mutex between graphics and computation
+pthread_cond_t graphics_cv; //Condition used to start graphics or computation thread from the other
+bool graphics_busy = false; // Indicate if graphics is currently drawing
/**
* @function Compute_Thread
pthread_exit(NULL);
}
- if (options.draw_graphics == false && options.verbosity != 0
- && universe.steps % options.verbosity == 1)
+ if (options.verbosity != 0 && universe.steps % options.verbosity == 1)
{
DisplayStatistics();
}
pthread_mutex_unlock(&mutex_workers);
//All the forces are now computed
+
+ // Wait for graphics to finish drawing the last step
+ if (options.pedantic_graphics && options.draw_graphics)
+ {
+ pthread_mutex_lock(&mutex_graphics);
+ while (graphics_busy)
+ pthread_cond_wait(&graphics_cv, &mutex_graphics);
+ pthread_mutex_unlock(&mutex_graphics);
+ }
workers_busy = options.num_threads; //All threads working
universe.steps += 1;
+ // Signal graphics thread to draw bodies
+ if (options.pedantic_graphics && options.draw_graphics)
+ {
+ pthread_mutex_lock(&mutex_graphics);
+ graphics_busy = true;
+ pthread_cond_signal(&graphics_cv);
+ pthread_mutex_unlock(&mutex_graphics);
+ }
}
}
+/**
+ * @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
+ */
+void BeforeDraw()
+{
+ if (!options.pedantic_graphics)
+ return;
+ pthread_mutex_lock(&mutex_graphics);
+ while (!graphics_busy)
+ pthread_cond_wait(&graphics_cv, &mutex_graphics);
+ pthread_mutex_unlock(&mutex_graphics);
+}
+
+/**
+ * @function AfterDraw
+ * @purpose Called in graphics thread after the draw loop
+ * When --pedantic-graphics is supplied, will signal computation thread that drawing is finished
+ * So that positions can be safely altered
+ * Otherwise does nothing
+ */
+void AfterDraw()
+{
+ if (!options.pedantic_graphics)
+ return;
+ pthread_mutex_lock(&mutex_graphics);
+ graphics_busy = false;
+ pthread_cond_signal(&graphics_cv);
+ pthread_mutex_unlock(&mutex_graphics);
+}
+
/**
* @function Force_Thread
* @purpose Thread - Calculates the forces on objects in a System