Fix some thread sync and gravity sync issues

This commit is contained in:
Simon Robertshaw 2011-05-12 18:45:14 +01:00
parent ee962dc8bb
commit 351565c6e2

View File

@ -189,6 +189,7 @@ int numCores = 4;
pthread_t gravthread; pthread_t gravthread;
pthread_mutex_t gravmutex; pthread_mutex_t gravmutex;
int grav_ready = 0; int grav_ready = 0;
int gravthread_done = 0;
int core_count() int core_count()
{ {
@ -1218,19 +1219,27 @@ char my_uri[] = "http://" SERVER "/Update.api?Action=Download&Architecture="
void update_grav_async() void update_grav_async()
{ {
int done = 0; int done = 0;
while(1){ int thread_done = 0;
memset(th_ogravmap, 0, sizeof(th_ogravmap));
memset(th_gravmap, 0, sizeof(th_gravmap));
memset(th_gravy, 0, sizeof(th_gravy));
memset(th_gravx, 0, sizeof(th_gravx));
while(!thread_done){
if(!done){ if(!done){
update_grav(); update_grav();
done = 1; done = 1;
pthread_mutex_lock(&gravmutex); pthread_mutex_lock(&gravmutex);
grav_ready = done; grav_ready = done;
thread_done = gravthread_done;
pthread_mutex_unlock(&gravmutex); pthread_mutex_unlock(&gravmutex);
} else { } else {
pthread_mutex_lock(&gravmutex); pthread_mutex_lock(&gravmutex);
done = grav_ready; done = grav_ready;
thread_done = gravthread_done;
pthread_mutex_unlock(&gravmutex); pthread_mutex_unlock(&gravmutex);
} }
} }
pthread_exit(NULL);
} }
void start_grav_async() void start_grav_async()
@ -1238,6 +1247,7 @@ void start_grav_async()
if(!ngrav_enable){ if(!ngrav_enable){
/*pthread_mutexattr_t gma; //I do not know why this is here /*pthread_mutexattr_t gma; //I do not know why this is here
pthread_mutexattr_init(&gma);*/ pthread_mutexattr_init(&gma);*/
gravthread_done = 0;
pthread_mutex_init (&gravmutex, NULL); pthread_mutex_init (&gravmutex, NULL);
pthread_create(&gravthread, NULL, update_grav_async, NULL); //Start asynchronous gravity simulation pthread_create(&gravthread, NULL, update_grav_async, NULL); //Start asynchronous gravity simulation
ngrav_enable = 1; ngrav_enable = 1;
@ -1247,7 +1257,10 @@ void start_grav_async()
void stop_grav_async() void stop_grav_async()
{ {
if(ngrav_enable){ if(ngrav_enable){
pthread_cancel(gravthread); //Terminate the thread, we don't really care if it finished or not pthread_mutex_lock(&gravmutex);
gravthread_done = 1;
pthread_mutex_unlock(&gravmutex);
pthread_join(gravthread, NULL);
pthread_mutex_destroy(&gravmutex); //Destroy the mutex pthread_mutex_destroy(&gravmutex); //Destroy the mutex
memset(gravy, 0, sizeof(gravy)); //Clear the grav velocities memset(gravy, 0, sizeof(gravy)); //Clear the grav velocities
memset(gravx, 0, sizeof(gravx)); //Clear the grav velocities memset(gravx, 0, sizeof(gravx)); //Clear the grav velocities
@ -1589,7 +1602,7 @@ int main(int argc, char *argv[])
if (!sys_pause||framerender) //Only update if not paused if (!sys_pause||framerender) //Only update if not paused
memset(gravmap, 0, sizeof(gravmap)); //Clear the old gravmap memset(gravmap, 0, sizeof(gravmap)); //Clear the old gravmap
if(ngrav_enable)
draw_grav(vid_buf); draw_grav(vid_buf);
update_particles(vid_buf); //update everything update_particles(vid_buf); //update everything
draw_parts(vid_buf); //draw particles draw_parts(vid_buf); //draw particles