pub mod metrics;
pub mod tracing;
pub mod health;
pub mod autoscale;
pub mod ai;
pub mod digital_twin;
pub mod edge;
pub mod quantum;
pub mod neuro;
pub mod blockchain;
pub mod crypto;
pub mod genomic;
pub use metrics::{Metrics, MetricsSnapshot};
pub use tracing::{TraceContext, Tracer, Span, CompletedSpan};
pub use health::{HealthCheck, HealthStatus, HealthReport};
pub use autoscale::{AutoScaler, ScalingConfig, ScalingDecision, ResourceLimits};
pub use ai::{WorkloadPredictor, AnomalyDetector, PerformanceOptimizer};
pub use digital_twin::{DigitalTwin, TwinSnapshot, TwinUpdate};
pub use edge::{EdgeManager, EdgeNode, DistributionStrategy, TaskDistribution};
pub use quantum::{QuantumScheduler, SchedulingDecision, QuantumStats};
pub use neuro::{NeuralNetwork, RecurrentNetwork, NetworkStats};
pub use blockchain::{RuntimeBlockchain, Block, Transaction, TransactionType, ConsensusManager};
pub use crypto::{CryptoService, SecureChannel, CryptoStats};
pub use genomic::{GeneticOptimizer, Genome, GeneticStats};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll, Wake};
use std::sync::{Arc, Mutex, Condvar, atomic::{AtomicBool, AtomicUsize, Ordering}};
use std::collections::VecDeque;
use std::thread;
use std::time::{Duration, Instant};
type Task = Pin<Box<dyn Future<Output = ()> + Send>>;
#[derive(Clone, Debug)]
pub struct RuntimeConfig {
pub num_threads: Option<usize>,
pub enable_autoscaling: bool,
pub scaling_config: ScalingConfig,
pub resource_limits: ResourceLimits,
}
impl Default for RuntimeConfig {
fn default() -> Self {
Self {
num_threads: None,
enable_autoscaling: false,
scaling_config: ScalingConfig::default(),
resource_limits: ResourceLimits::default(),
}
}
}
pub struct JoinHandle<T> {
result: Arc<Mutex<Option<T>>>,
completed: Arc<AtomicBool>,
}
impl<T> JoinHandle<T> {
pub async fn await_result(self) -> Option<T> {
while !self.completed.load(Ordering::Acquire) {
yield_now().await;
}
self.result.lock().unwrap().take()
}
}
pub struct Runtime {
queue: Arc<Mutex<VecDeque<Task>>>,
shutdown: Arc<AtomicBool>,
task_count: Arc<AtomicUsize>,
condvar: Arc<Condvar>,
metrics: Metrics,
health: HealthCheck,
tracer: Tracer,
#[allow(dead_code)]
autoscaler: Option<AutoScaler>,
resource_limits: ResourceLimits,
}
impl Runtime {
pub fn new() -> Self {
Self::with_config(RuntimeConfig::default())
}
pub fn with_config(config: RuntimeConfig) -> Self {
let metrics = Metrics::new();
let health = HealthCheck::new();
let tracer = Tracer::new();
let autoscaler = if config.enable_autoscaling {
Some(AutoScaler::new(config.scaling_config))
} else {
None
};
let num_threads = config.num_threads.unwrap_or_else(|| {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(4)
});
metrics.set_thread_count(num_threads);
Self {
queue: Arc::new(Mutex::new(VecDeque::new())),
shutdown: Arc::new(AtomicBool::new(false)),
task_count: Arc::new(AtomicUsize::new(0)),
condvar: Arc::new(Condvar::new()),
metrics,
health,
tracer,
autoscaler,
resource_limits: config.resource_limits,
}
}
pub fn metrics(&self) -> &Metrics {
&self.metrics
}
pub fn health(&self) -> &HealthCheck {
&self.health
}
pub fn tracer(&self) -> &Tracer {
&self.tracer
}
pub fn task_count(&self) -> usize {
self.task_count.load(Ordering::Relaxed)
}
pub fn shutdown(&self) {
self.shutdown.store(true, Ordering::Release);
self.health.set_ready(false);
self.condvar.notify_all();
}
pub fn spawn<F>(&self, future: F)
where
F: Future<Output = ()> + Send + 'static,
{
let queue_len = {
let queue = self.queue.lock().unwrap();
queue.len()
};
if self.resource_limits.is_queue_size_exceeded(queue_len) {
self.health.add_check(
"queue_limit",
HealthStatus::Degraded,
format!("Queue size {} exceeds limit", queue_len),
);
return;
}
self.metrics.task_spawned();
self.task_count.fetch_add(1, Ordering::Relaxed);
let task_count = Arc::clone(&self.task_count);
let condvar = Arc::clone(&self.condvar);
let metrics = self.metrics.clone();
let start_time = Instant::now();
let wrapped = async move {
future.await;
let execution_time = start_time.elapsed();
metrics.task_completed(execution_time);
task_count.fetch_sub(1, Ordering::Relaxed);
condvar.notify_all();
};
let mut queue = self.queue.lock().unwrap();
queue.push_back(Box::pin(wrapped));
self.metrics.queue_length_changed(queue.len());
self.condvar.notify_one();
}
pub fn spawn_with_handle<F, T>(&self, future: F) -> JoinHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
let result = Arc::new(Mutex::new(None));
let completed = Arc::new(AtomicBool::new(false));
let result_clone = Arc::clone(&result);
let completed_clone = Arc::clone(&completed);
let task = async move {
let output = future.await;
*result_clone.lock().unwrap() = Some(output);
completed_clone.store(true, Ordering::Release);
};
self.spawn(task);
JoinHandle { result, completed }
}
pub fn block_on<F, T>(&self, future: F) -> T
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
let result = Arc::new(Mutex::new(None));
let result_clone = Arc::clone(&result);
let task = async move {
let output = future.await;
*result_clone.lock().unwrap() = Some(output);
};
self.spawn(Box::pin(task));
self.run();
Arc::try_unwrap(result)
.ok()
.and_then(|m| m.into_inner().ok())
.and_then(|opt| opt)
.expect("Task did not complete")
}
fn run(&self) {
let num_threads = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(4);
self.health.set_alive(true);
self.health.set_ready(true);
let mut handles = vec![];
for _thread_id in 0..num_threads {
let queue = Arc::clone(&self.queue);
let shutdown = Arc::clone(&self.shutdown);
let task_count = Arc::clone(&self.task_count);
let condvar = Arc::clone(&self.condvar);
let metrics = self.metrics.clone();
let health = self.health.clone();
let handle = thread::spawn(move || {
let waker = Arc::new(RuntimeWaker { condvar: Arc::clone(&condvar) }).into();
loop {
health.heartbeat();
if shutdown.load(Ordering::Acquire) && task_count.load(Ordering::Relaxed) == 0 {
break;
}
let task = {
let mut q = queue.lock().unwrap();
if q.is_empty() && !shutdown.load(Ordering::Acquire) {
metrics.thread_idle();
q = condvar.wait_timeout(q, Duration::from_millis(100)).unwrap().0;
metrics.thread_active();
}
let task = q.pop_front();
metrics.queue_length_changed(q.len());
task
};
match task {
Some(mut task) => {
metrics.thread_active();
let mut context = Context::from_waker(&waker);
match task.as_mut().poll(&mut context) {
Poll::Ready(()) => {},
Poll::Pending => {
let mut q = queue.lock().unwrap();
q.push_back(task);
metrics.queue_length_changed(q.len());
}
}
}
None if shutdown.load(Ordering::Acquire) => break,
None => {}
}
}
});
handles.push(handle);
}
for handle in handles {
let _ = handle.join();
}
self.health.set_alive(false);
}
}
impl Default for Runtime {
fn default() -> Self {
Self::new()
}
}
struct RuntimeWaker {
condvar: Arc<Condvar>,
}
impl Wake for RuntimeWaker {
fn wake(self: Arc<Self>) {
self.condvar.notify_one();
}
fn wake_by_ref(self: &Arc<Self>) {
self.condvar.notify_one();
}
}
pub fn spawn<F>(future: F)
where
F: Future<Output = ()> + Send + 'static,
{
RUNTIME.with(|rt| {
rt.borrow().spawn(future);
});
}
thread_local! {
static RUNTIME: std::cell::RefCell<Runtime> = std::cell::RefCell::new(Runtime::new());
}
#[macro_export]
macro_rules! main {
($($body:tt)*) => {
fn main() {
let rt = $crate::Runtime::new();
rt.block_on(async { $($body)* });
}
};
}
pub async fn yield_now() {
struct YieldNow {
yielded: bool,
}
impl Future for YieldNow {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.yielded {
Poll::Ready(())
} else {
self.yielded = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
YieldNow { yielded: false }.await
}
pub async fn sleep(duration: Duration) {
struct Sleep {
when: std::time::Instant,
}
impl Future for Sleep {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if std::time::Instant::now() >= self.when {
Poll::Ready(())
} else {
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
Sleep {
when: std::time::Instant::now() + duration,
}
.await
}
pub async fn timeout<F, T>(duration: Duration, future: F) -> Result<T, TimeoutError>
where
F: Future<Output = T>,
{
struct Timeout<F> {
future: Pin<Box<F>>,
deadline: Instant,
}
impl<F: Future> Future for Timeout<F> {
type Output = Result<F::Output, TimeoutError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if Instant::now() >= self.deadline {
return Poll::Ready(Err(TimeoutError));
}
match self.future.as_mut().poll(cx) {
Poll::Ready(v) => Poll::Ready(Ok(v)),
Poll::Pending => {
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
}
Timeout {
future: Box::pin(future),
deadline: Instant::now() + duration,
}
.await
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeoutError;
impl std::fmt::Display for TimeoutError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "operation timed out")
}
}
impl std::error::Error for TimeoutError {}
pub mod channel {
use std::sync::{Arc, Mutex, Condvar};
use std::collections::VecDeque;
pub fn bounded<T>(capacity: usize) -> (Sender<T>, Receiver<T>) {
let inner = Arc::new(ChannelInner {
queue: Mutex::new(VecDeque::with_capacity(capacity)),
condvar: Condvar::new(),
capacity,
closed: Mutex::new(false),
});
(Sender { inner: inner.clone() }, Receiver { inner })
}
pub fn unbounded<T>() -> (Sender<T>, Receiver<T>) {
bounded(usize::MAX)
}
struct ChannelInner<T> {
queue: Mutex<VecDeque<T>>,
condvar: Condvar,
capacity: usize,
closed: Mutex<bool>,
}
pub struct Sender<T> {
inner: Arc<ChannelInner<T>>,
}
impl<T> Sender<T> {
pub async fn send(&self, value: T) -> Result<(), SendError<T>> {
if *self.inner.closed.lock().unwrap() {
return Err(SendError(value));
}
loop {
let mut queue = self.inner.queue.lock().unwrap();
if queue.len() < self.inner.capacity {
queue.push_back(value);
self.inner.condvar.notify_one();
return Ok(());
}
drop(queue);
let queue = self.inner.queue.lock().unwrap();
let _guard = self.inner.condvar.wait(queue).unwrap();
}
}
}
impl<T> Clone for Sender<T> {
fn clone(&self) -> Self {
Self { inner: self.inner.clone() }
}
}
impl<T> Drop for Sender<T> {
fn drop(&mut self) {
if Arc::strong_count(&self.inner) == 2 {
*self.inner.closed.lock().unwrap() = true;
self.inner.condvar.notify_all();
}
}
}
pub struct Receiver<T> {
inner: Arc<ChannelInner<T>>,
}
impl<T> Receiver<T> {
pub async fn recv(&self) -> Option<T> {
loop {
let mut queue = self.inner.queue.lock().unwrap();
if let Some(value) = queue.pop_front() {
self.inner.condvar.notify_one();
return Some(value);
}
if *self.inner.closed.lock().unwrap() && queue.is_empty() {
return None;
}
drop(queue);
let queue = self.inner.queue.lock().unwrap();
let _guard = self.inner.condvar.wait(queue).unwrap();
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SendError<T>(pub T);
impl<T> std::fmt::Display for SendError<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "channel closed")
}
}
impl<T: std::fmt::Debug> std::error::Error for SendError<T> {}
}
pub mod net {
use std::io;
use std::net::{TcpListener as StdListener, TcpStream as StdStream, SocketAddr};
pub struct TcpListener(StdListener);
pub struct TcpStream(StdStream);
impl TcpListener {
pub async fn bind(addr: SocketAddr) -> io::Result<Self> {
let listener = StdListener::bind(addr)?;
listener.set_nonblocking(true)?;
Ok(Self(listener))
}
pub async fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
loop {
match self.0.accept() {
Ok((stream, addr)) => {
stream.set_nonblocking(true)?;
return Ok((TcpStream(stream), addr));
}
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
crate::sleep(std::time::Duration::from_millis(10)).await;
}
Err(e) => return Err(e),
}
}
}
}
impl TcpStream {
pub async fn connect(addr: SocketAddr) -> io::Result<Self> {
let stream = StdStream::connect(addr)?;
stream.set_nonblocking(true)?;
Ok(Self(stream))
}
pub fn into_std(self) -> StdStream {
self.0
}
pub fn as_std(&self) -> &StdStream {
&self.0
}
pub async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
use std::io::Read;
loop {
match self.0.read(buf) {
Ok(n) => return Ok(n),
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
crate::sleep(std::time::Duration::from_millis(1)).await;
}
Err(e) => return Err(e),
}
}
}
pub async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
use std::io::Write;
loop {
match self.0.write(buf) {
Ok(n) => return Ok(n),
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
crate::sleep(std::time::Duration::from_millis(1)).await;
}
Err(e) => return Err(e),
}
}
}
pub async fn write_all(&mut self, mut buf: &[u8]) -> io::Result<()> {
while !buf.is_empty() {
let n = self.write(buf).await?;
buf = &buf[n..];
}
Ok(())
}
}
}
pub mod io {
use std::io::{self, Read, Write};
pub async fn copy<R: Read, W: Write>(reader: &mut R, writer: &mut W) -> io::Result<u64> {
let mut buf = [0u8; 8192];
let mut total = 0u64;
loop {
match reader.read(&mut buf) {
Ok(0) => return Ok(total),
Ok(n) => {
writer.write_all(&buf[..n])?;
total += n as u64;
}
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
crate::sleep(std::time::Duration::from_millis(1)).await;
}
Err(e) => return Err(e),
}
}
}
}