olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
TaskScheduler.h
1/*
2 * Copyright (C) 2019-2021 HERE Europe B.V.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 * License-Filename: LICENSE
18 */
19
20#pragma once
21
22#include <utility>
23
24#include <olp/core/client/ApiResponse.h>
25#include <olp/core/client/CancellationContext.h>
27
28namespace olp {
29namespace thread {
30
33enum Priority : uint32_t { LOW = 100, NORMAL = 500, HIGH = 1000 };
34
45class CORE_API TaskScheduler {
46 public:
48 using CallFuncType = std::function<void()>;
49
50 virtual ~TaskScheduler() = default;
51
60 void ScheduleTask(CallFuncType&& func) { EnqueueTask(std::move(func)); }
61
70 void ScheduleTask(CallFuncType&& func, uint32_t priority) {
71 EnqueueTask(std::move(func), priority);
72 }
73
91 template <class Function, typename std::enable_if<!std::is_convertible<
92 decltype(std::declval<Function>()),
93 CallFuncType>::value>::type* = nullptr>
96 auto task = [func, context]() {
97 if (!context.IsCancelled()) {
98 func(context);
99 }
100 };
101 EnqueueTask(std::move(task));
102 return context;
103 }
104
105 protected:
117 virtual void EnqueueTask(CallFuncType&&) = 0;
118
135 virtual void EnqueueTask(CallFuncType&& func, uint32_t priority) {
136 OLP_SDK_CORE_UNUSED(priority);
137 EnqueueTask(std::forward<CallFuncType>(func));
138 }
139};
140
148inline CORE_API void ExecuteOrSchedule(
149 const std::shared_ptr<TaskScheduler>& scheduler,
151 if (!scheduler) {
152 // User didn't specify a TaskScheduler, execute sync
153 func();
154 return;
155 }
156
157 // Schedule for async execution
158 scheduler->ScheduleTask(std::move(func));
159}
160
161} // namespace thread
162} // namespace olp
Contains utilities used to work around compiler warnings.
#define OLP_SDK_CORE_UNUSED(...)
Arbitrarily marks many variables as unused to avoid compiler warnings.
Definition WarningWorkarounds.h:28
A wrapper that manages the cancellation state of an asynchronous operation in a thread-safe way.
Definition CancellationContext.h:40
bool IsCancelled() const
Checks whether this context is cancelled.
Definition CancellationContext.inl:66
An abstract interface that is used as a base for the custom thread scheduling strategy.
Definition TaskScheduler.h:45
virtual void EnqueueTask(CallFuncType &&)=0
The abstract enqueue task interface that is implemented by the subclass.
client::CancellationContext ScheduleTask(Function &&func)
Schedules the asynchronous cancellable task.
Definition TaskScheduler.h:94
virtual void EnqueueTask(CallFuncType &&func, uint32_t priority)
The enqueue task with priority interface that is implemented by the subclass.
Definition TaskScheduler.h:135
std::function< void()> CallFuncType
An alias for the abstract interface input.
Definition TaskScheduler.h:48
void ScheduleTask(CallFuncType &&func, uint32_t priority)
Schedules the asynchronous task.
Definition TaskScheduler.h:70
void ScheduleTask(CallFuncType &&func)
Schedules the asynchronous task.
Definition TaskScheduler.h:60
Rules all the other namespaces.
Definition AppleSignInProperties.h:24