1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef META_INTERFACE_ITASKQUEUE_REGISTRY_H
17 #define META_INTERFACE_ITASKQUEUE_REGISTRY_H
18
19 #include <core/plugin/intf_plugin.h>
20 #include <core/plugin/intf_plugin_register.h>
21
22 #include <meta/base/meta_types.h>
23 #include <meta/base/namespace.h>
24 #include <meta/interface/intf_meta_object_lib.h>
25 #include <meta/interface/intf_task_queue.h>
26
27 META_BEGIN_NAMESPACE()
28
29 class IPromise;
30 class IFuture;
31
32 META_REGISTER_INTERFACE(ITaskQueueRegistry, "9dd2907e-dc6c-4090-8873-fa84f367b87e")
33
34 /**
35 * @brief The ITaskQueueRegistry interface can be used to register task queue objects which
36 * need to be used across the application.
37 */
38 class ITaskQueueRegistry : public CORE_NS::IInterface {
39 META_INTERFACE(CORE_NS::IInterface, ITaskQueueRegistry)
40 public:
41 /**
42 * @brief Get a task queue with a given queue id.
43 * @param queueId The id of the queue to get.
44 * @return The task queue with a given id or nullptr if no queue has been registered with the id.
45 */
46 virtual ITaskQueue::Ptr GetTaskQueue(const BASE_NS::Uid& queueId) const = 0;
47 /**
48 * @brief Register a task queue instance with a given id.
49 * @param queue The task queue to register.
50 * If queue = {} this call is equivalent to calling UnregisterTaskQueue(queueId).
51 * @param queueId Id to register the queue with.
52 * If a queue has already been registered with the given id, the previous registration
53 * is replaced with the new one.
54 * @return True if registration was successful, false otherwise.
55 */
56 virtual bool RegisterTaskQueue(const ITaskQueue::Ptr& queue, const BASE_NS::Uid& queueId) = 0;
57 /**
58 * @brief Unregister a previous registered task queue.
59 * @param queueId Id of the task queue to unregister.
60 * @return True if a queue was unregistered, false otherwise.
61 */
62 virtual bool UnregisterTaskQueue(const BASE_NS::Uid& queueId) = 0;
63 /**
64 * @brief Check if a queue with a given id has been registered.
65 * @param queueId Id of the queue to check.
66 * @return True if a task queue with a given id has been registered, false otherwise.
67 */
68 virtual bool HasTaskQueue(const BASE_NS::Uid& queueId) const = 0;
69 /**
70 * @brief Unregisters all task queues.
71 * @return True if task queue registry is empty after this call.
72 */
73 virtual bool UnregisterAllTaskQueues() = 0;
74 /**
75 * @brief If execution of current thread comes via task queue (i.e. executing a task), this returns that queue.
76 * Note: Not all task queues have to implement this feature.
77 * Note: This is single variable, if executing tasks in a task, one has to take care to reset it correctly.
78 */
79 virtual ITaskQueue::Ptr GetCurrentTaskQueue() const = 0;
80 /**
81 * @brief Sets the currently executing task queue. One should always set the queue to nullptr when not executing a
82 * task.
83 * @return The previously set task queue.
84 */
85 virtual ITaskQueue::WeakPtr SetCurrentTaskQueue(ITaskQueue::WeakPtr) = 0;
86
87 virtual BASE_NS::shared_ptr<IPromise> ConstructPromise() = 0;
88 virtual BASE_NS::shared_ptr<IFuture> ConstructFutureWithValue(const IAny::Ptr& value) = 0;
89 };
90
91 /** Returns a reference to the global ITaskQueueRegistry instance. */
GetTaskQueueRegistry()92 inline META_NS::ITaskQueueRegistry& GetTaskQueueRegistry()
93 {
94 return GetMetaObjectLib().GetTaskQueueRegistry();
95 }
96
97 META_END_NAMESPACE()
98
99 #endif // META_INTERFACE_ITASKQUEUE_REGISTRY_H
100