1use crate::*;
2use std::sync::atomic::AtomicUsize;
3
4pub struct ReadWorkers {
5 workers: Vec<ReadWorker>,
6 index: AtomicUsize,
7}
8
9impl ReadWorkers {
10 pub fn start(config: &RingConfig, num_threads: usize) -> Result<Self> {
11 if num_threads == 0 {
12 return Err(Error::InvalidArgument);
13 }
14 let mut workers = Vec::with_capacity(num_threads);
15 for _ in 0..num_threads {
16 workers.push(ReadWorker::start(config)?);
17 }
18 Ok(Self {
19 workers,
20 index: Default::default(),
21 })
22 }
23
24 pub fn enqueue(&self, job: BatchReadJobs) {
25 let index = self.index.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
26 self.workers[index % self.workers.len()].enqueue(job);
27 }
28
29 pub fn config(&self) -> &RingConfig {
30 self.workers[0].config()
31 }
32}
33
34impl std::fmt::Debug for ReadWorkers {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 f.debug_struct("ReadWorkers")
37 .field("config", &self.config())
38 .field("num_threads", &self.workers.len())
39 .field("index", &self.index)
40 .finish()
41 }
42}