2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* standard library header */
21 /* SLP library header */
25 #include "DispatcherHelper.h"
27 namespace smartcard_service_api
29 DispatcherHelper::DispatcherHelper()
34 DispatcherHelper::~DispatcherHelper()
36 stopDispatcherThread();
39 DispatcherMsg *DispatcherHelper::fetchMessage()
41 DispatcherMsg *result = NULL;
43 if (messageQ.size() > 0)
45 result = messageQ.front();
52 void DispatcherHelper::clearQueue()
54 DispatcherMsg *temp = NULL;
56 while (messageQ.size() > 0)
58 temp = fetchMessage();
63 void DispatcherHelper::pushMessage(DispatcherMsg *msg)
65 DispatcherMsg *pushMsg = new DispatcherMsg(msg);
69 messageQ.push(pushMsg);
75 void *DispatcherHelper::_dispatcherThreadFunc(void *data)
78 DispatcherMsg *msg = NULL;
79 DispatcherHelper *helper = (DispatcherHelper *)data;
84 if ((msg = helper->fetchMessage()) == NULL)
86 result = helper->waitTimedCondition(0);
93 helper->dispatcherThreadFunc(msg, data);
101 void DispatcherHelper::processMessage(DispatcherMsg *msg)
103 dispatcherThreadFunc(msg, this);
106 bool DispatcherHelper::runDispatcherThread()
111 if (dispatcherThread == 0)
115 pthread_attr_init(&attr);
116 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
118 if ((ret = pthread_create(&dispatcherThread, &attr, &DispatcherHelper::_dispatcherThreadFunc, this)) != 0)
120 SCARD_DEBUG_ERR("pthread_create failed [%d]", ret);
124 SCARD_DEBUG("pthread_create success");
130 SCARD_DEBUG("thread already start");
137 void DispatcherHelper::stopDispatcherThread()
139 if (dispatcherThread != 0)
141 pthread_cancel(dispatcherThread);
142 dispatcherThread = 0;
146 } /* namespace smartcard_service_api */