2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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)
73 void *DispatcherHelper::_dispatcherThreadFunc(void *data)
75 DispatcherMsg *msg = NULL;
76 DispatcherHelper *helper = (DispatcherHelper *)data;
81 if ((msg = helper->fetchMessage()) == NULL)
83 helper->waitTimedCondition(0);
90 helper->dispatcherThreadFunc(msg, data);
98 bool DispatcherHelper::runDispatcherThread()
103 if (dispatcherThread == 0)
107 pthread_attr_init(&attr);
108 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
110 if ((ret = pthread_create(&dispatcherThread, &attr, &DispatcherHelper::_dispatcherThreadFunc, this)) != 0)
112 SCARD_DEBUG_ERR("pthread_create failed [%d]", ret);
116 SCARD_DEBUG("pthread_create success");
122 SCARD_DEBUG("thread already start");
129 void DispatcherHelper::stopDispatcherThread()
131 if (dispatcherThread != 0)
133 pthread_cancel(dispatcherThread);
134 dispatcherThread = 0;
138 } /* namespace smartcard_service_api */