Cleanup Redox repo, update Rust, remove old target
This commit is contained in:
7
src/sync/mod.rs
Normal file
7
src/sync/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
pub use self::wait_condition::WaitCondition;
|
||||
pub use self::wait_queue::WaitQueue;
|
||||
pub use self::wait_map::WaitMap;
|
||||
|
||||
pub mod wait_condition;
|
||||
pub mod wait_queue;
|
||||
pub mod wait_map;
|
||||
48
src/sync/wait_condition.rs
Normal file
48
src/sync/wait_condition.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use alloc::arc::Arc;
|
||||
use collections::Vec;
|
||||
use spin::{Mutex, RwLock};
|
||||
|
||||
use context::{self, Context};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WaitCondition {
|
||||
contexts: Mutex<Vec<Arc<RwLock<Context>>>>
|
||||
}
|
||||
|
||||
impl WaitCondition {
|
||||
pub fn new() -> WaitCondition {
|
||||
WaitCondition {
|
||||
contexts: Mutex::new(Vec::with_capacity(16))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn notify(&self) -> usize {
|
||||
let mut contexts = self.contexts.lock();
|
||||
let len = contexts.len();
|
||||
while let Some(context_lock) = contexts.pop() {
|
||||
context_lock.write().unblock();
|
||||
}
|
||||
len
|
||||
}
|
||||
|
||||
pub fn wait(&self) {
|
||||
{
|
||||
let context_lock = {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().expect("WaitCondition::wait: no context");
|
||||
context_lock.clone()
|
||||
};
|
||||
|
||||
context_lock.write().block();
|
||||
|
||||
self.contexts.lock().push(context_lock);
|
||||
}
|
||||
unsafe { context::switch(); }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WaitCondition {
|
||||
fn drop(&mut self){
|
||||
self.notify();
|
||||
}
|
||||
}
|
||||
62
src/sync/wait_map.rs
Normal file
62
src/sync/wait_map.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use collections::BTreeMap;
|
||||
use core::mem;
|
||||
use spin::Mutex;
|
||||
|
||||
use sync::WaitCondition;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WaitMap<K, V> {
|
||||
inner: Mutex<BTreeMap<K, V>>,
|
||||
condition: WaitCondition
|
||||
}
|
||||
|
||||
impl<K, V> WaitMap<K, V> where K: Clone + Ord {
|
||||
pub fn new() -> WaitMap<K, V> {
|
||||
WaitMap {
|
||||
inner: Mutex::new(BTreeMap::new()),
|
||||
condition: WaitCondition::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn receive_nonblock(&self, key: &K) -> Option<V> {
|
||||
self.inner.lock().remove(key)
|
||||
}
|
||||
|
||||
pub fn receive(&self, key: &K) -> V {
|
||||
loop {
|
||||
if let Some(value) = self.receive_nonblock(key) {
|
||||
return value;
|
||||
}
|
||||
self.condition.wait();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn receive_any_nonblock(&self) -> Option<(K, V)> {
|
||||
let mut inner = self.inner.lock();
|
||||
if let Some(key) = inner.keys().next().map(|key| key.clone()) {
|
||||
inner.remove(&key).map(|value| (key, value))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn receive_any(&self) -> (K, V) {
|
||||
loop {
|
||||
if let Some(entry) = self.receive_any_nonblock() {
|
||||
return entry;
|
||||
}
|
||||
self.condition.wait();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn receive_all(&self) -> BTreeMap<K, V> {
|
||||
let mut ret = BTreeMap::new();
|
||||
mem::swap(&mut ret, &mut *self.inner.lock());
|
||||
ret
|
||||
}
|
||||
|
||||
pub fn send(&self, key: K, value: V) {
|
||||
self.inner.lock().insert(key, value);
|
||||
self.condition.notify();
|
||||
}
|
||||
}
|
||||
82
src/sync/wait_queue.rs
Normal file
82
src/sync/wait_queue.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use collections::vec_deque::VecDeque;
|
||||
use spin::Mutex;
|
||||
|
||||
use sync::WaitCondition;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WaitQueue<T> {
|
||||
pub inner: Mutex<VecDeque<T>>,
|
||||
pub condition: WaitCondition,
|
||||
}
|
||||
|
||||
impl<T> WaitQueue<T> {
|
||||
pub fn new() -> WaitQueue<T> {
|
||||
WaitQueue {
|
||||
inner: Mutex::new(VecDeque::new()),
|
||||
condition: WaitCondition::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clone(&self) -> WaitQueue<T> where T: Clone {
|
||||
WaitQueue {
|
||||
inner: Mutex::new(self.inner.lock().clone()),
|
||||
condition: WaitCondition::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.inner.lock().is_empty()
|
||||
}
|
||||
|
||||
pub fn receive(&self) -> T {
|
||||
loop {
|
||||
if let Some(value) = self.inner.lock().pop_front() {
|
||||
return value;
|
||||
}
|
||||
self.condition.wait();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn receive_into(&self, buf: &mut [T], block: bool) -> usize {
|
||||
let mut i = 0;
|
||||
|
||||
if i < buf.len() && block {
|
||||
buf[i] = self.receive();
|
||||
i += 1;
|
||||
}
|
||||
|
||||
{
|
||||
let mut inner = self.inner.lock();
|
||||
while i < buf.len() {
|
||||
if let Some(value) = inner.pop_front() {
|
||||
buf[i] = value;
|
||||
i += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
i
|
||||
}
|
||||
|
||||
pub fn send(&self, value: T) -> usize {
|
||||
let len = {
|
||||
let mut inner = self.inner.lock();
|
||||
inner.push_back(value);
|
||||
inner.len()
|
||||
};
|
||||
self.condition.notify();
|
||||
len
|
||||
}
|
||||
|
||||
pub fn send_from(&self, buf: &[T]) -> usize where T: Copy {
|
||||
let len = {
|
||||
let mut inner = self.inner.lock();
|
||||
inner.extend(buf.iter());
|
||||
inner.len()
|
||||
};
|
||||
self.condition.notify();
|
||||
len
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user