00001 /* 00002 * Copyright 2016 The Cartographer Authors 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef CARTOGRAPHER_COMMON_THREAD_POOL_H_ 00018 #define CARTOGRAPHER_COMMON_THREAD_POOL_H_ 00019 00020 #include <deque> 00021 #include <functional> 00022 #include <memory> 00023 #include <thread> 00024 #include <vector> 00025 00026 #include "absl/container/flat_hash_map.h" 00027 #include "absl/synchronization/mutex.h" 00028 #include "cartographer/common/task.h" 00029 00030 namespace cartographer { 00031 namespace common { 00032 00033 class Task; 00034 00035 class ThreadPoolInterface { 00036 public: 00037 ThreadPoolInterface() {} 00038 virtual ~ThreadPoolInterface() {} 00039 virtual std::weak_ptr<Task> Schedule(std::unique_ptr<Task> task) = 0; 00040 00041 protected: 00042 void Execute(Task* task); 00043 void SetThreadPool(Task* task); 00044 00045 private: 00046 friend class Task; 00047 00048 virtual void NotifyDependenciesCompleted(Task* task) = 0; 00049 }; 00050 00051 // A fixed number of threads working on tasks. Adding a task does not block. 00052 // Tasks may be added whether or not their dependencies are completed. 00053 // When all dependencies of a task are completed, it is queued up for execution 00054 // in a background thread. The queue must be empty before calling the 00055 // destructor. The thread pool will then wait for the currently executing work 00056 // items to finish and then destroy the threads. 00057 class ThreadPool : public ThreadPoolInterface { 00058 public: 00059 explicit ThreadPool(int num_threads); 00060 ~ThreadPool(); 00061 00062 ThreadPool(const ThreadPool&) = delete; 00063 ThreadPool& operator=(const ThreadPool&) = delete; 00064 00065 // When the returned weak pointer is expired, 'task' has certainly completed, 00066 // so dependants no longer need to add it as a dependency. 00067 std::weak_ptr<Task> Schedule(std::unique_ptr<Task> task) 00068 LOCKS_EXCLUDED(mutex_) override; 00069 00070 private: 00071 void DoWork(); 00072 00073 void NotifyDependenciesCompleted(Task* task) LOCKS_EXCLUDED(mutex_) override; 00074 00075 absl::Mutex mutex_; 00076 bool running_ GUARDED_BY(mutex_) = true; 00077 std::vector<std::thread> pool_ GUARDED_BY(mutex_); 00078 std::deque<std::shared_ptr<Task>> task_queue_ GUARDED_BY(mutex_); 00079 absl::flat_hash_map<Task*, std::shared_ptr<Task>> tasks_not_ready_ 00080 GUARDED_BY(mutex_); 00081 }; 00082 00083 } // namespace common 00084 } // namespace cartographer 00085 00086 #endif // CARTOGRAPHER_COMMON_THREAD_POOL_H_