blob: b128846a4d1b079f2062273483681ae393a3a680 (
plain)
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
|
use crate::types::*;
use anyhow::Result;
use hal::queue::family::QueueFamilyId;
use std::sync::{Arc, RwLock};
use thiserror::Error;
pub struct QueueNegotiator {
family_id: QueueFamilyId,
already_allocated: Vec<Arc<RwLock<QueueT>>>,
next_share: usize,
}
impl QueueNegotiator {
pub fn find<F: FnMut(&&QueueFamilyT) -> bool>(adapter: &Adapter, filter: F) -> Result<Self> {
let family = adapter
.queue_families
.iter()
.find(filter)
.ok_or(QueueNegotiatorError::NoSuitableFamilies)?;
Ok(QueueNegotiator {
family_id: family.id(),
already_allocated: Vec::with_capacity(family.max_queues()),
next_share: 0,
})
}
pub fn family<'a>(&self, adapter: &'a Adapter) -> &'a QueueFamilyT {
adapter
.queue_families
.iter()
.find(|x| x.id() == self.family_id)
.unwrap()
}
pub fn family_id(&self) -> QueueFamilyId {
self.family_id
}
pub fn get_queue(&mut self, groups: &mut Vec<QueueGroup>) -> Option<Arc<RwLock<QueueT>>> {
match groups
.iter()
.position(|x| x.queues.len() > 0 && x.family == self.family_id)
{
Some(idx) => {
// At least one remaining queue
let queue = groups[idx].queues.pop().unwrap();
let queue = Arc::new(RwLock::new(queue));
self.already_allocated.push(queue.clone());
Some(queue)
}
None => {
if self.already_allocated.len() == 0 {
return None;
}
let queue = self.already_allocated[self.next_share].clone();
self.next_share = (self.next_share + 1) % self.already_allocated.len();
Some(queue)
}
}
}
}
#[derive(Error, Debug)]
pub enum QueueNegotiatorError {
#[error("No suitable queue families found")]
NoSuitableFamilies,
}
|