Coverage Report

Created: 2025-07-23 08:18

/src/libjxl/lib/threads/thread_parallel_runner_internal.cc
Line
Count
Source (jump to first uncovered line)
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
#include "lib/threads/thread_parallel_runner_internal.h"
7
8
#include <jxl/parallel_runner.h>
9
#include <jxl/types.h>
10
11
#include <algorithm>
12
#include <atomic>
13
#include <cstddef>
14
#include <cstdint>
15
#include <mutex>
16
#include <thread>
17
18
#include "lib/jxl/base/compiler_specific.h"
19
20
namespace jpegxl {
21
22
// static
23
JxlParallelRetCode ThreadParallelRunner::Runner(
24
    void* runner_opaque, void* jpegxl_opaque, JxlParallelRunInit init,
25
128k
    JxlParallelRunFunction func, uint32_t start_range, uint32_t end_range) {
26
128k
  ThreadParallelRunner* self =
27
128k
      static_cast<ThreadParallelRunner*>(runner_opaque);
28
128k
  if (start_range > end_range) return JXL_PARALLEL_RET_RUNNER_ERROR;
29
128k
  if (start_range == end_range) return JXL_PARALLEL_RET_SUCCESS;
30
31
128k
  int ret = init(jpegxl_opaque, std::max<size_t>(self->num_worker_threads_, 1));
32
128k
  if (ret != JXL_PARALLEL_RET_SUCCESS) return ret;
33
34
  // Use a sequential run when num_worker_threads_ is zero since we have no
35
  // worker threads.
36
128k
  if (self->num_worker_threads_ == 0) {
37
0
    const size_t thread = 0;
38
0
    for (uint32_t task = start_range; task < end_range; ++task) {
39
0
      func(jpegxl_opaque, task, thread);
40
0
    }
41
0
    return JXL_PARALLEL_RET_SUCCESS;
42
0
  }
43
44
128k
  if (self->depth_.fetch_add(1, std::memory_order_acq_rel) != 0) {
45
0
    return JXL_PARALLEL_RET_RUNNER_ERROR;  // Must not re-enter.
46
0
  }
47
48
128k
  const WorkerCommand worker_command =
49
128k
      (static_cast<WorkerCommand>(start_range) << 32) + end_range;
50
  // Ensure the inputs do not result in a reserved command.
51
128k
  if ((worker_command == kWorkerWait) || (worker_command == kWorkerOnce) ||
52
128k
      (worker_command == kWorkerExit)) {
53
0
    return JXL_PARALLEL_RET_RUNNER_ERROR;
54
0
  }
55
56
128k
  self->data_func_ = func;
57
128k
  self->jpegxl_opaque_ = jpegxl_opaque;
58
128k
  self->num_reserved_.store(0, std::memory_order_relaxed);
59
60
128k
  self->StartWorkers(worker_command);
61
128k
  self->WorkersReadyBarrier();
62
63
128k
  if (self->depth_.fetch_add(-1, std::memory_order_acq_rel) != 1) {
64
0
    return JXL_PARALLEL_RET_RUNNER_ERROR;
65
0
  }
66
128k
  return JXL_PARALLEL_RET_SUCCESS;
67
128k
}
68
69
// static
70
void ThreadParallelRunner::RunRange(ThreadParallelRunner* self,
71
                                    const WorkerCommand command,
72
128k
                                    const int thread) {
73
128k
  const uint32_t begin = command >> 32;
74
128k
  const uint32_t end = command & 0xFFFFFFFF;
75
128k
  const uint32_t num_tasks = end - begin;
76
128k
  const uint32_t num_worker_threads = self->num_worker_threads_;
77
78
  // OpenMP introduced several "schedule" strategies:
79
  // "single" (static assignment of exactly one chunk per thread): slower.
80
  // "dynamic" (allocates k tasks at a time): competitive for well-chosen k.
81
  // "guided" (allocates k tasks, decreases k): computing k = remaining/n
82
  //   is faster than halving k each iteration. We prefer this strategy
83
  //   because it avoids user-specified parameters.
84
85
767k
  for (;;) {
86
#if JXL_FALSE
87
    // dynamic
88
    const uint32_t my_size = std::max(num_tasks / (num_worker_threads * 4), 1);
89
#else
90
    // guided
91
767k
    const uint32_t num_reserved =
92
767k
        self->num_reserved_.load(std::memory_order_relaxed);
93
    // It is possible that more tasks are reserved than ready to run.
94
767k
    const uint32_t num_remaining =
95
767k
        num_tasks - std::min(num_reserved, num_tasks);
96
767k
    const uint32_t my_size =
97
767k
        std::max(num_remaining / (num_worker_threads * 4), 1u);
98
767k
#endif
99
767k
    const uint32_t my_begin = begin + self->num_reserved_.fetch_add(
100
767k
                                          my_size, std::memory_order_relaxed);
101
767k
    const uint32_t my_end = std::min(my_begin + my_size, begin + num_tasks);
102
    // Another thread already reserved the last task.
103
767k
    if (my_begin >= my_end) {
104
128k
      break;
105
128k
    }
106
5.58M
    for (uint32_t task = my_begin; task < my_end; ++task) {
107
4.94M
      self->data_func_(self->jpegxl_opaque_, task, thread);
108
4.94M
    }
109
639k
  }
110
128k
}
111
112
// static
113
void ThreadParallelRunner::ThreadFunc(ThreadParallelRunner* self,
114
62.8k
                                      const int thread) {
115
  // Until kWorkerExit command received:
116
191k
  for (;;) {
117
191k
    std::unique_lock<std::mutex> lock(self->mutex_);
118
    // Notify main thread that this thread is ready.
119
191k
    if (++self->workers_ready_ == self->num_threads_) {
120
191k
      self->workers_ready_cv_.notify_one();
121
191k
    }
122
191k
  RESUME_WAIT:
123
    // Wait for a command.
124
191k
    self->worker_start_cv_.wait(lock);
125
191k
    const WorkerCommand command = self->worker_start_command_;
126
191k
    switch (command) {
127
0
      case kWorkerWait:    // spurious wakeup:
128
0
        goto RESUME_WAIT;  // lock still held, avoid incrementing ready.
129
0
      case kWorkerOnce:
130
0
        lock.unlock();
131
0
        self->data_func_(self->jpegxl_opaque_, thread, thread);
132
0
        break;
133
62.8k
      case kWorkerExit:
134
62.8k
        return;  // exits thread
135
128k
      default:
136
128k
        lock.unlock();
137
128k
        RunRange(self, command, thread);
138
128k
        break;
139
191k
    }
140
191k
  }
141
62.8k
}
142
143
ThreadParallelRunner::ThreadParallelRunner(const int num_worker_threads)
144
62.8k
    : num_worker_threads_(num_worker_threads),
145
62.8k
      num_threads_(std::max(num_worker_threads, 1)) {
146
62.8k
  threads_.reserve(num_worker_threads_);
147
148
  // Suppress "unused-private-field" warning.
149
62.8k
  (void)padding1;
150
62.8k
  (void)padding2;
151
152
  // Safely handle spurious worker wakeups.
153
62.8k
  worker_start_command_ = kWorkerWait;
154
155
125k
  for (uint32_t i = 0; i < num_worker_threads_; ++i) {
156
62.8k
    threads_.emplace_back(ThreadFunc, this, i);
157
62.8k
  }
158
159
62.8k
  if (num_worker_threads_ != 0) {
160
62.8k
    WorkersReadyBarrier();
161
62.8k
  }
162
62.8k
}
163
164
62.8k
ThreadParallelRunner::~ThreadParallelRunner() {
165
62.8k
  if (num_worker_threads_ != 0) {
166
62.8k
    StartWorkers(kWorkerExit);
167
62.8k
  }
168
169
62.8k
  for (std::thread& thread : threads_) {
170
62.8k
    if (thread.joinable()) {
171
62.8k
      thread.join();
172
62.8k
    } else {
173
0
#if JXL_IS_DEBUG_BUILD
174
0
      JXL_PRINT_STACK_TRACE();
175
0
      JXL_CRASH();
176
0
#endif
177
0
    }
178
62.8k
  }
179
62.8k
}
180
}  // namespace jpegxl