Coverage Report

Created: 2025-07-16 07:53

/src/libjxl/lib/threads/thread_parallel_runner.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 <jxl/memory_manager.h>
7
#include <jxl/parallel_runner.h>
8
#include <jxl/thread_parallel_runner.h>
9
#include <string.h>
10
11
#include <cstdint>
12
#include <cstdlib>
13
#include <thread>
14
15
#include "lib/threads/thread_parallel_runner_internal.h"
16
17
namespace {
18
19
// Default JxlMemoryManager using malloc and free for the jpegxl_threads
20
// library. Same as the default JxlMemoryManager for the jpegxl library
21
// itself.
22
23
// Default alloc and free functions.
24
981
void* ThreadMemoryManagerDefaultAlloc(void* opaque, size_t size) {
25
981
  return malloc(size);
26
981
}
27
28
981
void ThreadMemoryManagerDefaultFree(void* opaque, void* address) {
29
981
  free(address);
30
981
}
31
32
// Initializes the memory manager instance with the passed one. The
33
// MemoryManager passed in |memory_manager| may be NULL or contain NULL
34
// functions which will be initialized with the default ones. If either alloc
35
// or free are NULL, then both must be NULL, otherwise this function returns an
36
// error.
37
bool ThreadMemoryManagerInit(JxlMemoryManager* self,
38
981
                             const JxlMemoryManager* memory_manager) {
39
981
  if (memory_manager) {
40
0
    *self = *memory_manager;
41
981
  } else {
42
981
    memset(self, 0, sizeof(*self));
43
981
  }
44
981
  bool is_default_alloc = (self->alloc == nullptr);
45
981
  bool is_default_free = (self->free == nullptr);
46
981
  if (is_default_alloc != is_default_free) {
47
0
    return false;
48
0
  }
49
981
  if (is_default_alloc) self->alloc = ThreadMemoryManagerDefaultAlloc;
50
981
  if (is_default_free) self->free = ThreadMemoryManagerDefaultFree;
51
52
981
  return true;
53
981
}
54
55
void* ThreadMemoryManagerAlloc(const JxlMemoryManager* memory_manager,
56
981
                               size_t size) {
57
981
  return memory_manager->alloc(memory_manager->opaque, size);
58
981
}
59
60
void ThreadMemoryManagerFree(const JxlMemoryManager* memory_manager,
61
981
                             void* address) {
62
981
  memory_manager->free(memory_manager->opaque, address);
63
981
}
64
65
}  // namespace
66
67
JxlParallelRetCode JxlThreadParallelRunner(
68
    void* runner_opaque, void* jpegxl_opaque, JxlParallelRunInit init,
69
23.9k
    JxlParallelRunFunction func, uint32_t start_range, uint32_t end_range) {
70
23.9k
  return jpegxl::ThreadParallelRunner::Runner(
71
23.9k
      runner_opaque, jpegxl_opaque, init, func, start_range, end_range);
72
23.9k
}
73
74
/// Starts the given number of worker threads and blocks until they are ready.
75
/// "num_worker_threads" defaults to one per hyperthread. If zero, all tasks
76
/// run on the main thread.
77
void* JxlThreadParallelRunnerCreate(const JxlMemoryManager* memory_manager,
78
981
                                    size_t num_worker_threads) {
79
981
  JxlMemoryManager local_memory_manager;
80
981
  if (!ThreadMemoryManagerInit(&local_memory_manager, memory_manager))
81
0
    return nullptr;
82
83
981
  void* alloc = ThreadMemoryManagerAlloc(&local_memory_manager,
84
981
                                         sizeof(jpegxl::ThreadParallelRunner));
85
981
  if (!alloc) return nullptr;
86
  // Placement new constructor on allocated memory
87
981
  jpegxl::ThreadParallelRunner* runner =
88
981
      new (alloc) jpegxl::ThreadParallelRunner(num_worker_threads);
89
981
  runner->memory_manager = local_memory_manager;
90
91
981
  return runner;
92
981
}
93
94
981
void JxlThreadParallelRunnerDestroy(void* runner_opaque) {
95
981
  jpegxl::ThreadParallelRunner* runner =
96
981
      reinterpret_cast<jpegxl::ThreadParallelRunner*>(runner_opaque);
97
981
  if (runner) {
98
981
    JxlMemoryManager local_memory_manager = runner->memory_manager;
99
    // Call destructor directly since custom free function is used.
100
981
    runner->~ThreadParallelRunner();
101
981
    ThreadMemoryManagerFree(&local_memory_manager, runner);
102
981
  }
103
981
}
104
105
// Get default value for num_worker_threads parameter of
106
// InitJxlThreadParallelRunner.
107
0
size_t JxlThreadParallelRunnerDefaultNumWorkerThreads() {
108
0
  return std::thread::hardware_concurrency();
109
0
}