X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=course%2Fsemester2%2Fpprog%2Fassignment1%2Fmthread%2Fnbody.c;h=f59b451228e37359697159a50204760395774ddc;hb=83cf58f62ddc5ce9fe44de27327fde27f767ebb6;hp=4acf28f2c9cd382d8298b7e10f2ac59e9773fcb3;hpb=19e590cc4f7dbc186cad9418bf5a2321bfa3fe1a;p=matches%2Fhonours.git diff --git a/course/semester2/pprog/assignment1/mthread/nbody.c b/course/semester2/pprog/assignment1/mthread/nbody.c index 4acf28f2..f59b4512 100644 --- a/course/semester2/pprog/assignment1/mthread/nbody.c +++ b/course/semester2/pprog/assignment1/mthread/nbody.c @@ -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