Fix the Prevent problems
[platform/core/connectivity/smartcard-service.git] / common / DispatcherHelper.cpp
1 /*
2  * Copyright (c) 2012, 2013 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 #ifndef USE_GDBUS
18 /* standard library header */
19 #include <stdio.h>
20 #include <string.h>
21
22 /* SLP library header */
23
24 /* local header */
25 #include "Debug.h"
26 #include "DispatcherHelper.h"
27
28 namespace smartcard_service_api
29 {
30         DispatcherHelper::DispatcherHelper()
31         {
32                 dispatcherThread = 0;
33         }
34
35         DispatcherHelper::~DispatcherHelper()
36         {
37                 stopDispatcherThread();
38         }
39
40         DispatcherMsg *DispatcherHelper::fetchMessage()
41         {
42                 DispatcherMsg *result = NULL;
43
44                 if (messageQ.size() > 0)
45                 {
46                         result = messageQ.front();
47                         messageQ.pop();
48                 }
49
50                 return result;
51         }
52
53         void DispatcherHelper::clearQueue()
54         {
55                 DispatcherMsg *temp = NULL;
56
57                 while (messageQ.size() > 0)
58                 {
59                         temp = fetchMessage();
60                         delete temp;
61                 }
62         }
63
64         void DispatcherHelper::pushMessage(const DispatcherMsg &msg)
65         {
66                 DispatcherMsg *pushMsg = new DispatcherMsg(msg);
67
68                 syncLock();
69
70                 messageQ.push(pushMsg);
71
72                 signalCondition();
73                 syncUnlock();
74         }
75
76         void *DispatcherHelper::_dispatcherThreadFunc(void *data)
77         {
78                 int result = 0;
79                 DispatcherMsg *msg = NULL;
80                 DispatcherHelper *helper = (DispatcherHelper *)data;
81
82                 while (1)
83                 {
84                         helper->syncLock();
85                         if ((msg = helper->fetchMessage()) == NULL)
86                         {
87                                 result = helper->waitTimedCondition(0);
88                                 helper->syncUnlock();
89                                 continue;
90                         }
91                         helper->syncUnlock();
92
93                         /* process message */
94                         helper->dispatcherThreadFunc(msg, data);
95
96                         delete msg;
97                 }
98
99                 return (void *)NULL;
100         }
101
102         void DispatcherHelper::processMessage(DispatcherMsg *msg)
103         {
104                 dispatcherThreadFunc(msg, this);
105         }
106
107         bool DispatcherHelper::runDispatcherThread()
108         {
109                 bool result = false;
110                 pthread_attr_t attr;
111
112                 if (dispatcherThread == 0)
113                 {
114                         int ret;
115
116                         pthread_attr_init(&attr);
117                         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
118
119                         if ((ret = pthread_create(&dispatcherThread, &attr, &DispatcherHelper::_dispatcherThreadFunc, this)) != 0)
120                         {
121                                 _ERR("pthread_create failed [%d]", ret);
122                         }
123                         else
124                         {
125                                 _DBG("pthread_create success");
126                                 result = true;
127                         }
128                 }
129                 else
130                 {
131                         _DBG("thread already start");
132                         result = true;
133                 }
134
135                 return result;
136         }
137
138         void DispatcherHelper::stopDispatcherThread()
139         {
140                 if (dispatcherThread != 0)
141                 {
142                         pthread_cancel(dispatcherThread);
143                         dispatcherThread = 0;
144                 }
145         }
146
147 } /* namespace smartcard_service_api */
148 #endif