Refactor AppRegistry to support multi-thread concurrency
[platform/framework/native/appfw.git] / src / app / FApp_AppRegistryImpl.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