2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 * @file FScl_AccountDbConnector.cpp
18 * @brief This is the implementation for _AccountDbConnector class.
20 * This file contains definitions of @e _AccountDbConnector class.
23 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include "FScl_AccountDbConnector.h"
28 namespace Tizen { namespace Social
31 __thread bool isAccountDbConnected = false;
32 pthread_key_t accountTlsKey = 0;
33 _AccountDbConnector* _AccountDbConnector::__pInstance = null;
35 _AccountDbConnector::_AccountDbConnector(void)
39 _AccountDbConnector::~_AccountDbConnector(void)
44 _AccountDbConnector::OnThreadExit(void *pValue)
46 _AccountDbConnector* pInstance = static_cast<_AccountDbConnector*>(pValue);
48 if (pInstance != null)
50 pInstance->Disconnect();
53 pthread_setspecific(accountTlsKey, null);
57 _AccountDbConnector::Connect(void)
59 int ret = account_connect();
60 SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_SYSTEM, "The method cannot proceed due to a severe system error.");
62 pthread_setspecific(accountTlsKey, this);
68 _AccountDbConnector::Disconnect(void)
70 int ret = account_disconnect();
71 SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_SYSTEM, "The method cannot proceed due to a severe system error.");
77 _AccountDbConnector::EnsureDbConnection(void)
79 if (!isAccountDbConnected)
81 _AccountDbConnector* pInstance = GetInstance();
82 SysTryReturn(NID_SCL, pInstance != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
84 result r = pInstance->Connect();
85 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
87 isAccountDbConnected = true;
94 _AccountDbConnector::InitAccountDbConnector(void)
96 std::unique_ptr<_AccountDbConnector> pInstance(new (std::nothrow) _AccountDbConnector());
97 SysTryReturnVoidResult(NID_SCL, pInstance, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
99 __pInstance = pInstance.release();
101 pthread_key_create(&accountTlsKey, OnThreadExit);
103 std::atexit(DestroyAccountDbConnector);
107 _AccountDbConnector::DestroyAccountDbConnector(void)
114 _AccountDbConnector::GetInstance(void)
116 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
118 if (__pInstance == null)
122 pthread_once(&onceBlock, InitAccountDbConnector);
124 result r = GetLastResult();
128 onceBlock = PTHREAD_ONCE_INIT;