summaryrefslogtreecommitdiff
path: root/tools/thread_pool_internal.h
blob: 92a11767577ca768414fab41fafb923a02cc1183 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#ifndef TOOLS_THREAD_POOL_INTERNAL_H_
#define TOOLS_THREAD_POOL_INTERNAL_H_

#include <jxl/thread_parallel_runner_cxx.h>
#include <stddef.h>

#include <cmath>
#include <thread>  // NOLINT

#include "lib/jxl/base/data_parallel.h"

namespace jpegxl {
namespace tools {

using ::jxl::ThreadPool;

// Helper class to pass an internal ThreadPool-like object using threads.
class ThreadPoolInternal {
 public:
  // Starts the given number of worker threads and blocks until they are ready.
  // "num_worker_threads" defaults to one per hyperthread. If zero, all tasks
  // run on the main thread.
  explicit ThreadPoolInternal(
      size_t num_threads = std::thread::hardware_concurrency()) {
    runner_ =
        JxlThreadParallelRunnerMake(/* memory_manager */ nullptr, num_threads);
    pool_.reset(new ThreadPool(JxlThreadParallelRunner, runner_.get()));
  }

  ThreadPoolInternal(const ThreadPoolInternal&) = delete;
  ThreadPoolInternal& operator&(const ThreadPoolInternal&) = delete;
  ThreadPool* operator&() { return pool_.get(); }

 private:
  JxlThreadParallelRunnerPtr runner_;
  std::unique_ptr<ThreadPool> pool_;
};

}  // namespace tools
}  // namespace jpegxl

#endif  // TOOLS_THREAD_POOL_INTERNAL_H_