This file will no longer implement only one service thread.
Changed the name not to be misleading.
Change-Id: Ibbf90daaf8a399108e5caabf4627ad8646156464
--- /dev/null
+/*
+ * Copyright (c) 2014-2022 Samsung Electronics Co., Ltd. All rights reserved
+ *
+ * This file is licensed under the terms of MIT License or the Apache License
+ * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
+ * See the LICENSE file or the notice below for Apache License Version 2.0
+ * details.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * @file service-thread-dispatcher.h
+ * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @author Tomasz Swierczek (t.swierczek@samsung.com)
+ * @version 1.0
+ * @brief Implementation of threads.
+ */
+
+#pragma once
+
+#include <cassert>
+#include <condition_variable>
+#include <mutex>
+#include <queue>
+#include <thread>
+
+#include <dpl/exception.h>
+#include <protocols.h> // Priority
+#include <utils.h>
+
+namespace SecurityManager {
+
+template <class DerivedService, class Event>
+class ServiceThreadDispatcher {
+ std::mutex m_eventQueueMutex;
+ std::condition_variable m_waitCondition;
+ bool m_quit = false;
+ std::queue<Event *> m_eventQueues[Priority::END];
+ std::thread m_thread; // initialized last
+
+public:
+ ServiceThreadDispatcher() : m_thread(&ServiceThreadDispatcher::ThreadLoop, this) {}
+
+ ~ServiceThreadDispatcher() {
+ {
+ std::lock_guard<std::mutex> lock(m_eventQueueMutex);
+ m_quit = true;
+ }
+ m_waitCondition.notify_one();
+ m_thread.join();
+
+ // clear the event queue
+ for (auto &queue: m_eventQueues)
+ while (!queue.empty()) {
+ delete queue.front();
+ queue.pop();
+ }
+ }
+
+ template <class...T>
+ void PutEvent(Priority priority, T&&...arg) {
+ assert(priority < arraySize(m_eventQueues));
+ const auto event = new Event{ std::forward<T>(arg)... };
+ {
+ std::lock_guard<std::mutex> lock(m_eventQueueMutex);
+ m_eventQueues[priority].emplace(event);
+ }
+ m_waitCondition.notify_one();
+ }
+
+private:
+ void ThreadLoop() {
+ for (;;) {
+ Event *event;
+ {
+ std::unique_lock<std::mutex> ulock(m_eventQueueMutex);
+ for (;;) {
+ if (m_quit)
+ return;
+ for (auto &queue: m_eventQueues)
+ if (!queue.empty()) {
+ event = queue.front();
+ queue.pop();
+ goto handleOneEvent;
+ }
+ m_waitCondition.wait(ulock);
+ }
+ }
+
+ handleOneEvent:
+ UNHANDLED_EXCEPTION_HANDLER_BEGIN
+ {
+ const auto eventGuard = makeUnique(event);
+ static_cast<DerivedService*>(this)->processEvent(std::move(*event));
+ }
+ UNHANDLED_EXCEPTION_HANDLER_END
+ }
+ }
+};
+
+} // namespace SecurityManager
+++ /dev/null
-/*
- * Copyright (c) 2014-2022 Samsung Electronics Co., Ltd. All rights reserved
- *
- * This file is licensed under the terms of MIT License or the Apache License
- * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
- * See the LICENSE file or the notice below for Apache License Version 2.0
- * details.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/*
- * @file service-thread.h
- * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
- * @version 1.0
- * @brief Implementation of threads.
- */
-
-#pragma once
-
-#include <cassert>
-#include <condition_variable>
-#include <mutex>
-#include <queue>
-#include <thread>
-
-#include <dpl/exception.h>
-#include <protocols.h> // Priority
-#include <utils.h>
-
-namespace SecurityManager {
-
-template <class DerivedService, class Event>
-class ServiceThread {
- std::mutex m_eventQueueMutex;
- std::condition_variable m_waitCondition;
- bool m_quit = false;
- std::queue<Event *> m_eventQueues[Priority::END];
- std::thread m_thread; // initialized last
-
-public:
- ServiceThread() : m_thread(&ServiceThread::ThreadLoop, this) {}
-
- ~ServiceThread() {
- {
- std::lock_guard<std::mutex> lock(m_eventQueueMutex);
- m_quit = true;
- }
- m_waitCondition.notify_one();
- m_thread.join();
-
- // clear the event queue
- for (auto &queue: m_eventQueues)
- while (!queue.empty()) {
- delete queue.front();
- queue.pop();
- }
- }
-
- template <class...T>
- void PutEvent(Priority priority, T&&...arg) {
- assert(priority < arraySize(m_eventQueues));
- const auto event = new Event{ std::forward<T>(arg)... };
- {
- std::lock_guard<std::mutex> lock(m_eventQueueMutex);
- m_eventQueues[priority].emplace(event);
- }
- m_waitCondition.notify_one();
- }
-
-private:
- void ThreadLoop() {
- for (;;) {
- Event *event;
- {
- std::unique_lock<std::mutex> ulock(m_eventQueueMutex);
- for (;;) {
- if (m_quit)
- return;
- for (auto &queue: m_eventQueues)
- if (!queue.empty()) {
- event = queue.front();
- queue.pop();
- goto handleOneEvent;
- }
- m_waitCondition.wait(ulock);
- }
- }
-
- handleOneEvent:
- UNHANDLED_EXCEPTION_HANDLER_BEGIN
- {
- const auto eventGuard = makeUnique(event);
- static_cast<DerivedService*>(this)->processEvent(std::move(*event));
- }
- UNHANDLED_EXCEPTION_HANDLER_END
- }
- }
-};
-
-} // namespace SecurityManager
#include <credentials.h>
#include <message-buffer.h>
#include <service_impl.h>
-#include <service-thread.h>
+#include <service-thread-dispatcher.h>
#include <socket-manager.h>
namespace SecurityManager {
MessageBuffer buffer;
};
-class Service final : public ServiceThread<Service, Event>
+class Service final : public ServiceThreadDispatcher<Service, Event>
{
ServiceImpl m_serviceImpl;
SocketManager *m_serviceManager = nullptr;