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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
This file is part of jpegxl-rs.

jpegxl-rs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

jpegxl-rs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with jpegxl-rs.  If not, see <https://www.gnu.org/licenses/>.
*/

//! Wrapper for default thread pool implementation with C++ standard library

#![cfg(feature = "threads")]
#![cfg_attr(docsrs, doc(cfg(feature = "threads")))]

use std::{ffi::c_void, ptr::null_mut};

#[allow(clippy::wildcard_imports)]
use jpegxl_sys::thread_parallel_runner::*;

use super::{JxlParallelRunner, RunnerFn};

use crate::memory::MemoryManager;

/// Wrapper for default thread pool implementation with C++ standard library
pub struct ThreadsRunner<'mm> {
    runner_ptr: *mut c_void,
    _memory_manager: Option<&'mm dyn MemoryManager>,
}

impl<'mm> ThreadsRunner<'mm> {
    /// Construct with number of threads
    #[must_use]
    pub fn new(
        memory_manager: Option<&'mm dyn MemoryManager>,
        num_workers: Option<usize>,
    ) -> Option<Self> {
        let mm = memory_manager.map(MemoryManager::manager);
        let runner_ptr = unsafe {
            JxlThreadParallelRunnerCreate(
                mm.as_ref().map_or(null_mut(), |mm| mm),
                num_workers.unwrap_or_else(|| JxlThreadParallelRunnerDefaultNumWorkerThreads()),
            )
        };

        if runner_ptr.is_null() {
            None
        } else {
            Some(Self {
                runner_ptr,
                _memory_manager: memory_manager,
            })
        }
    }
}

impl Default for ThreadsRunner<'_> {
    fn default() -> Self {
        Self {
            runner_ptr: unsafe {
                JxlThreadParallelRunnerCreate(
                    std::ptr::null(),
                    JxlThreadParallelRunnerDefaultNumWorkerThreads(),
                )
            },
            _memory_manager: None,
        }
    }
}

impl JxlParallelRunner for ThreadsRunner<'_> {
    fn runner(&self) -> RunnerFn {
        JxlThreadParallelRunner
    }

    fn as_opaque_ptr(&self) -> *mut c_void {
        self.runner_ptr
    }
}

impl Drop for ThreadsRunner<'_> {
    fn drop(&mut self) {
        unsafe { JxlThreadParallelRunnerDestroy(self.runner_ptr) };
    }
}

#[cfg(test)]
mod tests {
    use crate::memory::tests::{BumpManager, NoManager};

    use super::*;

    #[test]
    fn test_construction() {
        let memory_manager = NoManager {};
        let parallel_runner = ThreadsRunner::new(Some(&memory_manager), None);
        assert!(parallel_runner.is_none());

        let memory_manager = BumpManager::<1024>::default();
        let parallel_runner = ThreadsRunner::new(Some(&memory_manager), Some(10));
        assert!(parallel_runner.is_some());
    }
}