diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2011-05-12 17:45:14 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2011-05-12 17:45:14 (GMT) |
| commit | 351565c6e2ae0910fa9e167006779e05aaa35c14 (patch) | |
| tree | 9cdbee3c8fd75872ce1655ae2ef9a69aa042607b /src/main.c | |
| parent | ee962dc8bb679a47e11456cedf24013de4594b64 (diff) | |
| download | powder-351565c6e2ae0910fa9e167006779e05aaa35c14.zip powder-351565c6e2ae0910fa9e167006779e05aaa35c14.tar.gz | |
Fix some thread sync and gravity sync issues
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 21 |
1 files changed, 17 insertions, 4 deletions
@@ -189,6 +189,7 @@ int numCores = 4; pthread_t gravthread; pthread_mutex_t gravmutex; int grav_ready = 0; +int gravthread_done = 0; int core_count() { @@ -1218,19 +1219,27 @@ char my_uri[] = "http://" SERVER "/Update.api?Action=Download&Architecture=" void update_grav_async() { 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){ update_grav(); done = 1; pthread_mutex_lock(&gravmutex); grav_ready = done; + thread_done = gravthread_done; pthread_mutex_unlock(&gravmutex); } else { pthread_mutex_lock(&gravmutex); done = grav_ready; + thread_done = gravthread_done; pthread_mutex_unlock(&gravmutex); } } + pthread_exit(NULL); } void start_grav_async() @@ -1238,6 +1247,7 @@ void start_grav_async() if(!ngrav_enable){ /*pthread_mutexattr_t gma; //I do not know why this is here pthread_mutexattr_init(&gma);*/ + gravthread_done = 0; pthread_mutex_init (&gravmutex, NULL); pthread_create(&gravthread, NULL, update_grav_async, NULL); //Start asynchronous gravity simulation ngrav_enable = 1; @@ -1247,7 +1257,10 @@ void start_grav_async() void stop_grav_async() { 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 memset(gravy, 0, sizeof(gravy)); //Clear the grav velocities memset(gravx, 0, sizeof(gravx)); //Clear the grav velocities @@ -1589,8 +1602,8 @@ int main(int argc, char *argv[]) if (!sys_pause||framerender) //Only update if not paused memset(gravmap, 0, sizeof(gravmap)); //Clear the old gravmap - - draw_grav(vid_buf); + if(ngrav_enable) + draw_grav(vid_buf); update_particles(vid_buf); //update everything draw_parts(vid_buf); //draw particles |
