Rename service-thread.h to service-thread-dispatcher.h 13/319213/1
authorTomasz Swierczek <t.swierczek@samsung.com>
Thu, 17 Oct 2024 07:32:51 +0000 (09:32 +0200)
committerTomasz Swierczek <t.swierczek@samsung.com>
Thu, 17 Oct 2024 07:40:51 +0000 (09:40 +0200)
This file will no longer implement only one service thread.
Changed the name not to be misleading.

Change-Id: Ibbf90daaf8a399108e5caabf4627ad8646156464

src/server/main/include/service-thread-dispatcher.h [new file with mode: 0644]
src/server/main/include/service-thread.h [deleted file]
src/server/service/include/service.h

diff --git a/src/server/main/include/service-thread-dispatcher.h b/src/server/main/include/service-thread-dispatcher.h
new file mode 100644 (file)
index 0000000..608238a
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * 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
diff --git a/src/server/main/include/service-thread.h b/src/server/main/include/service-thread.h
deleted file mode 100644 (file)
index 573e496..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * 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
index 86bb407cf58421376659a4bf74b477d043ef98a4..98a5443d8ee94bbe758205ef11da5d561aa9ef4e 100644 (file)
@@ -32,7 +32,7 @@
 #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 {
@@ -43,7 +43,7 @@ struct Event {
     MessageBuffer buffer;
 };
 
-class Service final : public ServiceThread<Service, Event>
+class Service final : public ServiceThreadDispatcher<Service, Event>
 {
     ServiceImpl m_serviceImpl;
     SocketManager *m_serviceManager = nullptr;