task.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The Cartographer Authors
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 
17 #ifndef CARTOGRAPHER_COMMON_TASK_H_
18 #define CARTOGRAPHER_COMMON_TASK_H_
19 
20 #include <set>
21 
23 #include "glog/logging.h"
24 #include "thread_pool.h"
25 
26 namespace cartographer {
27 namespace common {
28 
29 class ThreadPoolInterface;
30 
31 class Task {
32  public:
33  friend class ThreadPoolInterface;
34 
35  using WorkItem = std::function<void()>;
37 
38  Task() = default;
39  ~Task();
40 
42 
43  // State must be 'NEW'.
44  void SetWorkItem(const WorkItem& work_item) EXCLUDES(mutex_);
45 
46  // State must be 'NEW'. 'dependency' may be nullptr, in which case it is
47  // assumed completed.
48  void AddDependency(std::weak_ptr<Task> dependency) EXCLUDES(mutex_);
49 
50  private:
51  // Allowed in all states.
52  void AddDependentTask(Task* dependent_task);
53 
54  // State must be 'DEPENDENCIES_COMPLETED' and becomes 'COMPLETED'.
55  void Execute() EXCLUDES(mutex_);
56 
57  // State must be 'NEW' and becomes 'DISPATCHED' or 'DEPENDENCIES_COMPLETED'.
59 
60  // State must be 'NEW' or 'DISPATCHED'. If 'DISPATCHED', may become
61  // 'DEPENDENCIES_COMPLETED'.
62  void OnDependenyCompleted();
63 
64  WorkItem work_item_ GUARDED_BY(mutex_);
65  ThreadPoolInterface* thread_pool_to_notify_ GUARDED_BY(mutex_) = nullptr;
66  State state_ GUARDED_BY(mutex_) = NEW;
67  unsigned int uncompleted_dependencies_ GUARDED_BY(mutex_) = 0;
68  std::set<Task*> dependent_tasks_ GUARDED_BY(mutex_);
69 
70  Mutex mutex_;
71 };
72 
73 } // namespace common
74 } // namespace cartographer
75 
76 #endif // CARTOGRAPHER_COMMON_TASK_H_
void SetWorkItem(const WorkItem &work_item) EXCLUDES(mutex_)
Definition: task.cc:34
std::function< void()> WorkItem
Definition: task.h:35
void AddDependency(std::weak_ptr< Task > dependency) EXCLUDES(mutex_)
Definition: task.cc:40
#define EXCLUDES(...)
Definition: mutex.h:53
void OnDependenyCompleted()
Definition: task.cc:76
State GetState() EXCLUDES(mutex_)
Definition: task.cc:29
void Execute() EXCLUDES(mutex_)
Definition: task.cc:87
void SetThreadPool(ThreadPoolInterface *thread_pool) EXCLUDES(mutex_)
Definition: task.cc:54
void AddDependentTask(Task *dependent_task)
Definition: task.cc:66
WorkItem work_item_ GUARDED_BY(mutex_)


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58