Refactor AppRegistry to support multi-thread concurrency
authorHyunbin Lee <hyunbin.lee@samsung.com>
Tue, 27 Aug 2013 06:27:45 +0000 (15:27 +0900)
committerHyunbin Lee <hyunbin.lee@samsung.com>
Tue, 27 Aug 2013 06:28:24 +0000 (15:28 +0900)
Change-Id: I3e9c56a70e640c23213bb347158b4a64b059b68a
Signed-off-by: Hyunbin Lee <hyunbin.lee@samsung.com>
src/app/FApp_AppRegistryImpl.cpp
src/app/FApp_AppRegistryImpl.h
src/io/FIo_SecureIoUtil.cpp

index 1d17ea0..11c2a89 100644 (file)
 
 using namespace Tizen::Io;
 using namespace Tizen::Base;
+using namespace Tizen::Base::Runtime;
 
 namespace Tizen { namespace App
 {
 
 _AppRegistryImpl::_AppRegistryImpl(void)
-       : __pRegistry(null)
-       , __sectionName(L"__ApplicationStates")
+       : __sectionName(L"__ApplicationStates")
 {
 }
 
@@ -54,6 +54,8 @@ _AppRegistryImpl::Construct(void)
        result r = __regPath.Append(packageId);
        SysTryReturnResult(NID_APP, !IsFailed(r), r, "String appending has failed.");
 
+       __mutex.Create();
+
        SysLog(NID_APP, "Exit.");
        return r;
 }
@@ -61,78 +63,90 @@ _AppRegistryImpl::Construct(void)
 result
 _AppRegistryImpl::Add(const String& key, const String& value)
 {
-       result r = Load();
-       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
+       Registry* pReg = LoadN();
+       SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
-       r = __pRegistry->AddValue(__sectionName, key, value);
+       __mutex.Acquire();
+       result r = pReg->AddValue(__sectionName, key, value);
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
+       delete pReg;
+       __mutex.Release();
 
-       Unload();
        return r;
 }
 
 result
 _AppRegistryImpl::Add(const String& key, int value)
 {
-       result r = Load();
-       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
+       Registry* pReg = LoadN();
+       SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
-       r = __pRegistry->AddValue(__sectionName, key, value);
+       __mutex.Acquire();
+       result r = pReg->AddValue(__sectionName, key, value);
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
+       delete pReg;
+       __mutex.Release();
 
-       Unload();
        return r;
 }
 
 result
 _AppRegistryImpl::Add(const String& key, double value)
 {
-       result r = Load();
-       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
+       Registry* pReg = LoadN();
+       SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
-       r = __pRegistry->AddValue(__sectionName, key, value);
+       __mutex.Acquire();
+       result r = pReg->AddValue(__sectionName, key, value);
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
+       delete pReg;
+       __mutex.Release();
 
-       Unload();
        return r;
 }
 
 result
 _AppRegistryImpl::Set(const String& key, const String& value)
 {
-       result r = Load();
-       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
+       Registry* pReg = LoadN();
+       SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
-       r = __pRegistry->SetValue(__sectionName, key, value);
+       __mutex.Acquire();
+       result r = pReg->SetValue(__sectionName, key, value);
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
+       delete pReg;
+       __mutex.Release();
 
-       Unload();
        return r;
 }
 
 result
 _AppRegistryImpl::Set(const String& key, int value)
 {
-       result r = Load();
-       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
+       Registry* pReg = LoadN();
+       SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
-       r = __pRegistry->SetValue(__sectionName, key, value);
+       __mutex.Acquire();
+       result r = pReg->SetValue(__sectionName, key, value);
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
+       delete pReg;
+       __mutex.Release();
 
-       Unload();
        return r;
 }
 
 result
 _AppRegistryImpl::Set(const String& key, double value)
 {
-       result r = Load();
-       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
+       Registry* pReg = LoadN();
+       SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
-       r = __pRegistry->SetValue(__sectionName, key, value);
+       __mutex.Acquire();
+       result r = pReg->SetValue(__sectionName, key, value);
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
+       delete pReg;
+       __mutex.Release();
 
-       Unload();
        return r;
 }
 
@@ -145,78 +159,84 @@ _AppRegistryImpl::Save(void)
 result
 _AppRegistryImpl::Remove(const String& key)
 {
-       result r = Load();
-       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
+       Registry* pReg = LoadN();
+       SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
-       r = __pRegistry->RemoveValue(__sectionName, key);
+       __mutex.Acquire();
+       result r = pReg->RemoveValue(__sectionName, key);
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
+       delete pReg;
+       __mutex.Release();
 
-       Unload();
        return r;
 }
 
 result
 _AppRegistryImpl::Get(const String& key, String& value) const
 {
-       result r = Load();
-       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
+       Registry* pReg = LoadN();
+       SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
-       r = __pRegistry->GetValue(__sectionName, key, value);
+       __mutex.Acquire();
+       result r = pReg->GetValue(__sectionName, key, value);
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
+       delete pReg;
+       __mutex.Release();
 
-       Unload();
        return r;
 }
 
 result
 _AppRegistryImpl::Get(const String& key, int& value) const
 {
-       result r = Load();
-       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
+       Registry* pReg = LoadN();
+       SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
-       r = __pRegistry->GetValue(__sectionName, key, value);
+       __mutex.Acquire();
+       result r = pReg->GetValue(__sectionName, key, value);
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
+       delete pReg;
+       __mutex.Release();
 
-       Unload();
        return r;
 }
 
 result
 _AppRegistryImpl::Get(const String& key, double& value) const
 {
-       result r = Load();
-       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
+       Registry* pReg = LoadN();
+       SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
 
-       r = __pRegistry->GetValue(__sectionName, key, value);
+       __mutex.Acquire();
+       result r = pReg->GetValue(__sectionName, key, value);
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
+       delete pReg;
+       __mutex.Release();
 
-       Unload();
        return r;
 }
 
-result
-_AppRegistryImpl::Load(void) const
+Registry*
+_AppRegistryImpl::LoadN(void) const
 {
-       __pRegistry = new (std::nothrow) Registry();
-       SysTryReturnResult(NID_APP, __pRegistry != null, E_OUT_OF_MEMORY, "Instantiating registry has failed.");
+       Registry* pReg = new (std::nothrow) Registry();
+       SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
 
-       result r = __pRegistry->Construct(__regPath, "a+");
-       SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed.", __regPath.GetPointer());
+       __mutex.Acquire();
+       result r = pReg->Construct(__regPath, "a+");
+       SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
+                       __regPath.GetPointer(), GetErrorMessage(r));
 
-       r = __pRegistry->AddSection(__sectionName);
+       r = pReg->AddSection(__sectionName);
        if (r == E_SECTION_ALREADY_EXIST)
        {
                r = E_SUCCESS;
        }
-       SysAssertf(!IsFailed(r), "Adding section to registry has failed.");
+       __mutex.Release();
+       SysAssertf(!IsFailed(r), "Adding section to registry has failed. %s occurred.", GetErrorMessage(r));
 
-       return E_SUCCESS;
-}
-
-void
-_AppRegistryImpl::Unload(void) const
-{
-       delete __pRegistry;
+       SetLastResult(r);
+       return pReg;
 }
 
 }} // Tizen::App
index 7c22cfb..ba17431 100644 (file)
@@ -23,7 +23,7 @@
 #define _FAPP_INTERNAL_APP_REGISTRY_IMPL_H_
 
 #include <FBaseObject.h>
-#include <FOspConfig.h>
+#include <FBaseRtMutex.h>
 #include <FAppTypes.h>
 
 namespace Tizen { namespace Io { class Registry; } }
@@ -230,22 +230,15 @@ private:
         * Loads the registry file.
         *
         * @since               2.2
-        * @return              An error code
+        * @return              The registry instance
         * @exception   E_SUCCESS       The method is successful.
         */
-       result Load(void) const;
-
-       /*
-        * Unloads the registry file.
-        *
-        * @since               2.2
-        */
-       void Unload(void) const;
+       Tizen::Io::Registry* LoadN(void) const;
 
 private:
-       mutable Tizen::Io::Registry* __pRegistry;
        const Tizen::Base::String __sectionName;
        Tizen::Base::String __regPath;
+       mutable Tizen::Base::Runtime::Mutex __mutex;
 
        friend class AppRegistry;
 }; // _AppRegistryImpl
index aff9321..5443f80 100644 (file)
@@ -1060,7 +1060,7 @@ _SecureIoUtil::CheckSecureFileHeader(const Tizen::Base::String& filePath, const
        ret = normalFile.Read(secureFileHeader, SECURE_FILE_HEADER_SIZE_V1);
        if (ret < SECURE_FILE_HEADER_SIZE_V1)
        {
-               r  = GetLastResult();
+               r = GetLastResult();
                if (IsEndOfFile(&normalFile))
                {
                        if (ret && pSecretKey)