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