update the latest source
[platform/core/connectivity/smartcard-service.git] / common / DispatcherHelper.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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                 syncLock();
66
67                 messageQ.push(msg);
68
69                 signalCondition();
70                 syncUnlock();
71         }
72
73         void *DispatcherHelper::_dispatcherThreadFunc(void *data)
74         {
75                 DispatcherMsg *msg = NULL;
76                 DispatcherHelper *helper = (DispatcherHelper *)data;
77
78                 while (1)
79                 {
80                         helper->syncLock();
81                         if ((msg = helper->fetchMessage()) == NULL)
82                         {
83                                 helper->waitTimedCondition(0);
84                                 helper->syncUnlock();
85                                 continue;
86                         }
87                         helper->syncUnlock();
88
89                         /* process message */
90                         helper->dispatcherThreadFunc(msg, data);
91
92                         delete msg;
93                 }
94
95                 return (void *)NULL;
96         }
97
98         bool DispatcherHelper::runDispatcherThread()
99         {
100                 bool result = false;
101                 pthread_attr_t attr;
102
103                 if (dispatcherThread == 0)
104                 {
105                         int ret;
106
107                         pthread_attr_init(&attr);
108                         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
109
110                         if ((ret = pthread_create(&dispatcherThread, &attr, &DispatcherHelper::_dispatcherThreadFunc, this)) != 0)
111                         {
112                                 SCARD_DEBUG_ERR("pthread_create failed [%d]", ret);
113                         }
114                         else
115                         {
116                                 SCARD_DEBUG("pthread_create success");
117                                 result = true;
118                         }
119                 }
120                 else
121                 {
122                         SCARD_DEBUG("thread already start");
123                         result = true;
124                 }
125
126                 return result;
127         }
128
129         void DispatcherHelper::stopDispatcherThread()
130         {
131                 if (dispatcherThread != 0)
132                 {
133                         pthread_cancel(dispatcherThread);
134                         dispatcherThread = 0;
135                 }
136         }
137
138 } /* namespace smartcard_service_api */