set motion capture thread running in the main thread

I identified this while debugging, the thread was created, but hadn't
yet set its state to running before the main thread checked the running
variable and started another thread for the same device.  That
resulted in a crash.  Set running in the main thread, to avoid this
race condition.
This commit is contained in:
David Fries
2012-11-08 22:26:17 -06:00
parent 23d6354d53
commit 0629d9db61

View File

@@ -1088,8 +1088,6 @@ static void *motion_loop(void *arg)
*/
unsigned long int time_last_frame = 1, time_current_frame;
cnt->running = 1;
if (motion_init(cnt) < 0)
goto err;
@@ -2595,11 +2593,22 @@ static void start_motion_thread(struct context *cnt, pthread_attr_t *thread_attr
/* Give the thread WATCHDOG_TMO to start */
cnt->watchdog = WATCHDOG_TMO;
/* Flag it as running outside of the thread, otherwise if the main loop
* checked if it is was running before the thread set it to 1, it would
* start another thread for this device. */
cnt->running = 1;
/*
* Create the actual thread. Use 'motion_loop' as the thread
* function.
*/
pthread_create(&cnt->thread_id, thread_attr, &motion_loop, cnt);
if (pthread_create(&cnt->thread_id, thread_attr, &motion_loop, cnt)) {
/* thread create failed, undo running state */
cnt->running = 0;
pthread_mutex_lock(&global_lock);
threads_running--;
pthread_mutex_unlock(&global_lock);
}
}
/**