[Tts/Stt] Use thread lock only when add and remove event handler (#2782)
authorSuyeon Hwang <35016426+stom-hwang@users.noreply.github.com>
Thu, 25 Mar 2021 01:51:25 +0000 (10:51 +0900)
committerGitHub <noreply@github.com>
Thu, 25 Mar 2021 01:51:25 +0000 (10:51 +0900)
* [Tts] Use thread lock only when add and remove event handler

Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
* [Stt] Use thread lock only when add and remove event handler

Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
* [Tts/Stt] Remove useless thread lock statements

null conditional operator already guarantees the thread safety.
So it does not need additional thread lock.

Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
src/Tizen.Uix.Stt/Tizen.Uix.Stt/SttClient.cs
src/Tizen.Uix.Tts/Tizen.Uix.Tts/TtsClient.cs

index c39b953..30a6cb7 100644 (file)
@@ -320,13 +320,17 @@ namespace Tizen.Uix.Stt
     public class SttClient : IDisposable
     {
         private IntPtr _handle;
-        private Object thisLock = new Object();
         private event EventHandler<RecognitionResultEventArgs> _recognitionResult;
         private event EventHandler<StateChangedEventArgs> _stateChanged;
         private event EventHandler<ErrorOccurredEventArgs> _errorOccurred;
         private event EventHandler<DefaultLanguageChangedEventArgs> _defaultLanguageChanged;
         private event EventHandler<EngineChangedEventArgs> _engineChanged;
         private bool disposedValue = false;
+        private readonly Object _recognitionResultLock = new Object();
+        private readonly Object _stateChangedLock = new Object();
+        private readonly Object _errorOccurredLock = new Object();
+        private readonly Object _defaultLanguageChangedLock = new Object();
+        private readonly Object _engineChangedLock = new Object();
         private Interop.Stt.RecognitionResultCallback _resultDelegate;
         private Interop.Stt.StateChangedCallback _stateDelegate;
         private Interop.Stt.ErrorCallback _errorDelegate;
@@ -379,7 +383,7 @@ namespace Tizen.Uix.Stt
         {
             add
             {
-                lock (thisLock)
+                lock (_recognitionResultLock)
                 {
                     if (_recognitionResult == null)
                     {
@@ -409,7 +413,7 @@ namespace Tizen.Uix.Stt
 
             remove
             {
-                lock (thisLock)
+                lock (_recognitionResultLock)
                 {
                     _recognitionResult -= value;
                     if (_recognitionResult == null)
@@ -432,7 +436,7 @@ namespace Tizen.Uix.Stt
         {
             add
             {
-                lock (thisLock)
+                lock (_stateChangedLock)
                 {
                     if (_stateChanged == null)
                     {
@@ -455,7 +459,7 @@ namespace Tizen.Uix.Stt
 
             remove
             {
-                lock (thisLock)
+                lock (_stateChangedLock)
                 {
                     _stateChanged -= value;
                     if (_stateChanged == null)
@@ -468,7 +472,6 @@ namespace Tizen.Uix.Stt
                     }
                 }
             }
-
         }
 
         /// <summary>
@@ -479,7 +482,7 @@ namespace Tizen.Uix.Stt
         {
             add
             {
-                lock (thisLock)
+                lock (_errorOccurredLock)
                 {
                     if (_errorOccurred == null)
                     {
@@ -501,7 +504,7 @@ namespace Tizen.Uix.Stt
 
             remove
             {
-                lock (thisLock)
+                lock (_errorOccurredLock)
                 {
                     _errorOccurred -= value;
                     if (_errorOccurred == null)
@@ -524,7 +527,7 @@ namespace Tizen.Uix.Stt
         {
             add
             {
-                lock (thisLock)
+                lock (_defaultLanguageChangedLock)
                 {
                     if (_defaultLanguageChanged == null)
                     {
@@ -548,7 +551,7 @@ namespace Tizen.Uix.Stt
 
             remove
             {
-                lock (thisLock)
+                lock (_defaultLanguageChangedLock)
                 {
                     _defaultLanguageChanged -= value;
                     if (_defaultLanguageChanged == null)
@@ -572,7 +575,7 @@ namespace Tizen.Uix.Stt
         {
             add
             {
-                lock (thisLock)
+                lock (_engineChangedLock)
                 {
                     if (_engineChanged == null)
                     {
@@ -596,7 +599,7 @@ namespace Tizen.Uix.Stt
 
             remove
             {
-                lock (thisLock)
+                lock (_engineChangedLock)
                 {
                     _engineChanged -= value;
                     if (_engineChanged == null)
@@ -631,14 +634,11 @@ namespace Tizen.Uix.Stt
             get
             {
                 string language;
-                lock (thisLock)
+                SttError error = SttGetDefaultLanguage(_handle, out language);
+                if (error != SttError.None)
                 {
-                    SttError error = SttGetDefaultLanguage(_handle, out language);
-                    if (error != SttError.None)
-                    {
-                        Log.Error(LogTag, "DefaultLanguage Failed with error " + error);
-                        return "";
-                    }
+                    Log.Error(LogTag, "DefaultLanguage Failed with error " + error);
+                    return "";
                 }
 
                 return language;
@@ -663,14 +663,11 @@ namespace Tizen.Uix.Stt
             get
             {
                 float volume;
-                lock (thisLock)
+                SttError error = SttGetRecordingVolume(_handle, out volume);
+                if (error != SttError.None)
                 {
-                    SttError error = SttGetRecordingVolume(_handle, out volume);
-                    if (error != SttError.None)
-                    {
-                        Log.Error(LogTag, "GetRecordingVolume Failed with error " + error);
-                        return 0.0f;
-                    }
+                    Log.Error(LogTag, "GetRecordingVolume Failed with error " + error);
+                    return 0.0f;
                 }
 
                 return volume;
@@ -696,14 +693,11 @@ namespace Tizen.Uix.Stt
             get
             {
                 State state;
-                lock (thisLock)
+                SttError error = SttGetState(_handle, out state);
+                if (error != SttError.None)
                 {
-                    SttError error = SttGetState(_handle, out state);
-                    if (error != SttError.None)
-                    {
-                        Log.Error(LogTag, "GetState Failed with error " + error);
-                        return State.Unavailable;
-                    }
+                    Log.Error(LogTag, "GetState Failed with error " + error);
+                    return State.Unavailable;
                 }
 
                 return state;
@@ -738,30 +732,23 @@ namespace Tizen.Uix.Stt
             get
             {
                 string engineId;
-                lock (thisLock)
+                SttError error = SttGetEngine(_handle, out engineId);
+                if (error != SttError.None)
                 {
-                    SttError error = SttGetEngine(_handle, out engineId);
-                    if (error != SttError.None)
-                    {
-                        Log.Error(LogTag, "Get EngineId Failed with error " + error);
-                        return "";
-                    }
+                    Log.Error(LogTag, "Get EngineId Failed with error " + error);
+                    return "";
                 }
 
                 return engineId;
             }
             set
             {
-                lock (thisLock)
+                SttError error = SttSetEngine(_handle, value);
+                if (error != SttError.None)
                 {
-                    SttError error = SttSetEngine(_handle, value);
-                    if (error != SttError.None)
-                    {
-                        Log.Error(LogTag, "Set EngineId Failed with error " + error);
-                        throw ExceptionFactory.CreateException(error);
-                    }
+                    Log.Error(LogTag, "Set EngineId Failed with error " + error);
+                    throw ExceptionFactory.CreateException(error);
                 }
-
             }
         }
 
@@ -788,20 +775,18 @@ namespace Tizen.Uix.Stt
         public IEnumerable<ResultTime> GetDetailedResult()
         {
             List<ResultTime> list = new List<ResultTime>();
-            lock (thisLock)
+            _resultTimeDelegate = (IntPtr handle, int index, TimeEvent e, IntPtr text, IntPtr startTime, IntPtr endTime, IntPtr userData) =>
             {
-                _resultTimeDelegate = (IntPtr handle, int index, TimeEvent e, IntPtr text, IntPtr startTime, IntPtr endTime, IntPtr userData) =>
-                {
-                    _result = new ResultTime(index, e, Marshal.PtrToStringAnsi(text), (long)startTime, (long)endTime);
-                    list.Add(_result);
-                    return true;
-                };
-                SttError error = SttForeachDetailedResult(_handle, _resultTimeDelegate, IntPtr.Zero);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "GetDetailedResult Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                _result = new ResultTime(index, e, Marshal.PtrToStringAnsi(text), (long)startTime, (long)endTime);
+                list.Add(_result);
+                return true;
+            };
+
+            SttError error = SttForeachDetailedResult(_handle, _resultTimeDelegate, IntPtr.Zero);
+            if (error != SttError.None)
+            {
+                Log.Error(LogTag, "GetDetailedResult Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
             return list;
         }
@@ -833,14 +818,11 @@ namespace Tizen.Uix.Stt
         public string GetPrivateData(string key)
         {
             string data;
-            lock (thisLock)
+            SttError error = SttGetPrivateData(_handle, key, out data);
+            if (error != SttError.None)
             {
-                SttError error = SttGetPrivateData(_handle, key, out data);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "GetPrivateData Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "GetPrivateData Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
 
             return data;
@@ -872,14 +854,11 @@ namespace Tizen.Uix.Stt
         /// </pre>
         public void SetPrivateData(string key, string data)
         {
-            lock (thisLock)
+            SttError error = SttSetPrivateData(_handle, key, data);
+            if (error != SttError.None)
             {
-                SttError error = SttSetPrivateData(_handle, key, data);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "SetPrivateData Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "SetPrivateData Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -904,22 +883,20 @@ namespace Tizen.Uix.Stt
         public IEnumerable<SupportedEngine> GetSupportedEngines()
         {
             List<SupportedEngine> engineList = new List<SupportedEngine>();
-            lock (thisLock)
+            SupportedEngineCallback supportedEngineDelegate = (IntPtr handle, IntPtr engineId, IntPtr engineName, IntPtr userData) =>
             {
-                SupportedEngineCallback supportedEngineDelegate = (IntPtr handle, IntPtr engineId, IntPtr engineName, IntPtr userData) =>
-                {
-                    string id = Marshal.PtrToStringAnsi(engineId);
-                    string name = Marshal.PtrToStringAnsi(engineName);
-                    SupportedEngine engine = new SupportedEngine(id, name);
-                    engineList.Add(engine);
-                    return true;
-                };
-                SttError error = SttForeEachSupportedEngines(_handle, supportedEngineDelegate, IntPtr.Zero);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "Create Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                string id = Marshal.PtrToStringAnsi(engineId);
+                string name = Marshal.PtrToStringAnsi(engineName);
+                SupportedEngine engine = new SupportedEngine(id, name);
+                engineList.Add(engine);
+                return true;
+            };
+
+            SttError error = SttForeEachSupportedEngines(_handle, supportedEngineDelegate, IntPtr.Zero);
+            if (error != SttError.None)
+            {
+                Log.Error(LogTag, "Create Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
 
             return engineList;
@@ -953,14 +930,11 @@ namespace Tizen.Uix.Stt
         /// </pre>
         public void SetCredential(string credential)
         {
-            lock (thisLock)
+            SttError error = SttSetcredential(_handle, credential);
+            if (error != SttError.None)
             {
-                SttError error = SttSetcredential(_handle, credential);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "SetCredential Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "SetCredential Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -987,14 +961,11 @@ namespace Tizen.Uix.Stt
         /// </post>
         public void Prepare()
         {
-            lock (thisLock)
+            SttError error = SttPrepare(_handle);
+            if (error != SttError.None)
             {
-                SttError error = SttPrepare(_handle);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "SetEngine Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "SetEngine Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1020,14 +991,11 @@ namespace Tizen.Uix.Stt
         /// </post>
         public void Unprepare()
         {
-            lock (thisLock)
+            SttError error = SttUnprepare(_handle);
+            if (error != SttError.None)
             {
-                SttError error = SttUnprepare(_handle);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "Unprepare Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "Unprepare Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1057,21 +1025,18 @@ namespace Tizen.Uix.Stt
         public IEnumerable<string> GetSupportedLanguages()
         {
             List<string> languageList = new List<string>();
-            lock (thisLock)
+            SupportedLanguageCallback supportedLanguageDelegate = (IntPtr handle, IntPtr language, IntPtr userData) =>
             {
-                SupportedLanguageCallback supportedLanguageDelegate = (IntPtr handle, IntPtr language, IntPtr userData) =>
-                {
-                    string lang = Marshal.PtrToStringAnsi(language);
-                    languageList.Add(lang);
-                    return true;
-                };
+                string lang = Marshal.PtrToStringAnsi(language);
+                languageList.Add(lang);
+                return true;
+            };
 
-                SttError error = SttForeachSupportedLanguages(_handle, supportedLanguageDelegate, IntPtr.Zero);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "GetSupportedLanguages Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+            SttError error = SttForeachSupportedLanguages(_handle, supportedLanguageDelegate, IntPtr.Zero);
+            if (error != SttError.None)
+            {
+                Log.Error(LogTag, "GetSupportedLanguages Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
 
             return languageList;
@@ -1107,48 +1072,45 @@ namespace Tizen.Uix.Stt
         public bool IsRecognitionTypeSupported(RecognitionType type)
         {
             bool supported;
-            lock (thisLock)
+            string recType = "stt.recognition.type.FREE";
+            switch (type)
             {
-                string recType = "stt.recognition.type.FREE";
-                switch (type)
-                {
-                    case RecognitionType.Free:
-                        {
-                            recType = "stt.recognition.type.FREE";
-                            break;
-                        }
+                case RecognitionType.Free:
+                    {
+                        recType = "stt.recognition.type.FREE";
+                        break;
+                    }
 
-                    case RecognitionType.Partial:
-                        {
-                            recType = "stt.recognition.type.FREE.PARTIAL";
-                            break;
-                        }
+                case RecognitionType.Partial:
+                    {
+                        recType = "stt.recognition.type.FREE.PARTIAL";
+                        break;
+                    }
 
-                    case RecognitionType.Search:
-                        {
-                            recType = "stt.recognition.type.SEARCH";
-                            break;
-                        }
+                case RecognitionType.Search:
+                    {
+                        recType = "stt.recognition.type.SEARCH";
+                        break;
+                    }
 
-                    case RecognitionType.WebSearch:
-                        {
-                            recType = "stt.recognition.type.WEB_SEARCH";
-                            break;
-                        }
+                case RecognitionType.WebSearch:
+                    {
+                        recType = "stt.recognition.type.WEB_SEARCH";
+                        break;
+                    }
 
-                    case RecognitionType.Map:
-                        {
-                            recType = "stt.recognition.type.MAP";
-                            break;
-                        }
-                }
+                case RecognitionType.Map:
+                    {
+                        recType = "stt.recognition.type.MAP";
+                        break;
+                    }
+            }
 
-                SttError error = SttIsRecognitionTypeSupported(_handle, recType, out supported);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "IsRecognitionTypeSupported Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+            SttError error = SttIsRecognitionTypeSupported(_handle, recType, out supported);
+            if (error != SttError.None)
+            {
+                Log.Error(LogTag, "IsRecognitionTypeSupported Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
 
             return supported;
@@ -1180,14 +1142,11 @@ namespace Tizen.Uix.Stt
         /// </pre>
         public void SetSilenceDetection(SilenceDetection type)
         {
-            lock (thisLock)
+            SttError error = SttSetSilenceDetection(_handle, type);
+            if (error != SttError.None)
             {
-                SttError error = SttSetSilenceDetection(_handle, type);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "SetSilenceDetection Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "SetSilenceDetection Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1219,14 +1178,11 @@ namespace Tizen.Uix.Stt
         /// </pre>
         public void SetStartSound(string filePath)
         {
-            lock (thisLock)
+            SttError error = SttSetStartSound(_handle, filePath);
+            if (error != SttError.None)
             {
-                SttError error = SttSetStartSound(_handle, filePath);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "SetStartSound Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "SetStartSound Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1253,14 +1209,11 @@ namespace Tizen.Uix.Stt
         /// </pre>
         public void UnsetStartSound()
         {
-            lock (thisLock)
+            SttError error = SttUnsetStartSound(_handle);
+            if (error != SttError.None)
             {
-                SttError error = SttUnsetStartSound(_handle);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "UnsetStartSound Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "UnsetStartSound Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1292,14 +1245,11 @@ namespace Tizen.Uix.Stt
         /// </pre>
         public void SetStopSound(string filePath)
         {
-            lock (thisLock)
+            SttError error = SttSetStopSound(_handle, filePath);
+            if (error != SttError.None)
             {
-                SttError error = SttSetStopSound(_handle, filePath);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "SetStopSound Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "SetStopSound Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1326,14 +1276,11 @@ namespace Tizen.Uix.Stt
         /// </pre>
         public void UnsetStopSound()
         {
-            lock (thisLock)
+            SttError error = SttUnsetStopSound(_handle);
+            if (error != SttError.None)
             {
-                SttError error = SttUnsetStopSound(_handle);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "UnsetStopSound Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "UnsetStopSound Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1376,48 +1323,45 @@ namespace Tizen.Uix.Stt
         /// </post>
         public void Start(string language, RecognitionType type)
         {
-            lock (thisLock)
+            string recType = "stt.recognition.type.FREE";
+            switch (type)
             {
-                string recType = "stt.recognition.type.FREE";
-                switch (type)
-                {
-                    case RecognitionType.Free:
-                        {
-                            recType = "stt.recognition.type.FREE";
-                            break;
-                        }
+                case RecognitionType.Free:
+                    {
+                        recType = "stt.recognition.type.FREE";
+                        break;
+                    }
 
-                    case RecognitionType.Partial:
-                        {
-                            recType = "stt.recognition.type.FREE.PARTIAL";
-                            break;
-                        }
+                case RecognitionType.Partial:
+                    {
+                        recType = "stt.recognition.type.FREE.PARTIAL";
+                        break;
+                    }
 
-                    case RecognitionType.Search:
-                        {
-                            recType = "stt.recognition.type.SEARCH";
-                            break;
-                        }
+                case RecognitionType.Search:
+                    {
+                        recType = "stt.recognition.type.SEARCH";
+                        break;
+                    }
 
-                    case RecognitionType.WebSearch:
-                        {
-                            recType = "stt.recognition.type.WEB_SEARCH";
-                            break;
-                        }
+                case RecognitionType.WebSearch:
+                    {
+                        recType = "stt.recognition.type.WEB_SEARCH";
+                        break;
+                    }
 
-                    case RecognitionType.Map:
-                        {
-                            recType = "stt.recognition.type.MAP";
-                            break;
-                        }
-                }
+                case RecognitionType.Map:
+                    {
+                        recType = "stt.recognition.type.MAP";
+                        break;
+                    }
+            }
 
-                SttError error = SttStart(_handle, language, recType);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "Start Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+            SttError error = SttStart(_handle, language, recType);
+            if (error != SttError.None)
+            {
+                Log.Error(LogTag, "Start Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1453,14 +1397,11 @@ namespace Tizen.Uix.Stt
         /// </post>
         public void Stop()
         {
-            lock (thisLock)
+            SttError error = SttStop(_handle);
+            if (error != SttError.None)
             {
-                SttError error = SttStop(_handle);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "Stop Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "Stop Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1497,14 +1438,11 @@ namespace Tizen.Uix.Stt
         /// </post>
         public void Cancel()
         {
-            lock (thisLock)
+            SttError error = SttCancel(_handle);
+            if (error != SttError.None)
             {
-                SttError error = SttCancel(_handle);
-                if (error != SttError.None)
-                {
-                    Log.Error(LogTag, "Cancel Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "Cancel Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1529,20 +1467,17 @@ namespace Tizen.Uix.Stt
         {
             if (!disposedValue)
             {
-                               lock (thisLock)
-                               {
-                                       if (_handle != IntPtr.Zero)
-                                       {
-                                               SttError error = SttDestroy(_handle);
-                                               if (error != SttError.None)
-                                               {
-                                                       Log.Error(LogTag, "Destroy Failed with error " + error);
-                                               }
-                                               _handle = IntPtr.Zero;
-                                       }
-                               }
-
-                               disposedValue = true;
+                if (_handle != IntPtr.Zero)
+                {
+                    SttError error = SttDestroy(_handle);
+                    if (error != SttError.None)
+                    {
+                        Log.Error(LogTag, "Destroy Failed with error " + error);
+                    }
+                    _handle = IntPtr.Zero;
+                }
+
+                disposedValue = true;
             }
         }
     }
index 7c9a2ad..02dc78a 100644 (file)
@@ -203,7 +203,12 @@ namespace Tizen.Uix.Tts
         private event EventHandler<DefaultVoiceChangedEventArgs> _defaultVoiceChanged;
         private event EventHandler<EngineChangedEventArgs> _engineChanged;
         private bool disposedValue = false;
-        private Object thisLock = new Object();
+        private readonly Object _stateChangedLock = new Object();
+        private readonly Object _utteranceStartedLock = new Object();
+        private readonly Object _utteranceCompletedLock = new Object();
+        private readonly Object _errorOccurredLock = new Object();
+        private readonly Object _defaultVoiceChangedLock = new Object();
+        private readonly Object _engineChangedLock = new Object();
         private TtsStateChangedCB _stateDelegate;
         private TtsUtteranceStartedCB _utteranceStartedResultDelegate;
         private TtsUtteranceCompletedCB _utteranceCompletedResultDelegate;
@@ -255,7 +260,7 @@ namespace Tizen.Uix.Tts
         {
             add
             {
-                lock (thisLock)
+                lock (_stateChangedLock)
                 {
                     if (_stateChanged == null)
                     {
@@ -278,7 +283,7 @@ namespace Tizen.Uix.Tts
 
             remove
             {
-                lock (thisLock)
+                lock (_stateChangedLock)
                 {
                     _stateChanged -= value;
                     if (_stateChanged == null)
@@ -302,7 +307,7 @@ namespace Tizen.Uix.Tts
         {
             add
             {
-                lock (thisLock)
+                lock (_utteranceStartedLock)
                 {
                     if (_utteranceStarted == null)
                     {
@@ -324,7 +329,7 @@ namespace Tizen.Uix.Tts
 
             remove
             {
-                lock (thisLock)
+                lock (_utteranceStartedLock)
                 {
                     _utteranceStarted -= value;
                     if (_utteranceStarted == null)
@@ -347,7 +352,7 @@ namespace Tizen.Uix.Tts
         {
             add
             {
-                lock (thisLock)
+                lock (_utteranceCompletedLock)
                 {
                     if (_utteranceCompleted == null)
                     {
@@ -369,7 +374,7 @@ namespace Tizen.Uix.Tts
 
             remove
             {
-                lock (thisLock)
+                lock (_utteranceCompletedLock)
                 {
                     _utteranceCompleted -= value;
                     if (_utteranceCompleted == null)
@@ -392,7 +397,7 @@ namespace Tizen.Uix.Tts
         {
             add
             {
-                lock (thisLock)
+                lock (_errorOccurredLock)
                 {
                     if (_errorOccurred == null)
                     {
@@ -414,7 +419,7 @@ namespace Tizen.Uix.Tts
 
             remove
             {
-                lock (thisLock)
+                lock (_errorOccurredLock)
                 {
                     _errorOccurred -= value;
                     if (_errorOccurred == null)
@@ -437,7 +442,7 @@ namespace Tizen.Uix.Tts
         {
             add
             {
-                lock (thisLock)
+                lock (_defaultVoiceChangedLock)
                 {
                     if (_defaultVoiceChanged == null)
                     {
@@ -457,16 +462,15 @@ namespace Tizen.Uix.Tts
                     }
                     _defaultVoiceChanged += value;
                 }
-
             }
 
             remove
             {
-                lock (thisLock)
+                lock (_defaultVoiceChangedLock)
                 {
                     _defaultVoiceChanged -= value;
                     if (_defaultVoiceChanged == null)
-                                       {
+                    {
                         TtsError error = TtsUnsetDefaultVoiceChangedCB(_handle);
                         if (error != TtsError.None)
                         {
@@ -485,7 +489,7 @@ namespace Tizen.Uix.Tts
         {
             add
             {
-                lock (thisLock)
+                lock (_engineChangedLock)
                 {
                     if (_engineChanged == null)
                     {
@@ -508,7 +512,7 @@ namespace Tizen.Uix.Tts
 
             remove
             {
-                lock (thisLock)
+                lock (_engineChangedLock)
                 {
                     _engineChanged -= value;
                     if (_engineChanged == null)
@@ -537,19 +541,16 @@ namespace Tizen.Uix.Tts
         {
             get
             {
-                lock (thisLock)
+                string language;
+                int voiceType;
+                TtsError error = TtsGetDefaultVoice(_handle, out language, out voiceType);
+                if (error != TtsError.None)
                 {
-                    string language;
-                    int voiceType;
-                    TtsError error = TtsGetDefaultVoice(_handle, out language, out voiceType);
-                    if (error != TtsError.None)
-                    {
-                        Log.Error(LogTag, "DefaultVoice Failed with error " + error);
-                        return new SupportedVoice();
-                    }
-
-                    return new SupportedVoice(language, voiceType);
+                    Log.Error(LogTag, "DefaultVoice Failed with error " + error);
+                    return new SupportedVoice();
                 }
+
+                return new SupportedVoice(language, voiceType);
             }
         }
 
@@ -571,14 +572,11 @@ namespace Tizen.Uix.Tts
             get
             {
                 uint maxTextSize;
-                lock (thisLock)
+                TtsError error = TtsGetMaxTextSize(_handle, out maxTextSize);
+                if (error != TtsError.None)
                 {
-                    TtsError error = TtsGetMaxTextSize(_handle, out maxTextSize);
-                    if (error != TtsError.None)
-                    {
-                        Log.Error(LogTag, "MaxTextSize Failed with error " + error);
-                        return 0;
-                    }
+                    Log.Error(LogTag, "MaxTextSize Failed with error " + error);
+                    return 0;
                 }
 
                 return maxTextSize;
@@ -601,14 +599,11 @@ namespace Tizen.Uix.Tts
             get
             {
                 State state;
-                lock (thisLock)
+                TtsError error = TtsGetState(_handle, out state);
+                if (error != TtsError.None)
                 {
-                    TtsError error = TtsGetState(_handle, out state);
-                    if (error != TtsError.None)
-                    {
-                        Log.Error(LogTag, "CurrentState Failed with error " + error);
-                        return State.Unavailable;
-                    }
+                    Log.Error(LogTag, "CurrentState Failed with error " + error);
+                    return State.Unavailable;
                 }
 
                 return state;
@@ -641,14 +636,11 @@ namespace Tizen.Uix.Tts
             get
             {
                 Mode mode = Mode.Default;
-                lock (thisLock)
+                TtsError error = TtsGetMode(_handle, out mode);
+                if (error != TtsError.None)
                 {
-                    TtsError error = TtsGetMode(_handle, out mode);
-                    if (error != TtsError.None)
-                    {
-                        Log.Error(LogTag, "Get Mode Failed with error " + error);
-                        return Mode.Default;
-                    }
+                    Log.Error(LogTag, "Get Mode Failed with error " + error);
+                    return Mode.Default;
                 }
 
                 return mode;
@@ -656,10 +648,7 @@ namespace Tizen.Uix.Tts
             set
             {
                 TtsError error;
-                lock (thisLock)
-                {
-                    error = TtsSetMode(_handle, value);
-                }
+                error = TtsSetMode(_handle, value);
 
                 if (error != TtsError.None)
                 {
@@ -687,14 +676,11 @@ namespace Tizen.Uix.Tts
         /// </pre>
         public void SetCredential(string credential)
         {
-            lock (thisLock)
+            TtsError error = TtsSetCredential(_handle, credential);
+            if (error != TtsError.None)
             {
-                TtsError error = TtsSetCredential(_handle, credential);
-                if (error != TtsError.None)
-                {
-                    Tizen.Log.Error(LogTag, "SetCredential Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Tizen.Log.Error(LogTag, "SetCredential Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -716,14 +702,11 @@ namespace Tizen.Uix.Tts
         /// </post>
         public void Prepare()
         {
-            lock (thisLock)
+            TtsError error = TtsPrepare(_handle);
+            if (error != TtsError.None)
             {
-                TtsError error = TtsPrepare(_handle);
-                if (error != TtsError.None)
-                {
-                    Log.Error(LogTag, "Prepare Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "Prepare Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -744,14 +727,11 @@ namespace Tizen.Uix.Tts
         /// </post>
         public void Unprepare()
         {
-            lock (thisLock)
+            TtsError error = TtsUnprepare(_handle);
+            if (error != TtsError.None)
             {
-                TtsError error = TtsUnprepare(_handle);
-                if (error != TtsError.None)
-                {
-                    Log.Error(LogTag, "Unprepare Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "Unprepare Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -774,21 +754,19 @@ namespace Tizen.Uix.Tts
         public IEnumerable<SupportedVoice> GetSupportedVoices()
         {
             List<SupportedVoice> voicesList = new List<SupportedVoice>();
-            lock (thisLock)
+
+            _supportedvoiceDelegate = (IntPtr handle, IntPtr language, int voiceType, IntPtr userData) =>
             {
-               _supportedvoiceDelegate = (IntPtr handle, IntPtr language, int voiceType, IntPtr userData) =>
-                {
-                    string lang = Marshal.PtrToStringAnsi(language);
-                    SupportedVoice voice = new SupportedVoice(lang, voiceType);
-                    voicesList.Add(voice);
-                    return true;
-                };
-                TtsError error = TtsForeachSupportedVoices(_handle, _supportedvoiceDelegate, IntPtr.Zero);
-                if (error != TtsError.None)
-                {
-                    Log.Error(LogTag, "GetSupportedVoices Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                string lang = Marshal.PtrToStringAnsi(language);
+                SupportedVoice voice = new SupportedVoice(lang, voiceType);
+                voicesList.Add(voice);
+                return true;
+            };
+            TtsError error = TtsForeachSupportedVoices(_handle, _supportedvoiceDelegate, IntPtr.Zero);
+            if (error != TtsError.None)
+            {
+                Log.Error(LogTag, "GetSupportedVoices Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
 
             return voicesList;
@@ -820,14 +798,11 @@ namespace Tizen.Uix.Tts
         public string GetPrivateData(string key)
         {
             string data;
-            lock (thisLock)
+            TtsError error = TtsGetPrivateData(_handle, key, out data);
+            if (error != TtsError.None)
             {
-                TtsError error = TtsGetPrivateData(_handle, key, out data);
-                if (error != TtsError.None)
-                {
-                    Log.Error(LogTag, "GetPrivateData Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "GetPrivateData Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
 
             return data;
@@ -859,14 +834,11 @@ namespace Tizen.Uix.Tts
         /// </pre>
         public void SetPrivateData(string key, string data)
         {
-            lock (thisLock)
+            TtsError error = TtsSetPrivateData(_handle, key, data);
+            if (error != TtsError.None)
             {
-                TtsError error = TtsSetPrivateData(_handle, key, data);
-                if (error != TtsError.None)
-                {
-                    Log.Error(LogTag, "SetPrivateData Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "SetPrivateData Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -892,14 +864,11 @@ namespace Tizen.Uix.Tts
         public SpeedRange GetSpeedRange()
         {
             int min = 0, max = 0, normal = 0;
-            lock (thisLock)
+            TtsError error = TtsGetSpeedRange(_handle, out min, out normal, out max);
+            if (error != TtsError.None)
             {
-                TtsError error = TtsGetSpeedRange(_handle, out min, out normal, out max);
-                if (error != TtsError.None)
-                {
-                    Log.Error(LogTag, "GetSpeedRange Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "GetSpeedRange Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
 
             return new SpeedRange(min, normal, max);
@@ -945,14 +914,11 @@ namespace Tizen.Uix.Tts
         public int AddText(string text, string language, int voiceType, int speed)
         {
             int id;
-            lock (thisLock)
+            TtsError error = TtsAddText(_handle, text, language, voiceType, speed, out id);
+            if (error != TtsError.None)
             {
-                TtsError error = TtsAddText(_handle, text, language, voiceType, speed, out id);
-                if (error != TtsError.None)
-                {
-                    Log.Error(LogTag, "AddText Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "AddText Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
 
             return id;
@@ -981,14 +947,11 @@ namespace Tizen.Uix.Tts
         /// </post>
         public void Play()
         {
-            lock (thisLock)
+            TtsError error = TtsPlay(_handle);
+            if (error != TtsError.None)
             {
-                TtsError error = TtsPlay(_handle);
-                if (error != TtsError.None)
-                {
-                    Log.Error(LogTag, "Play Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "Play Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1014,14 +977,11 @@ namespace Tizen.Uix.Tts
         /// </post>
         public void Stop()
         {
-            lock (thisLock)
+            TtsError error = TtsStop(_handle);
+            if (error != TtsError.None)
             {
-                TtsError error = TtsStop(_handle);
-                if (error != TtsError.None)
-                {
-                    Log.Error(LogTag, "Stop Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "Stop Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1046,14 +1006,11 @@ namespace Tizen.Uix.Tts
         /// </post>
         public void Pause()
         {
-            lock (thisLock)
+            TtsError error = TtsPause(_handle);
+            if (error != TtsError.None)
             {
-                TtsError error = TtsPause(_handle);
-                if (error != TtsError.None)
-                {
-                    Log.Error(LogTag, "Pause Failed with error " + error);
-                    throw ExceptionFactory.CreateException(error);
-                }
+                Log.Error(LogTag, "Pause Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
             }
         }
 
@@ -1077,21 +1034,18 @@ namespace Tizen.Uix.Tts
         protected virtual void Dispose(bool disposing)
         {
             if (!disposedValue)
-                       {
-                               lock (thisLock)
-                               {
-                                       if (_handle != IntPtr.Zero)
-                                       {
-                                               TtsError error = TtsDestroy(_handle);
-                                               if (error != TtsError.None)
-                                               {
-                                                       Log.Error(LogTag, "Destroy Failed with error " + error);
-                                               }
-                                               _handle = IntPtr.Zero;
-                                       }
-                               }
-
-                               disposedValue = true;
+            {
+                if (_handle != IntPtr.Zero)
+                {
+                    TtsError error = TtsDestroy(_handle);
+                    if (error != TtsError.None)
+                    {
+                        Log.Error(LogTag, "Destroy Failed with error " + error);
+                    }
+                    _handle = IntPtr.Zero;
+                }
+
+                disposedValue = true;
             }
         }
     }