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, Task task) override;
72 : void enqueueTaskAndWait(uint32_t clientId, Task task) override;
73 : void enqueuePriorityTaskAndWait(uint32_t clientId, Task task) override;
74 :
75 : private:
76 : /**
77 : * @brief Information of a task.
78 : */
79 : struct TaskInfo
80 : {
81 : uint32_t clientId; /**< The id of the client creating the task. */
82 : Task task; /**< The task to execute. */
83 : std::unique_ptr<std::mutex> mutex; /**< Mutex for the task condition variable. */
84 : std::unique_ptr<std::condition_variable> cv; /**< The condition variable of the task. */
85 : };
86 :
87 : /**
88 : * @brief Starts a loop that listens for enqueued tasks.
89 : */
90 : void mainThreadLoop();
91 :
92 : /**
93 : * @brief Waits for tasks to enter the queue and returns the next task.
94 : *
95 : * @retval The next task in the queue.
96 : */
97 : const std::shared_ptr<TaskInfo> waitForTask();
98 :
99 : /**
100 : * @brief Whether the main thread is running.
101 : */
102 : bool m_isMainThreadRunning;
103 :
104 : /**
105 : * @brief The main thread.
106 : */
107 : std::thread m_thread;
108 :
109 : /**
110 : * @brief A mutex protecting access to the task queue.
111 : */
112 : std::mutex m_taskQueueMutex;
113 :
114 : /**
115 : * @brief A condition variable used to notify of tasks entering the task queue.
116 : */
117 : std::condition_variable m_taskQueueCv;
118 :
119 : /**
120 : * @brief The queue of tasks and there infomation.
121 : */
122 : std::deque<std::shared_ptr<TaskInfo>> m_taskQueue;
123 :
124 : /**
125 : * @brief The main thread objects client id, for registering new clients.
126 : */
127 : const uint32_t m_mainThreadClientId;
128 :
129 : /**
130 : * @brief The next client id to be given to a registering client.
131 : */
132 : std::atomic<uint32_t> m_nextClientId;
133 :
134 : /**
135 : * @brief Clients registered on this thread.
136 : */
137 : std::set<uint32_t> m_registeredClients;
138 : };
139 : } // namespace firebolt::rialto::server
140 :
141 : #endif // FIREBOLT_RIALTO_SERVER_MAIN_THREAD_H_
|