aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-platform-linux / src / queue.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Priority-deadline concurrent WorkQueue for the Linux platform. |
| 17 | |
| 18 | use aien_platform::{InferenceWork, QueueError, WorkQueue}; |
| 19 | use parking_lot::Mutex; |
| 20 | use std::cmp::Ordering; |
| 21 | use std::collections::BinaryHeap; |
| 22 | |
| 23 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 24 | struct ScheduledWorkItem(InferenceWork); |
| 25 | |
| 26 | impl Ord for ScheduledWorkItem { |
| 27 | fn cmp(&self, other: &Self) -> Ordering { |
| 28 | match self.0.priority.cmp(&other.0.priority) { |
| 29 | Ordering::Equal => match (self.0.deadline, other.0.deadline) { |
| 30 | (Some(d1), Some(d2)) => d2.cmp(&d1), |
| 31 | (Some(_), None) => Ordering::Greater, |
| 32 | (None, Some(_)) => Ordering::Less, |
| 33 | (None, None) => Ordering::Equal, |
| 34 | }, |
| 35 | ord => ord, |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl PartialOrd for ScheduledWorkItem { |
| 41 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 42 | Some(self.cmp(other)) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub struct LinuxWorkQueue { |
| 47 | capacity: usize, |
| 48 | items: Mutex<BinaryHeap<ScheduledWorkItem>>, |
| 49 | } |
| 50 | |
| 51 | impl LinuxWorkQueue { |
| 52 | pub fn new(capacity: usize) -> Self { |
| 53 | Self { |
| 54 | capacity, |
| 55 | items: Mutex::new(BinaryHeap::with_capacity(capacity)), |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pub fn with_default_capacity() -> Self { |
| 60 | Self::new(4096) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl Default for LinuxWorkQueue { |
| 65 | fn default() -> Self { |
| 66 | Self::with_default_capacity() |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl WorkQueue for LinuxWorkQueue { |
| 71 | fn push(&self, work: InferenceWork) -> Result<(), QueueError> { |
| 72 | let mut heap = self.items.lock(); |
| 73 | if heap.len() >= self.capacity { |
| 74 | return Err(QueueError::Full); |
| 75 | } |
| 76 | heap.push(ScheduledWorkItem(work)); |
| 77 | Ok(()) |
| 78 | } |
| 79 | |
| 80 | fn pop(&self) -> Option<InferenceWork> { |
| 81 | let mut heap = self.items.lock(); |
| 82 | heap.pop().map(|item| item.0) |
| 83 | } |
| 84 | |
| 85 | fn len(&self) -> usize { |
| 86 | self.items.lock().len() |
| 87 | } |
| 88 | } |