Merge "Fixed exception handling codes" into tizen_2.1
[framework/osp/social.git] / src / FScl_AccountDbConnector.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 /**
18  * @file                FScl_AccountDbConnector.cpp
19  * @brief               This is the implementation for _AccountDbConnector class.
20  *
21  * This file contains definitions of @e _AccountDbConnector class.
22  */
23
24 #include <unique_ptr.h>
25 #include <pthread.h>
26 #include <FBaseSysLog.h>
27 #include "FScl_AccountDbConnector.h"
28
29 namespace Tizen { namespace Social
30 {
31
32 __thread bool isAccountDbConnected = false;
33 pthread_key_t accountTlsKey = 0;
34 _AccountDbConnector* _AccountDbConnector::__pInstance = null;
35
36 _AccountDbConnector::_AccountDbConnector(void)
37 {
38 }
39
40 _AccountDbConnector::~_AccountDbConnector(void)
41 {
42 }
43
44 void
45 _AccountDbConnector::OnThreadExit(void *pValue)
46 {
47         _AccountDbConnector* pInstance = static_cast<_AccountDbConnector*>(pValue);
48
49         if (pInstance != null)
50         {
51                 pInstance->Disconnect();
52         }
53
54         pthread_setspecific(accountTlsKey, null);
55 }
56
57 result
58 _AccountDbConnector::Connect(void)
59 {
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.");
62
63         pthread_setspecific(accountTlsKey, this);
64
65         return E_SUCCESS;
66 }
67
68 result
69 _AccountDbConnector::Disconnect(void)
70 {
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.");
73
74         return E_SUCCESS;
75 }
76
77 result
78 _AccountDbConnector::EnsureDbConnection(void)
79 {
80         if (!isAccountDbConnected)
81         {
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));
84
85                 result r = pInstance->Connect();
86                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
87
88                 isAccountDbConnected = true;
89         }
90
91         return E_SUCCESS;
92 }
93
94 void
95 _AccountDbConnector::InitAccountDbConnector(void)
96 {
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));
99
100         __pInstance = pInstance.release();
101
102         pthread_key_create(&accountTlsKey, OnThreadExit);
103
104         std::atexit(DestroyAccountDbConnector);
105 }
106
107 void
108 _AccountDbConnector::DestroyAccountDbConnector(void)
109 {
110         delete __pInstance;
111         __pInstance = null;
112 }
113
114 _AccountDbConnector*
115 _AccountDbConnector::GetInstance(void)
116 {
117         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
118
119         if (__pInstance == null)
120         {
121                 ClearLastResult();
122
123                 pthread_once(&onceBlock, InitAccountDbConnector);
124
125                 result r = GetLastResult();
126
127                 if (IsFailed(r))
128                 {
129                         onceBlock = PTHREAD_ONCE_INIT;
130                 }
131         }
132
133         return __pInstance;
134 }
135
136 }}  // Tizen::Social