pub mod futures;
pub mod channels;
pub mod parallel;
pub mod sync;
pub mod actors;
pub mod distributed;
pub mod scheduler;
pub mod concurrency_runtime;
pub mod mutex;
pub mod rwlock;
pub mod semaphore;
pub mod condvar;
pub mod barrier;
pub mod atomic_ref;
pub mod lockfree_queue;
pub mod atomic_primitives;
pub mod sync_registry;
#[cfg(test)]
mod tests;
pub use concurrency_runtime::*;
pub use mutex::{Mutex, MutexGuard};
pub use rwlock::{RwLock, ReadGuard, WriteGuard};
pub use semaphore::{SemaphoreSync, SemaphorePermit};
pub use condvar::CondVar;
pub use barrier::{Barrier, BarrierWaitResult};
pub use atomic_ref::AtomicRef;
pub use lockfree_queue::{LockFreeQueue, BoundedLockFreeQueue};
pub use atomic_primitives::{AtomicCounter, AtomicFlag};
pub use sync_registry::{SyncRegistry, global_sync_registry};
use crate::diagnostics::{Error, Result, LambdustError};
#[derive(Debug)]
pub enum ConcurrencyError {
ChannelClosed,
Timeout,
Cancelled,
Deadlock,
ActorNotFound(String),
Serialization(String),
Network(String),
}
impl std::fmt::Display for ConcurrencyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ChannelClosed => write!(f, "Channel closed"),
Self::Timeout => write!(f, "Timeout expired"),
Self::Cancelled => write!(f, "Task cancelled"),
Self::Deadlock => write!(f, "Deadlock detected"),
Self::ActorNotFound(name) => write!(f, "Actor not found: {}", name),
Self::Serialization(msg) => write!(f, "Serialization error: {}", msg),
Self::Network(msg) => write!(f, "Network error: {}", msg),
}
}
}
impl LambdustError for ConcurrencyError {
fn error_code(&self) -> &'static str {
match self {
Self::ChannelClosed => "lambdust::concurrency::channel_closed",
Self::Timeout => "lambdust::concurrency::timeout",
Self::Cancelled => "lambdust::concurrency::cancelled",
Self::Deadlock => "lambdust::concurrency::deadlock",
Self::ActorNotFound(_) => "lambdust::concurrency::actor_not_found",
Self::Serialization(_) => "lambdust::concurrency::serialization",
Self::Network(_) => "lambdust::concurrency::network",
}
}
}
impl std::error::Error for ConcurrencyError {}
impl From<ConcurrencyError> for Error {
fn from(err: ConcurrencyError) -> Self {
Error::runtime_error(err.to_string(), None)
}
}
impl From<ConcurrencyError> for Box<Error> {
fn from(err: ConcurrencyError) -> Self {
Error::from(err).into()
}
}
impl ConcurrencyError {
pub fn boxed(self) -> Box<Error> {
Box::new(Error::from(self))
}
}
pub fn initialize() -> Result<()> {
let _runtime = ConcurrencyRuntime::global();
actors::initialize()?;
scheduler::initialize()?;
Ok(())
}
pub async fn shutdown() -> Result<()> {
actors::shutdown().await?;
scheduler::shutdown().await?;
Ok(())
}