Parallel Programming - Commit before I break things
[matches/honours.git] / course / semester2 / pprog / assignment1 / mthread / nbody.c
index 4acf28f..f59b451 100644 (file)
@@ -21,7 +21,9 @@ unsigned workers_busy; // Number of workers currently doing something
 
 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
@@ -94,8 +96,7 @@ void * Compute_Thread(void * arg)
                        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();
                }
@@ -134,6 +135,15 @@ void * Compute_Thread(void * arg)
                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
 
@@ -158,10 +168,51 @@ void * Compute_Thread(void * arg)
                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

UCC git Repository :: git.ucc.asn.au