00001 /* 00002 * Copyright 2018 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_INTERNAL_TESTING_THREAD_POOL_FOR_TESTING_H_ 00018 #define CARTOGRAPHER_COMMON_INTERNAL_TESTING_THREAD_POOL_FOR_TESTING_H_ 00019 00020 #include <deque> 00021 #include <functional> 00022 #include <map> 00023 #include <thread> 00024 00025 #include "absl/synchronization/mutex.h" 00026 #include "cartographer/common/thread_pool.h" 00027 00028 namespace cartographer { 00029 namespace common { 00030 namespace testing { 00031 00032 class ThreadPoolForTesting : public ThreadPoolInterface { 00033 public: 00034 ThreadPoolForTesting(); 00035 ~ThreadPoolForTesting(); 00036 00037 std::weak_ptr<Task> Schedule(std::unique_ptr<Task> task) 00038 LOCKS_EXCLUDED(mutex_) override; 00039 00040 void WaitUntilIdle(); 00041 00042 private: 00043 friend class Task; 00044 00045 void DoWork(); 00046 00047 void NotifyDependenciesCompleted(Task* task) LOCKS_EXCLUDED(mutex_) override; 00048 00049 absl::Mutex mutex_; 00050 bool running_ GUARDED_BY(mutex_) = true; 00051 bool idle_ GUARDED_BY(mutex_) = true; 00052 std::deque<std::shared_ptr<Task>> task_queue_ GUARDED_BY(mutex_); 00053 std::map<Task*, std::shared_ptr<Task>> tasks_not_ready_ GUARDED_BY(mutex_); 00054 std::thread thread_ GUARDED_BY(mutex_); 00055 }; 00056 00057 } // namespace testing 00058 } // namespace common 00059 } // namespace cartographer 00060 00061 #endif // CARTOGRAPHER_COMMON_INTERNAL_TESTING_THREAD_POOL_FOR_TESTING_H_