2016-09-19 02:59:46 +02:00
|
|
|
use core::{mem, str};
|
|
|
|
|
2016-09-19 04:17:08 +02:00
|
|
|
use arch::interrupt::irq::{COUNTS, acknowledge};
|
2016-09-19 02:59:46 +02:00
|
|
|
use context;
|
|
|
|
use syscall::{Error, Result};
|
|
|
|
use super::Scheme;
|
|
|
|
|
|
|
|
pub struct IrqScheme;
|
|
|
|
|
|
|
|
impl Scheme for IrqScheme {
|
|
|
|
fn open(&mut self, path: &[u8], _flags: usize) -> Result<usize> {
|
|
|
|
let path_str = str::from_utf8(path).or(Err(Error::NoEntry))?;
|
2016-09-19 04:17:08 +02:00
|
|
|
|
2016-09-19 02:59:46 +02:00
|
|
|
let id = path_str.parse::<usize>().or(Err(Error::NoEntry))?;
|
2016-09-19 04:17:08 +02:00
|
|
|
|
2016-09-19 02:59:46 +02:00
|
|
|
if id < COUNTS.lock().len() {
|
|
|
|
Ok(id)
|
|
|
|
} else {
|
|
|
|
Err(Error::NoEntry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dup(&mut self, file: usize) -> Result<usize> {
|
2016-09-19 04:17:08 +02:00
|
|
|
Err(Error::NotPermitted)
|
2016-09-19 02:59:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result<usize> {
|
|
|
|
// Ensures that the length of the buffer is larger than the size of a usize
|
|
|
|
if buffer.len() >= mem::size_of::<usize>() {
|
2016-09-19 04:17:08 +02:00
|
|
|
let prev = { COUNTS.lock()[file] };
|
2016-09-19 02:59:46 +02:00
|
|
|
loop {
|
2016-09-19 04:17:08 +02:00
|
|
|
{
|
|
|
|
let current = COUNTS.lock()[file];
|
|
|
|
if prev != current {
|
|
|
|
// Safe if the length of the buffer is larger than the size of a usize
|
|
|
|
assert!(buffer.len() >= mem::size_of::<usize>());
|
|
|
|
unsafe { *(buffer.as_mut_ptr() as *mut usize) = current; }
|
|
|
|
return Ok(mem::size_of::<usize>());
|
|
|
|
}
|
2016-09-19 02:59:46 +02:00
|
|
|
}
|
2016-09-19 04:17:08 +02:00
|
|
|
|
|
|
|
// Safe if all locks have been dropped
|
|
|
|
unsafe { context::switch(); }
|
2016-09-19 02:59:46 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 04:17:08 +02:00
|
|
|
fn write(&mut self, file: usize, buffer: &[u8]) -> Result<usize> {
|
|
|
|
if buffer.len() >= mem::size_of::<usize>() {
|
|
|
|
assert!(buffer.len() >= mem::size_of::<usize>());
|
|
|
|
let prev = unsafe { *(buffer.as_ptr() as *const usize) };
|
|
|
|
let current = COUNTS.lock()[file];
|
|
|
|
if prev == current {
|
|
|
|
unsafe { acknowledge(file); }
|
|
|
|
return Ok(mem::size_of::<usize>());
|
|
|
|
} else {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidValue)
|
|
|
|
}
|
2016-09-19 02:59:46 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 04:17:08 +02:00
|
|
|
fn fsync(&mut self, _file: usize) -> Result<()> {
|
2016-09-19 02:59:46 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(&mut self, file: usize) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|