task.cc
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 
18 
19 namespace cartographer {
20 namespace common {
21 
23  // TODO(gaschler): Relax some checks after testing.
24  if (state_ != NEW && state_ != COMPLETED) {
25  LOG(WARNING) << "Delete Task between dispatch and completion.";
26  }
27 }
28 
30  MutexLocker locker(&mutex_);
31  return state_;
32 }
33 
34 void Task::SetWorkItem(const WorkItem& work_item) {
35  MutexLocker locker(&mutex_);
36  CHECK_EQ(state_, NEW);
37  work_item_ = work_item;
38 }
39 
40 void Task::AddDependency(std::weak_ptr<Task> dependency) {
41  std::shared_ptr<Task> shared_dependency;
42  {
43  MutexLocker locker(&mutex_);
44  CHECK_EQ(state_, NEW);
45  if ((shared_dependency = dependency.lock())) {
46  ++uncompleted_dependencies_;
47  }
48  }
49  if (shared_dependency) {
50  shared_dependency->AddDependentTask(this);
51  }
52 }
53 
55  MutexLocker locker(&mutex_);
56  CHECK_EQ(state_, NEW);
57  state_ = DISPATCHED;
58  thread_pool_to_notify_ = thread_pool;
59  if (uncompleted_dependencies_ == 0) {
60  state_ = DEPENDENCIES_COMPLETED;
61  CHECK(thread_pool_to_notify_);
62  thread_pool_to_notify_->NotifyDependenciesCompleted(this);
63  }
64 }
65 
66 void Task::AddDependentTask(Task* dependent_task) {
67  MutexLocker locker(&mutex_);
68  if (state_ == COMPLETED) {
69  dependent_task->OnDependenyCompleted();
70  return;
71  }
72  bool inserted = dependent_tasks_.insert(dependent_task).second;
73  CHECK(inserted) << "Given dependency is already a dependency.";
74 }
75 
77  MutexLocker locker(&mutex_);
78  CHECK(state_ == NEW || state_ == DISPATCHED);
79  --uncompleted_dependencies_;
80  if (uncompleted_dependencies_ == 0 && state_ == DISPATCHED) {
81  state_ = DEPENDENCIES_COMPLETED;
82  CHECK(thread_pool_to_notify_);
83  thread_pool_to_notify_->NotifyDependenciesCompleted(this);
84  }
85 }
86 
87 void Task::Execute() {
88  {
89  MutexLocker locker(&mutex_);
90  CHECK_EQ(state_, DEPENDENCIES_COMPLETED);
91  state_ = RUNNING;
92  }
93 
94  // Execute the work item.
95  if (work_item_) {
96  work_item_();
97  }
98 
99  MutexLocker locker(&mutex_);
100  state_ = COMPLETED;
101  for (Task* dependent_task : dependent_tasks_) {
102  dependent_task->OnDependenyCompleted();
103  }
104 }
105 
106 } // namespace common
107 } // namespace cartographer
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
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
Mutex::Locker MutexLocker
Definition: mutex.h:95
void AddDependentTask(Task *dependent_task)
Definition: task.cc:66


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