Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #ifndef FIREBOLT_RIALTO_SERVER_MAIN_THREAD_H_
21 : #define FIREBOLT_RIALTO_SERVER_MAIN_THREAD_H_
22 :
23 : #include "IMainThread.h"
24 : #include <atomic>
25 : #include <condition_variable>
26 : #include <deque>
27 : #include <map>
28 : #include <memory>
29 : #include <mutex>
30 : #include <set>
31 : #include <string>
32 : #include <thread>
33 :
34 : namespace firebolt::rialto::server
35 : {
36 : /**
37 : * @brief IMainThread factory class definition.
38 : */
39 : class MainThreadFactory : public IMainThreadFactory
40 : {
41 : public:
42 28 : MainThreadFactory() = default;
43 28 : ~MainThreadFactory() override = default;
44 :
45 : std::shared_ptr<IMainThread> getMainThread() const override;
46 :
47 : protected:
48 : /**
49 : * @brief Weak pointer to the singleton main thread object.
50 : */
51 : static std::weak_ptr<IMainThread> m_mainThread;
52 :
53 : /**
54 : * @brief Mutex protection for creation of the MainThread object.
55 : */
56 : static std::mutex m_creationMutex;
57 : };
58 :
59 : /**
60 : * @brief The definition of the MediaKeys.
61 : */
62 : class MainThread : public IMainThread
63 : {
64 : public:
65 : MainThread();
66 : virtual ~MainThread();
67 :
68 : int32_t registerClient() override;
69 : void unregisterClient(uint32_t clientId) override;
70 :
71 : void enqueueTask(uint32_t clientId, const Task &task) override;
72 : void enqueueTaskAndWait(uint32_t clientId, const Task &task) override;
73 : void enqueuePriorityTaskAndWait(uint32_t clientId, const Task &task) override;
74 :
75 : private:
76 : /**
77 : * @brief Information of a task.
78 : */
79 : struct TaskInfo
80 : {
81 : bool done{false}; /**< A flag indicating whether the task has completed. */
82 : uint32_t clientId; /**< The id of the client creating the task. */
83 : Task task; /**< The task to execute. */
84 : std::unique_ptr<std::mutex> mutex; /**< Mutex for the task condition variable. */
85 : std::unique_ptr<std::condition_variable> cv; /**< The condition variable of the task. */
86 : };
87 :
88 : /**
89 : * @brief Starts a loop that listens for enqueued tasks.
90 : */
91 : void mainThreadLoop();
92 :
93 : /**
94 : * @brief Waits for tasks to enter the queue and returns the next task.
95 : *
96 : * @retval The next task in the queue.
97 : */
98 : const std::shared_ptr<TaskInfo> waitForTask();
99 :
100 : /**
101 : * @brief Whether the main thread is running.
102 : */
103 : bool m_isMainThreadRunning;
104 :
105 : /**
106 : * @brief The main thread.
107 : */
108 : std::thread m_thread;
109 :
110 : /**
111 : * @brief A mutex protecting access to the task queue.
112 : */
113 : std::mutex m_taskQueueMutex;
114 :
115 : /**
116 : * @brief A condition variable used to notify of tasks entering the task queue.
117 : */
118 : std::condition_variable m_taskQueueCv;
119 :
120 : /**
121 : * @brief The queue of tasks and there infomation.
122 : */
123 : std::deque<std::shared_ptr<TaskInfo>> m_taskQueue;
124 :
125 : /**
126 : * @brief The main thread objects client id, for registering new clients.
127 : */
128 : const uint32_t m_mainThreadClientId;
129 :
130 : /**
131 : * @brief The next client id to be given to a registering client.
132 : */
133 : std::atomic<uint32_t> m_nextClientId;
134 :
135 : /**
136 : * @brief Clients registered on this thread.
137 : */
138 : std::set<uint32_t> m_registeredClients;
139 : };
140 : } // namespace firebolt::rialto::server
141 :
142 : #endif // FIREBOLT_RIALTO_SERVER_MAIN_THREAD_H_
|