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_interface.h>
20 #include <core/plugin/intf_plugin.h>
21 #include <core/plugin/intf_plugin_register.h>
22
23 #include <meta/base/meta_types.h>
24 #include <meta/base/namespace.h>
25 #include <meta/interface/intf_meta_object_lib.h>
26 #include <meta/interface/intf_task_queue.h>
27
28 META_BEGIN_NAMESPACE()
29
30 class IPromise;
31 class IFuture;
32
33 META_REGISTER_INTERFACE(ITaskQueueRegistry, "9dd2907e-dc6c-4090-8873-fa84f367b87e")
34
35 /**
36 * @brief The ITaskQueueRegistry interface can be used to register task queue objects which
37 * need to be used across the application.
38 */
39 class ITaskQueueRegistry : public CORE_NS::IInterface {
40 META_INTERFACE(CORE_NS::IInterface, ITaskQueueRegistry)
41 public:
42 /**
43 * @brief Get a task queue with a given queue id.
44 * @param queueId The id of the queue to get.
45 * @return The task queue with a given id or nullptr if no queue has been registered with the id.
46 */
47 virtual ITaskQueue::Ptr GetTaskQueue(const BASE_NS::Uid& queueId) const = 0;
48 /**
49 * @brief Register a task queue instance with a given id.
50 * @param queue The task queue to register.
51 * If queue = {} this call is equivalent to calling UnregisterTaskQueue(queueId).
52 * @param queueId Id to register the queue with.
53 * If a queue has already been registered with the given id, the previous registration
54 * is replaced with the new one.
55 * @return True if registration was successful, false otherwise.
56 */
57 virtual bool RegisterTaskQueue(const ITaskQueue::Ptr& queue, const BASE_NS::Uid& queueId) = 0;
58 /**
59 * @brief Unregister a previous registered task queue.
60 * @param queueId Id of the task queue to unregister.
61 * @return True if a queue was unregistered, false otherwise.
62 */
63 virtual bool UnregisterTaskQueue(const BASE_NS::Uid& queueId) = 0;
64 /**
65 * @brief Check if a queue with a given id has been registered.
66 * @param queueId Id of the queue to check.
67 * @return True if a task queue with a given id has been registered, false otherwise.
68 */
69 virtual bool HasTaskQueue(const BASE_NS::Uid& queueId) const = 0;
70 /**
71 * @brief Unregisters all task queues.
72 * @return True if task queue registry is empty after this call.
73 */
74 virtual bool UnregisterAllTaskQueues() = 0;
75 /**
76 * @brief If execution of current thread comes via task queue (i.e. executing a task), this returns that queue.
77 * Note: Not all task queues have to implement this feature.
78 * Note: This is single variable, if executing tasks in a task, one has to take care to reset it correctly.
79 */
80 virtual ITaskQueue::Ptr GetCurrentTaskQueue() const = 0;
81 /**
82 * @brief Sets the currently executing task queue. One should always set the queue to nullptr when not executing a
83 * task.
84 * @return The previously set task queue.
85 */
86 virtual ITaskQueue::WeakPtr SetCurrentTaskQueue(ITaskQueue::WeakPtr) = 0;
87
88 virtual BASE_NS::shared_ptr<IPromise> ConstructPromise() = 0;
89 virtual BASE_NS::shared_ptr<IFuture> ConstructFutureWithValue(const IAny::Ptr& value) = 0;
90 };
91
92 /** Returns a reference to the global ITaskQueueRegistry instance. */
GetTaskQueueRegistry()93 inline META_NS::ITaskQueueRegistry& GetTaskQueueRegistry()
94 {
95 return GetMetaObjectLib().GetTaskQueueRegistry();
96 }
97
98 META_END_NAMESPACE()
99
100 #endif // META_INTERFACE_ITASKQUEUE_REGISTRY_H
101