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.
18 /* standard library header */
22 /* SLP library header */
26 #include "DispatcherHelper.h"
28 namespace smartcard_service_api
30 DispatcherHelper::DispatcherHelper()
35 DispatcherHelper::~DispatcherHelper()
37 stopDispatcherThread();
40 DispatcherMsg *DispatcherHelper::fetchMessage()
42 DispatcherMsg *result = NULL;
44 if (messageQ.size() > 0)
46 result = messageQ.front();
53 void DispatcherHelper::clearQueue()
55 DispatcherMsg *temp = NULL;
57 while (messageQ.size() > 0)
59 temp = fetchMessage();
64 void DispatcherHelper::pushMessage(const DispatcherMsg &msg)
66 DispatcherMsg *pushMsg = new DispatcherMsg(msg);
70 messageQ.push(pushMsg);
76 void *DispatcherHelper::_dispatcherThreadFunc(void *data)
79 DispatcherMsg *msg = NULL;
80 DispatcherHelper *helper = (DispatcherHelper *)data;
85 if ((msg = helper->fetchMessage()) == NULL)
87 result = helper->waitTimedCondition(0);
94 helper->dispatcherThreadFunc(msg, data);
102 void DispatcherHelper::processMessage(DispatcherMsg *msg)
104 dispatcherThreadFunc(msg, this);
107 bool DispatcherHelper::runDispatcherThread()
112 if (dispatcherThread == 0)
116 pthread_attr_init(&attr);
117 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
119 if ((ret = pthread_create(&dispatcherThread, &attr, &DispatcherHelper::_dispatcherThreadFunc, this)) != 0)
121 _ERR("pthread_create failed [%d]", ret);
125 _DBG("pthread_create success");
131 _DBG("thread already start");
138 void DispatcherHelper::stopDispatcherThread()
140 if (dispatcherThread != 0)
142 pthread_cancel(dispatcherThread);
143 dispatcherThread = 0;
147 } /* namespace smartcard_service_api */