[ElmSharp] Fix EcoreMainloop crash issue in multi thread 56/162256/1
authorSeungkeun Lee <sngn.lee@samsung.com>
Thu, 30 Nov 2017 02:51:21 +0000 (11:51 +0900)
committerSeungkeun Lee <sngn.lee@samsung.com>
Thu, 30 Nov 2017 02:51:21 +0000 (11:51 +0900)
 - EcoreMainLoop.Post allow to call in worker thread
 - Dictionary.Add was used in Post and it didn't support multi-thread and did not protected by lock
 - So, Replace to ConcurrentDictionary

Change-Id: I0a964d49317380ba81751c4d4b1cc0484f7a39c5

src/ElmSharp/ElmSharp/EcoreMainloop.cs [changed mode: 0755->0644]

old mode 100755 (executable)
new mode 100644 (file)
index b15658f..60202fa
@@ -15,7 +15,7 @@
  */
 
 using System;
-using System.Collections.Generic;
+using System.Collections.Concurrent;
 
 namespace ElmSharp
 {
@@ -25,7 +25,8 @@ namespace ElmSharp
     /// <since_tizen> preview </since_tizen>
     public static class EcoreMainloop
     {
-        static readonly Dictionary<int, Func<bool>> _taskMap = new Dictionary<int, Func<bool>>();
+
+        static readonly ConcurrentDictionary<int, Func<bool>> _taskMap = new ConcurrentDictionary<int, Func<bool>>();
         static readonly Object _taskLock = new Object();
         static int _newTaskId = 0;
 
@@ -117,7 +118,8 @@ namespace ElmSharp
         public static void RemoveTimer(IntPtr id)
         {
             int taskId = (int)Interop.Ecore.ecore_timer_del(id);
-            _taskMap.Remove(taskId);
+            Func<bool> unused;
+            _taskMap.TryRemove(taskId, out unused);
         }
 
         static int RegistHandler(Func<bool> task)
@@ -140,11 +142,14 @@ namespace ElmSharp
                 bool result = false;
 
                 if (userAction != null)
+                {
                     result = userAction();
+                }
 
-                if (result == false)
-                    _taskMap.Remove(task_id);
-
+                if (!result)
+                {
+                    _taskMap.TryRemove(task_id, out userAction);
+                }
                 return result;
             }
             return false;