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_I_MAIN_THREAD_H_
21 : #define FIREBOLT_RIALTO_SERVER_I_MAIN_THREAD_H_
22 :
23 : #include "IMainThread.h"
24 : #include <functional>
25 : #include <memory>
26 : #include <utility>
27 :
28 : namespace firebolt::rialto::server
29 : {
30 : class IMainThread;
31 :
32 : /**
33 : * @brief IMainThread factory class, gets the concrete implementation of IMainThread
34 : */
35 : class IMainThreadFactory
36 : {
37 : public:
38 346 : IMainThreadFactory() = default;
39 346 : virtual ~IMainThreadFactory() = default;
40 :
41 : /**
42 : * @brief Create a IMainThreadFactory instance.
43 : *
44 : * @retval the factory instance or null on error.
45 : */
46 : static std::shared_ptr<IMainThreadFactory> createFactory();
47 :
48 : /**
49 : * @brief IMainThread factory method, gets a concrete implementation of IMainThread
50 : *
51 : * @retval the main thread instance or null on error.
52 : */
53 : virtual std::shared_ptr<IMainThread> getMainThread() const = 0;
54 : };
55 :
56 : /**
57 : * @brief The definition of the IMainThread interface.
58 : */
59 : class IMainThread
60 : {
61 : public:
62 : using Task = std::function<void()>;
63 :
64 326 : IMainThread() = default;
65 326 : virtual ~IMainThread() = default;
66 :
67 : IMainThread(const IMainThread &) = delete;
68 : IMainThread(IMainThread &&) = delete;
69 : IMainThread &operator=(const IMainThread &) = delete;
70 : IMainThread &operator=(IMainThread &&) = delete;
71 :
72 : /**
73 : * @brief Register a client on the main thread.
74 : *
75 : * Required by clients who want to enqueue tasks on the main thread.
76 : *
77 : * @retval The registered client id.
78 : */
79 : virtual int32_t registerClient() = 0;
80 :
81 : /**
82 : * @brief Unregister a client on the main thread.
83 : *
84 : * Should be called on the main thread.
85 : * After been called the client will no longer be able to enqueue tasks.
86 : *
87 : * @param[in] clientId : The id of the registered client.
88 : */
89 : virtual void unregisterClient(uint32_t clientId) = 0;
90 :
91 : /**
92 : * @brief Enqueue a task on the main thread and return.
93 : *
94 : * @param[in] clientId : The id of the registered client.
95 : * @param[in] task : Task to queue.
96 : */
97 : virtual void enqueueTask(uint32_t clientId, Task task) = 0;
98 :
99 : /**
100 : * @brief Enqueue a task on the main thread and wait for it to finish before returning.
101 : *
102 : * @param[in] clientId : The id of the registered client.
103 : * @param[in] task : Task to queue.
104 : */
105 : virtual void enqueueTaskAndWait(uint32_t clientId, Task task) = 0;
106 :
107 : /**
108 : * @brief Enqueue a priority task on the main thread and wait for it to finish before returning.
109 : *
110 : * @param[in] clientId : The id of the registered client.
111 : * @param[in] task : Task to queue.
112 : */
113 : virtual void enqueuePriorityTaskAndWait(uint32_t clientId, Task task) = 0;
114 : };
115 : } // namespace firebolt::rialto::server
116 :
117 : #endif // FIREBOLT_RIALTO_SERVER_I_MAIN_THREAD_H_
|