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
/*
 * // Copyright (c) the Radzivon Bartoshyk. All rights reserved.
 * //
 * // Use of this source code is governed by a BSD-style
 * // license that can be found in the LICENSE file.
 */

use rayon::ThreadPool;
use crate::ImageSize;

#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum ThreadingPolicy {
    Single,
    Fixed(usize),
    Adaptive,
}

impl<'a> ThreadingPolicy {
    pub fn get_threads_count(&self, for_size: ImageSize) -> usize {
        match self {
            ThreadingPolicy::Single => 1,
            ThreadingPolicy::Fixed(thread_count) => (*thread_count).max(1),
            ThreadingPolicy::Adaptive => {
                let box_size = 256 * 256;
                let new_box_size = for_size.height * for_size.width;
                return (new_box_size / box_size).max(1).min(16);
            }
        }
    }

}

impl<'a> ThreadingPolicy {
    pub fn get_pool(&self, for_size: ImageSize) -> Option<ThreadPool> {
        if *self == ThreadingPolicy::Single {
            return None;
        }
        let threads_count = self.get_threads_count(for_size);
        let shared_pool = rayon::ThreadPoolBuilder::new()
            .num_threads(threads_count)
            .use_current_thread()
            .build()
            .unwrap();
        return Some(shared_pool);
    }
}