jpegxl_sys\threads/resizable_parallel_runner.rs
1/*!
2This file is part of jpegxl-sys.
3
4jpegxl-sys is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 3 of the License, or
7(at your option) any later version.
8
9jpegxl-sys is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with jpegxl-sys. If not, see <https://www.gnu.org/licenses/>.
16*/
17
18//! Implementation using `std::thread` of a resizeable [`JxlParallelRunner`].
19
20//! Implementation of [`JxlParallelRunner`] than can be used to enable
21//! multithreading when using the JPEG XL library. This uses `std::thread`
22//! internally and related synchronization functions. The number of threads
23//! created can be changed after creation of the thread pool; the threads
24//! (including the main thread) are re-used for every
25//! `ResizableParallelRunner::Runner` call. Only one concurrent
26//! [`JxlResizableParallelRunner`] call per instance is allowed at a time.
27//!
28//! This is a scalable, lower-overhead thread pool runner, especially suitable
29//! for data-parallel computations in the fork-join model, where clients need to
30//! know when all tasks have completed.
31//!
32//! Compared to the implementation in [`super::thread_parallel_runner`], this
33//! implementation is tuned for execution on lower-powered systems, including
34//! for example ARM CPUs with big.LITTLE computation models.
35
36use std::ffi::c_void;
37
38#[cfg(doc)]
39use super::parallel_runner::JxlParallelRunner;
40use super::parallel_runner::{JxlParallelRetCode, JxlParallelRunFunction, JxlParallelRunInit};
41use crate::common::memory_manager::JxlMemoryManager;
42
43extern "C-unwind" {
44 /// Parallel runner internally using `std::thread`. Use as [`JxlParallelRunner`].
45 pub fn JxlResizableParallelRunner(
46 runner_opaque: *mut c_void,
47 jpegxl_opaque: *mut c_void,
48 init: JxlParallelRunInit,
49 func: JxlParallelRunFunction,
50 start_range: u32,
51 end_range: u32,
52 ) -> JxlParallelRetCode;
53
54 /// Creates the runner for [`JxlResizableParallelRunner`]. Use as the opaque
55 /// runner. The runner will execute tasks on the calling thread until
56 /// [`JxlResizableParallelRunnerSetThreads`] is called.
57 pub fn JxlResizableParallelRunnerCreate(memory_manager: *const JxlMemoryManager)
58 -> *mut c_void;
59
60 /// Changes the number of threads for [`JxlResizableParallelRunner`].
61 pub fn JxlResizableParallelRunnerSetThreads(runner_opaque: *mut c_void, num_threads: usize);
62
63 /// Suggests a number of threads to use for an image of given size.
64 pub fn JxlResizableParallelRunnerSuggestThreads(xsize: u64, ysize: u64) -> u32;
65
66 /// Destroys the runner created by [`JxlResizableParallelRunnerCreate`].
67 pub fn JxlResizableParallelRunnerDestroy(runner_opaque: *mut c_void);
68}