2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FScl_AccountDbConnector.cpp
19 * @brief This is the implementation for _AccountDbConnector class.
21 * This file contains definitions of @e _AccountDbConnector class.
24 #include <unique_ptr.h>
26 #include <FBaseSysLog.h>
27 #include "FScl_AccountDbConnector.h"
29 namespace Tizen { namespace Social
32 __thread bool isAccountDbConnected = false;
33 pthread_key_t accountTlsKey = 0;
34 _AccountDbConnector* _AccountDbConnector::__pInstance = null;
36 _AccountDbConnector::_AccountDbConnector(void)
40 _AccountDbConnector::~_AccountDbConnector(void)
45 _AccountDbConnector::OnThreadExit(void *pValue)
47 _AccountDbConnector* pInstance = static_cast<_AccountDbConnector*>(pValue);
49 if (pInstance != null)
51 pInstance->Disconnect();
54 pthread_setspecific(accountTlsKey, null);
58 _AccountDbConnector::Connect(void)
60 int ret = account_connect();
61 SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_SYSTEM, "The method cannot proceed due to a severe system error.");
63 pthread_setspecific(accountTlsKey, this);
69 _AccountDbConnector::Disconnect(void)
71 int ret = account_disconnect();
72 SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_SYSTEM, "The method cannot proceed due to a severe system error.");
78 _AccountDbConnector::EnsureDbConnection(void)
80 if (!isAccountDbConnected)
82 _AccountDbConnector* pInstance = GetInstance();
83 SysTryReturn(NID_SCL, pInstance != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
85 result r = pInstance->Connect();
86 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
88 isAccountDbConnected = true;
95 _AccountDbConnector::InitAccountDbConnector(void)
97 std::unique_ptr<_AccountDbConnector> pInstance(new (std::nothrow) _AccountDbConnector());
98 SysTryReturnVoidResult(NID_SCL, pInstance, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
100 __pInstance = pInstance.release();
102 pthread_key_create(&accountTlsKey, OnThreadExit);
104 std::atexit(DestroyAccountDbConnector);
108 _AccountDbConnector::DestroyAccountDbConnector(void)
115 _AccountDbConnector::GetInstance(void)
117 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
119 if (__pInstance == null)
123 pthread_once(&onceBlock, InitAccountDbConnector);
125 result r = GetLastResult();
129 onceBlock = PTHREAD_ONCE_INIT;