1// Copyright 2020 The Marl Authors.
2//
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// https://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#ifndef marl_task_h
16#define marl_task_h
17
18#include "export.h"
19
20#include <functional>
21
22namespace marl {
23
24// Task is a unit of work for the scheduler.
25class Task {
26 public:
27 using Function = std::function<void()>;
28
29 enum class Flags {
30 None = 0,
31
32 // SameThread ensures the task will be run on the same thread that scheduled
33 // the task. This can offer performance improvements if the current thread
34 // is immediately going to block on the newly scheduled task, by reducing
35 // overheads of waking another thread.
36 SameThread = 1,
37 };
38
39 MARL_NO_EXPORT inline Task();
40 MARL_NO_EXPORT inline Task(const Task&);
41 MARL_NO_EXPORT inline Task(Task&&);
42 MARL_NO_EXPORT inline Task(const Function& function,
43 Flags flags = Flags::None);
44 MARL_NO_EXPORT inline Task(Function&& function, Flags flags = Flags::None);
45 MARL_NO_EXPORT inline Task& operator=(const Task&);
46 MARL_NO_EXPORT inline Task& operator=(Task&&);
47 MARL_NO_EXPORT inline Task& operator=(const Function&);
48 MARL_NO_EXPORT inline Task& operator=(Function&&);
49
50 // operator bool() returns true if the Task has a valid function.
51 MARL_NO_EXPORT inline operator bool() const;
52
53 // operator()() runs the task.
54 MARL_NO_EXPORT inline void operator()() const;
55
56 // is() returns true if the Task was created with the given flag.
57 MARL_NO_EXPORT inline bool is(Flags flag) const;
58
59 private:
60 Function function;
61 Flags flags = Flags::None;
62};
63
64Task::Task() {}
65Task::Task(const Task& o) : function(o.function), flags(o.flags) {}
66Task::Task(Task&& o) : function(std::move(o.function)), flags(o.flags) {}
67Task::Task(const Function& function, Flags flags /* = Flags::None */)
68 : function(function), flags(flags) {}
69Task::Task(Function&& function, Flags flags /* = Flags::None */)
70 : function(std::move(function)), flags(flags) {}
71Task& Task::operator=(const Task& o) {
72 function = o.function;
73 flags = o.flags;
74 return *this;
75}
76Task& Task::operator=(Task&& o) {
77 function = std::move(o.function);
78 flags = o.flags;
79 return *this;
80}
81
82Task& Task::operator=(const Function& f) {
83 function = f;
84 flags = Flags::None;
85 return *this;
86}
87Task& Task::operator=(Function&& f) {
88 function = std::move(f);
89 flags = Flags::None;
90 return *this;
91}
92Task::operator bool() const {
93 return function.operator bool();
94}
95
96void Task::operator()() const {
97 function();
98}
99
100bool Task::is(Flags flag) const {
101 return (static_cast<int>(flags) & static_cast<int>(flag)) ==
102 static_cast<int>(flag);
103}
104
105} // namespace marl
106
107#endif // marl_task_h
108