MTP_LOGI("bus_no : %03d, dev_no : %03d", bus_no, dev_no);
+ pthread_mutex_lock(&mtp_ctx->usb_thread_mutex);
+
if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) {
MTP_LOGI("USB device added");
}
}
+ pthread_mutex_unlock(&mtp_ctx->usb_thread_mutex);
+
return 0;
}
/* LCOV_EXCL_STOP */
RETV_IF(mtp_ctx == NULL, MTP_ERROR_INVALID_PARAMETER);
+ pthread_mutex_lock(&mtp_ctx->usb_thread_mutex);
+
LIBMTP_Set_Debug(LIBMTP_DEBUG_ALL);
LIBMTP_Init();
LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
}
g_free(rawdevices);
+ pthread_mutex_unlock(&mtp_ctx->usb_thread_mutex);
+
return ret;
}
/* LCOV_EXCL_STOP */
}
- MTP_LOGE("dispatch thread created");
+ MTP_LOGE("dispatch thread created. Signal the main thread that work is done");
+ pthread_mutex_lock(&mtp_ctx->usb_thread_mutex);
+ pthread_cond_signal(&mtp_ctx->usb_thread_cond);
+ pthread_mutex_unlock(&mtp_ctx->usb_thread_mutex);
+
+ MTP_LOGE("USB event thread prepared");
while (mtp_ctx->is_running) {
libusb_handle_events_completed(mtp_ctx->usb_ctx, NULL);
}
for (slot = 1; slot < MTP_MAX_SLOT; slot++)
mtp_ctx->device_list->device_info_list[slot] = NULL;
- /* mtp device list init */
- if (__device_list_init(mtp_ctx) != MTP_ERROR_NONE)
- return MTP_ERROR_NO_DEVICE;
+ if (pthread_mutex_init(&mtp_ctx->usb_thread_mutex, NULL) != 0) {
+ MTP_LOGE("Failed to initialize mutex");
+ return MTP_ERROR_PLUGIN_FAIL;
+ }
+
+ if (pthread_cond_init(&mtp_ctx->usb_thread_cond, NULL) != 0) {
+ MTP_LOGE("Failed to initialize condition variable");
+ pthread_mutex_destroy(&mtp_ctx->usb_thread_mutex);
+ return MTP_ERROR_PLUGIN_FAIL;
+ }
/* usb event setting */
ret = libusb_init(&(mtp_ctx->usb_ctx));
/* LCOV_EXCL_STOP */
}
+ MTP_LOGE("Waits up to 5 sec for the USB event thread to become ready");
+ pthread_mutex_lock(&mtp_ctx->usb_thread_mutex);
+ pthread_cond_timedwait(&mtp_ctx->usb_thread_cond, &mtp_ctx->usb_thread_mutex, &(struct timespec){time(NULL) + 5});
+ pthread_mutex_unlock(&mtp_ctx->usb_thread_mutex);
+ MTP_LOGE("USB event thread prepared");
+
+ /* mtp device list init */
+ if (__device_list_init(mtp_ctx) != MTP_ERROR_NONE)
+ return MTP_ERROR_NO_DEVICE;
+
/* create thread pool */
mtp_ctx->device_list->threads
= g_thread_pool_new((GFunc) __event_thread, mtp_ctx, MTP_MAX_SLOT-1, TRUE, NULL);