diff --git a/wrappers/c_sync/libfreenect_sync.c b/wrappers/c_sync/libfreenect_sync.c index 0ca3d257..68d2ba75 100644 --- a/wrappers/c_sync/libfreenect_sync.c +++ b/wrappers/c_sync/libfreenect_sync.c @@ -214,15 +214,18 @@ static void *init(void *unused) return NULL; } -static void init_thread(void) +static int init_thread(void) { thread_running = 1; - freenect_init(&ctx, 0); + int ret = freenect_init(&ctx, 0); + if (ret != 0) return ret; // We claim both the motor and the camera, because we can't know in advance // which devices the caller will want, and the c_sync interface doesn't // support audio, so there's no reason to claim the device needlessly. freenect_select_subdevices(ctx, (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA)); - pthread_create(&thread, NULL, init, NULL); + ret = pthread_create(&thread, NULL, init, NULL); + if (ret != 0) return ret; + return 0; } static int change_video_format(sync_kinect_t *kinect, freenect_resolution res, freenect_video_format fmt) @@ -279,8 +282,10 @@ static int setup_kinect(int index, int res, int fmt, int is_depth) pending_runloop_tasks_inc(); pthread_mutex_lock(&runloop_lock); int thread_running_prev = thread_running; - if (!thread_running) - init_thread(); + if (!thread_running) { + int ret = init_thread(); + if (ret != 0) return ret; + } if (!kinects[index]) { kinects[index] = alloc_kinect(index); } @@ -334,15 +339,15 @@ static int sync_get(void **data, uint32_t *timestamp, buffer_ring_t *buf) } -/* +/* Use this to make sure the runloop is locked and no one is in it. Then you can call arbitrary functions from libfreenect.h in a safe way. If the kinect with - this index has not been initialized yet, then it will try to set it up. If + this index has not been initialized yet, then it will try to set it up. If this function is successful, then you can access kinects[index]. Don't forget to unlock the runloop when you're done. - + Returns 0 if successful, nonzero if kinect[index] is unvailable - */ + */ static int runloop_enter(int index) { if (index < 0 || index >= MAX_KINECTS) { @@ -352,7 +357,7 @@ static int runloop_enter(int index) if (!thread_running || !kinects[index]) if (setup_kinect(index, FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT, 1)) return -1; - + pending_runloop_tasks_inc(); pthread_mutex_lock(&runloop_lock); return 0; @@ -407,7 +412,7 @@ int freenect_sync_get_tilt_state(freenect_raw_tilt_state **state, int index) { if (runloop_enter(index)) return -1; freenect_update_tilt_state(kinects[index]->dev); - *state = freenect_get_tilt_state(kinects[index]->dev); + *state = freenect_get_tilt_state(kinects[index]->dev); runloop_exit(); return 0; }