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