Fix race condition between 2 threads 05/196805/2
authorJihoon Jung <jh8801.jung@samsung.com>
Mon, 7 Jan 2019 10:10:42 +0000 (19:10 +0900)
committerJihoon Jung <jh8801.jung@samsung.com>
Mon, 7 Jan 2019 10:10:42 +0000 (19:10 +0900)
Change-Id: I5e653a2fe3131c35937282fb82dffd47cdf0b9d3
Signed-off-by: Jihoon Jung <jh8801.jung@samsung.com>
src/daemon/include/mtp_daemon.h
src/daemon/mtp_daemon_event.c

index 0e378cceec5dd2bcad2329be05f635df95e7ebe3..5774b91274cb2d6b1fe49347c6457a46300e88f8 100755 (executable)
@@ -129,6 +129,7 @@ struct _mtp_device_info {
        int device_number;
        char *model_name;
        char *stitching_engine_version;
+       pthread_mutex_t mutex;
 };
 
 struct _mtp_device_list {
index 49834fe63ede83fafcb142cfdf0001a52967e651..b9669dbdf4da774faa85668426ba2d9e10101c84 100755 (executable)
@@ -113,6 +113,8 @@ int __usb_host_status_changed_cb(struct libusb_context *ctx,
                                device_info->device_number = raw_devices[slot].devnum;
                                device_info->model_name = LIBMTP_Get_Modelname(device);
 
+                               pthread_mutex_init(&device_info->mutex, NULL);
+
                                mtp_ctx->device_list->device_info_list[empty_slot] = device_info;
                                mtp_ctx->device_list->device_num++;
 
@@ -149,6 +151,18 @@ int __usb_host_status_changed_cb(struct libusb_context *ctx,
                                        return 0;
                                }
 
+                               struct timeval now;
+                               struct timespec ts;
+
+                               gettimeofday(&now, NULL);
+                               ts.tv_sec = now.tv_sec + 2;
+
+                               MTP_LOGI("Wait for 2 seconds to terminate Event Thread.");
+                               pthread_mutex_timedlock(&device_info->mutex, &ts);
+                               pthread_mutex_unlock(&device_info->mutex);
+                               pthread_mutex_destroy(&device_info->mutex);
+                               MTP_LOGI("The Event Thread is terminated");
+
                                LIBMTP_Release_Device(device_info->device);
                                mtp_daemon_db_delete(device_id, 0, 0, mtp_ctx);
 
@@ -193,6 +207,8 @@ static void* __event_thread(gpointer dev, gpointer data)
        if (device_info == NULL)
                return NULL;
 
+       pthread_mutex_lock(&device_info->mutex);
+
        MTP_LOGI("%s, %03d-%03d event loop start", device_info->model_name,
                device_info->bus_location, device_info->device_number);
 
@@ -231,6 +247,8 @@ static void* __event_thread(gpointer dev, gpointer data)
        MTP_LOGI("%s, %03d-%03d event loop stop", device_info->model_name,
                device_info->bus_location, device_info->device_number);
 
+       pthread_mutex_unlock(&device_info->mutex);
+
        return NULL;
 }
 
@@ -304,6 +322,8 @@ mtp_error_e __device_list_init(mtp_context *mtp_ctx)
                if (device_info->model_name == NULL)
                        device_info->model_name = strdup(tmp_name); //LCOV_EXCL_LINE
 
+               pthread_mutex_init(&device_info->mutex, NULL);
+
                mtp_ctx->device_list->device_info_list[empty_slot] = device_info;
                mtp_ctx->device_list->device_num++;
        }