Fix race condition in Gravity.cpp

This commit is contained in:
iczero
2019-04-07 14:05:23 -07:00
committed by Tamás Bálint Misius
parent ff39c82e48
commit 2ba0f70efd
2 changed files with 48 additions and 46 deletions

1
.gitignore vendored
View File

@@ -65,5 +65,6 @@ config.log
*.pyc *.pyc
site_scons/site_tools/mfprogram/*.pyc site_scons/site_tools/mfprogram/*.pyc
site_scons/site_tools/gch/*.pyc site_scons/site_tools/gch/*.pyc
.vscode/ipch
screenshot_* screenshot_*

View File

@@ -85,7 +85,8 @@ void Gravity::gravity_update_async()
int result; int result;
if (ngrav_enable) if (ngrav_enable)
{ {
pthread_mutex_lock(&gravmutex); if (!pthread_mutex_trylock(&gravmutex))
{
result = grav_ready; result = grav_ready;
if (result) //Did the gravity thread finish? if (result) //Did the gravity thread finish?
{ {
@@ -124,6 +125,7 @@ void Gravity::gravity_update_async()
//} //}
} }
pthread_mutex_unlock(&gravmutex); pthread_mutex_unlock(&gravmutex);
}
//Apply the gravity mask //Apply the gravity mask
membwand(gravy, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); membwand(gravy, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned));
membwand(gravx, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned)); membwand(gravx, gravmask, (XRES/CELL)*(YRES/CELL)*sizeof(float), (XRES/CELL)*(YRES/CELL)*sizeof(unsigned));
@@ -153,26 +155,24 @@ void Gravity::update_grav_async()
if (!grav_fft_status) if (!grav_fft_status)
grav_fft_init(); grav_fft_init();
#endif #endif
while(!thread_done){ pthread_mutex_lock(&gravmutex);
if(!done){ while (!thread_done)
{
if (!done)
{
// run gravity update
update_grav(); update_grav();
done = 1; done = 1;
pthread_mutex_lock(&gravmutex); grav_ready = 1;
grav_ready = done;
thread_done = gravthread_done; thread_done = gravthread_done;
pthread_mutex_unlock(&gravmutex);
} else { } else {
pthread_mutex_lock(&gravmutex); // wait for main thread
pthread_cond_wait(&gravcv, &gravmutex); pthread_cond_wait(&gravcv, &gravmutex);
done = grav_ready; done = grav_ready;
thread_done = gravthread_done; thread_done = gravthread_done;
}
}
pthread_mutex_unlock(&gravmutex); pthread_mutex_unlock(&gravmutex);
}
}
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -196,7 +196,8 @@ void Gravity::start_grav_async()
void Gravity::stop_grav_async() void Gravity::stop_grav_async()
{ {
if(ngrav_enable){ if (ngrav_enable)
{
pthread_mutex_lock(&gravmutex); pthread_mutex_lock(&gravmutex);
gravthread_done = 1; gravthread_done = 1;
pthread_cond_signal(&gravcv); pthread_cond_signal(&gravcv);