Fix race condition in Gravity.cpp
This commit is contained in:
parent
ff39c82e48
commit
2ba0f70efd
1
.gitignore
vendored
1
.gitignore
vendored
@ -65,5 +65,6 @@ config.log
|
||||
*.pyc
|
||||
site_scons/site_tools/mfprogram/*.pyc
|
||||
site_scons/site_tools/gch/*.pyc
|
||||
.vscode/ipch
|
||||
|
||||
screenshot_*
|
||||
|
@ -85,7 +85,8 @@ void Gravity::gravity_update_async()
|
||||
int result;
|
||||
if (ngrav_enable)
|
||||
{
|
||||
pthread_mutex_lock(&gravmutex);
|
||||
if (!pthread_mutex_trylock(&gravmutex))
|
||||
{
|
||||
result = grav_ready;
|
||||
if (result) //Did the gravity thread finish?
|
||||
{
|
||||
@ -124,6 +125,7 @@ void Gravity::gravity_update_async()
|
||||
//}
|
||||
}
|
||||
pthread_mutex_unlock(&gravmutex);
|
||||
}
|
||||
//Apply the gravity mask
|
||||
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));
|
||||
@ -153,26 +155,24 @@ void Gravity::update_grav_async()
|
||||
if (!grav_fft_status)
|
||||
grav_fft_init();
|
||||
#endif
|
||||
while(!thread_done){
|
||||
if(!done){
|
||||
pthread_mutex_lock(&gravmutex);
|
||||
while (!thread_done)
|
||||
{
|
||||
if (!done)
|
||||
{
|
||||
// run gravity update
|
||||
update_grav();
|
||||
done = 1;
|
||||
pthread_mutex_lock(&gravmutex);
|
||||
|
||||
grav_ready = done;
|
||||
grav_ready = 1;
|
||||
thread_done = gravthread_done;
|
||||
|
||||
pthread_mutex_unlock(&gravmutex);
|
||||
} else {
|
||||
pthread_mutex_lock(&gravmutex);
|
||||
// wait for main thread
|
||||
pthread_cond_wait(&gravcv, &gravmutex);
|
||||
|
||||
done = grav_ready;
|
||||
thread_done = gravthread_done;
|
||||
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&gravmutex);
|
||||
}
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
@ -196,7 +196,8 @@ void Gravity::start_grav_async()
|
||||
|
||||
void Gravity::stop_grav_async()
|
||||
{
|
||||
if(ngrav_enable){
|
||||
if (ngrav_enable)
|
||||
{
|
||||
pthread_mutex_lock(&gravmutex);
|
||||
gravthread_done = 1;
|
||||
pthread_cond_signal(&gravcv);
|
||||
|
Reference in New Issue
Block a user