• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_API_TASK_QUEUE_H
17 #define META_API_TASK_QUEUE_H
18 
19 #include <base/containers/type_traits.h>
20 
21 #include <meta/api/future.h>
22 #include <meta/api/task.h>
23 #include <meta/interface/detail/any.h>
24 #include <meta/interface/interface_helpers.h>
25 #include <meta/interface/intf_promise.h>
26 #include <meta/interface/intf_task_queue_registry.h>
27 
META_BEGIN_NAMESPACE()28 META_BEGIN_NAMESPACE()
29 
30 template<typename Func>
31 auto CreateFutureFromResultFunction(Func&& func)
32 {
33     auto fut =
34         GetTaskQueueRegistry().ConstructFutureWithValue(InvokeWaitableTaskFunction(BASE_NS::forward<Func>(func)));
35     return Future<PlainType_t<decltype(func())>>(fut);
36 }
37 
38 template<typename Func>
AddFutureTaskOrRunDirectly(const ITaskQueue::Ptr & queue,Func && func)39 auto AddFutureTaskOrRunDirectly(const ITaskQueue::Ptr& queue, Func&& func)
40 {
41     auto q = GetTaskQueueRegistry().GetCurrentTaskQueue();
42     if (q == queue) {
43         return CreateFutureFromResultFunction(BASE_NS::forward<Func>(func));
44     }
45     return Future<PlainType_t<decltype(func())>>(queue->AddWaitableTask(CreateWaitableTask(BASE_NS::move(func))));
46 }
47 
48 META_END_NAMESPACE()
49 
50 #endif
51