Modify DataControl manager for thread-safe
[platform/framework/native/appfw.git] / src / app / FApp_DataControlManager.cpp
index 02a33d8..25d3bd9 100644 (file)
@@ -30,6 +30,7 @@
 #include <FBaseInteger.h>
 #include <FBaseSysLog.h>
 #include <FBaseColIEnumerator.h>
+#include <FBaseRtMutexGuard.h>
 
 #include <FBase_StringConverter.h>
 #include <FAppPkg_PackageManagerImpl.h>
@@ -38,6 +39,7 @@
 using namespace std;
 using namespace Tizen::Base;
 using namespace Tizen::Base::Collection;
+using namespace Tizen::Base::Runtime;
 using namespace Tizen::App::Package;
 
 namespace Tizen { namespace App
@@ -50,12 +52,27 @@ _DataControlManager::_DataControlManager(void)
        , __pProviderList(null)
        , __uniqueId(-1)
 {
-       __pDataControlRequestList = new (std::nothrow) HashMap(SingleObjectDeleter);
-       SysTryReturnVoidResult(NID_APP, __pDataControlRequestList != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
-       __pDataControlRequestList->Construct();
+}
+
+result
+_DataControlManager::Construct(void)
+{
+       unique_ptr< HashMap > pDataControlRequestList(new (std::nothrow) HashMap(SingleObjectDeleter));
+       SysTryReturnResult(NID_APP, pDataControlRequestList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
+
+       result r = pDataControlRequestList->Construct();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "Propagating to caller...");
 
-       __pProviderList = new (std::nothrow) LinkedList(SingleObjectDeleter);
-       SysTryReturnVoidResult(NID_APP, __pProviderList != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
+       unique_ptr< LinkedList > pProviderList(new (std::nothrow) LinkedList(SingleObjectDeleter));
+       SysTryReturnResult(NID_APP, pProviderList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
+
+       r = __mutex.Create();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "Mutex initialization failed.");
+
+       __pDataControlRequestList = pDataControlRequestList.release();
+       __pProviderList = pProviderList.release();
+
+       return E_SUCCESS;
 }
 
 _DataControlManager::~_DataControlManager(void)
@@ -70,10 +87,16 @@ _DataControlManager::InitSingleton(void)
        _DataControlManager* pInst = new (std::nothrow) _DataControlManager();
        SysTryReturnVoidResult(NID_APP, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
 
+       result r = pInst->Construct();
+       SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
+
        __pDataControlManagerInstance = pInst;
-               
+
        std::atexit(DestroySingleton);
        return;
+
+CATCH:
+       delete pInst;
 }
 
 void
@@ -105,6 +128,8 @@ _DataControlManager::GetInstance(void)
 int
 _DataControlManager::GetRequestCount(void) const
 {
+       MutexGuard lock(__mutex);
+
        return __pDataControlRequestList->GetCount();
 }
 
@@ -112,6 +137,8 @@ result
 _DataControlManager::AddRequestInfo(Integer* pReqId, _DataControlRequestInfo* pReqInfo)
 {
        //SysLog(NID_APP, "DataControl request list count: %d", __pDataControlRequestList->GetCount());
+       MutexGuard lock(__mutex);
+
        return __pDataControlRequestList->Add(pReqId, pReqInfo);
 }
 
@@ -120,6 +147,8 @@ _DataControlManager::GetRequestInfo(Integer& reqId)
 {
        result r = E_SUCCESS;
 
+       MutexGuard lock(__mutex);
+
        Object* pObj = __pDataControlRequestList->GetValue(reqId);
        SysTryReturn(NID_APP, pObj != null, null, GetLastResult(), "[%s] Propagating to caller...", GetErrorMessage(GetLastResult()));
 
@@ -132,12 +161,16 @@ _DataControlManager::GetRequestInfo(Integer& reqId)
 void
 _DataControlManager::RemoveRequestInfo(Integer& reqId)
 {
+       MutexGuard lock(__mutex);
+
        __pDataControlRequestList->Remove(reqId);
 }
 
 void
 _DataControlManager::RemoveAllRequests(void)
 {
+       MutexGuard lock(__mutex);
+
        __pDataControlRequestList->RemoveAll();
 }