thread_pool_internal.h (1474B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #ifndef TOOLS_THREAD_POOL_INTERNAL_H_ 7 #define TOOLS_THREAD_POOL_INTERNAL_H_ 8 9 #include <jxl/thread_parallel_runner.h> 10 #include <jxl/thread_parallel_runner_cxx.h> 11 12 #include <cstddef> 13 #include <memory> 14 #include <thread> // NOLINT 15 16 #include "lib/jxl/base/data_parallel.h" 17 18 namespace jpegxl { 19 namespace tools { 20 21 using ::jxl::ThreadPool; 22 23 // Helper class to pass an internal ThreadPool-like object using threads. 24 class ThreadPoolInternal { 25 public: 26 // Starts the given number of worker threads and blocks until they are ready. 27 // "num_worker_threads" defaults to one per hyperthread. If zero, all tasks 28 // run on the main thread. 29 explicit ThreadPoolInternal( 30 size_t num_threads = std::thread::hardware_concurrency()) { 31 runner_ = 32 JxlThreadParallelRunnerMake(/* memory_manager */ nullptr, num_threads); 33 pool_.reset(new ThreadPool(JxlThreadParallelRunner, runner_.get())); 34 } 35 36 ThreadPoolInternal(const ThreadPoolInternal&) = delete; 37 ThreadPoolInternal& operator&(const ThreadPoolInternal&) = delete; 38 // TODO(eustas): avoid unary `&` overload? 39 ThreadPool* operator&() { return pool_.get(); } 40 41 private: 42 JxlThreadParallelRunnerPtr runner_; 43 std::unique_ptr<ThreadPool> pool_; 44 }; 45 46 } // namespace tools 47 } // namespace jpegxl 48 49 #endif // TOOLS_THREAD_POOL_INTERNAL_H_