Fixed the problem that push/pull progress information is broken.
authorKim Gunsoo <gunsoo83.kim@samsung.com>
Thu, 8 Sep 2016 04:58:40 +0000 (13:58 +0900)
committerKim Gunsoo <gunsoo83.kim@samsung.com>
Thu, 8 Sep 2016 05:58:47 +0000 (14:58 +0900)
- When outputting the progress information, there was a problem
  for reentrant of multi-threaded. It was resolved using the
  critical section.

Change-Id: Ie6d62ca2dfd4af44b8d7d4746fdfda13d6146433
Signed-off-by: Kim Gunsoo <gunsoo83.kim@samsung.com>
src/file_sync_client.c
src/utils.h
src/utils_windows.c

index 741f96a05e5491e055527aaee70eb1aa731d5975..d9c5cb992c3977883c1cf26e80da666c7e88dd78 100644 (file)
@@ -88,7 +88,9 @@ static void deinit_sync_progress_timer(void) {
 HANDLE hTimer = NULL;
 
 static VOID CALLBACK update_progress(PVOID lpParam, BOOLEAN TimerOrWaitFired) {
-    print_progress(SDB_PROGRESS_STAGE_TRANSFERRING);
+    if (hTimer != NULL) {
+        print_progress(SDB_PROGRESS_STAGE_TRANSFERRING);
+    }
 }
 
 static void init_sync_progress_timer(void) {
@@ -104,6 +106,7 @@ static void deinit_sync_progress_timer(void) {
         D("failed to DeleteTimerQueueTimer()\n");
         return;
     }
+    hTimer = NULL;
 }
 #endif
 
@@ -169,7 +172,15 @@ void print_progress(int stage) {
     unsigned percent = 0;
     unsigned speed = 0;
     unsigned progress_bytes = 0;
+    static int is_finished = 0;
+
+    /* initialize timer */
+    if (stage == SDB_PROGRESS_STAGE_STARTED) {
+        init_sync_progress_timer();
+        is_finished = 0;
+    }
 
+    /* calc progress information */
     sdb_mutex_lock(&prg_info_lock, "read written_bytes. print_progress()");
     progress_bytes = g_prg_info.written_bytes;
     sdb_mutex_unlock(&prg_info_lock, "read written_bytes. print_progress()");
@@ -177,16 +188,25 @@ void print_progress(int stage) {
     speed = calc_progress_speed(stage, progress_bytes);
     percent = calc_progress_percent(progress_bytes);
 
-    //TODO: consider current window size.
-    fprintf(stdout,"\r%s %30s\t%3d%%\t%7d%s\t%5dKB/s", g_prg_info.tag, g_prg_info.file_name,
+    /* print progress information */
+    sdb_mutex_lock(&prg_print_lock, "print progress info. print_progress()");
+    if (!is_finished) {
+        //TODO: consider current window size.
+        fprintf(stdout,"\r%s %30s\t%3d%%\t%7d%s\t%5dKB/s", g_prg_info.tag, g_prg_info.file_name,
                 percent, (progress_bytes/g_prg_info.flag_size), g_prg_info.byte_flag, speed);
-    fflush(stdout);
+        fflush(stdout);
+    }
+    sdb_mutex_unlock(&prg_print_lock, "print progress info. print_progress()");
 
-    if (stage == SDB_PROGRESS_STAGE_STARTED) {
-        init_sync_progress_timer();
-    } else if (stage == SDB_PROGRESS_STAGE_FINISHED) {
+    /* deinitialize timer and print '\n' */
+    if (stage == SDB_PROGRESS_STAGE_FINISHED) {
         deinit_sync_progress_timer();
+
+        sdb_mutex_lock(&prg_print_lock, "print progress info. print_progress()");
         fprintf(stdout,"\n");
+        fflush(stdout);
+        is_finished = 1;
+        sdb_mutex_unlock(&prg_print_lock, "print progress info. print_progress()");
     }
 }
 
index 29c61a02499689c1ade3c03e545f918851f10c20..1c72e5551d83ddaf2e198623c6a3d9c4c0808eac 100755 (executable)
@@ -106,6 +106,7 @@ SDB_MUTEX(wakeup_select_lock)
 SDB_MUTEX(usb_lock)
 SDB_MUTEX(D_lock)
 SDB_MUTEX(prg_info_lock)
+SDB_MUTEX(prg_print_lock)
 
 int launch_server();
 void start_logging(void);
index 7d180e2b25efa101d3ce9803ac778b4be01d386d..5bb7d06418b155f5051f4a79ada0fcbdeafbb9ae 100755 (executable)
@@ -972,6 +972,7 @@ static void _sdb_sysdeps_init(void) {
     SDB_MUTEX(_win32_lock);
     SDB_MUTEX(sdb_handle_map_lock);
     SDB_MUTEX(prg_info_lock);
+    SDB_MUTEX(prg_print_lock);
 }
 
 typedef  void (*win_thread_func_t)(void*  arg);