#![warn(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(unsafe_op_in_unsafe_fn)]
pub mod consumer;
pub mod copy;
pub mod error;
pub mod layout;
mod mapping;
pub mod producer;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
pub use consumer::Consumer;
pub use error::{Error, Result};
pub use producer::Producer;
#[derive(Clone)]
pub struct Queue {
map: Arc<mapping::Mapping>,
producer_taken: Arc<AtomicBool>,
}
impl Queue {
pub fn create(path: impl AsRef<Path>, capacity: u64) -> Result<Self> {
Ok(Self::wrap(mapping::Mapping::create(
path.as_ref(),
capacity,
)?))
}
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
Ok(Self::wrap(mapping::Mapping::open(path.as_ref())?))
}
pub fn anon(capacity: u64) -> Result<Self> {
Ok(Self::wrap(mapping::Mapping::anon(capacity)?))
}
fn wrap(map: mapping::Mapping) -> Self {
Self {
map: Arc::new(map),
producer_taken: Arc::new(AtomicBool::new(false)),
}
}
pub fn capacity(&self) -> u64 {
self.map.capacity()
}
pub fn instance(&self) -> u64 {
self.map.instance()
}
pub fn producer(&self) -> Result<Producer> {
if self.producer_taken.swap(true, Ordering::AcqRel) {
return Err(Error::Io(std::io::Error::new(
std::io::ErrorKind::AlreadyExists,
"this queue already has a producer",
)));
}
if !self.map.claim_producer() {
self.producer_taken.store(false, Ordering::Release);
return Err(Error::Io(std::io::Error::new(
std::io::ErrorKind::AlreadyExists,
"another handle or process is already the producer for this queue",
)));
}
Ok(Producer::new(
Arc::clone(&self.map),
Arc::clone(&self.producer_taken),
))
}
pub fn consumer(&self) -> Consumer {
Consumer::new(Arc::clone(&self.map))
}
}