Changeset - 50630ed25c60
[Not reviewed]
Cargo.toml
Show inline comments
 
[package]
 
name = "reowolf_rs"
 
version = "0.1.3"
 
version = "0.1.4"
 
authors = [
 
	"Christopher Esterhuyse <esterhuy@cwi.nl, christopher.esterhuyse@gmail.com>",
 
	"Hans-Dieter Hiep <hdh@cwi.nl>"
 
]
 
edition = "2018"
 

	
 
[dependencies]
 
# convenience macros
 
maplit = "1.0.2"
 
derive_more = "0.99.2"
 

	
 
# runtime
 
bincode = "1.2.1"
 
serde = { version = "1.0.112", features = ["derive"] }
 
getrandom = "0.1.14" # tiny crate. used to guess controller-id
 
take_mut = "0.2.2"
 
indexmap = "1.3.0" # hashsets/hashmaps with efficient arbitrary element removal
 
replace_with = "0.1.5"
 

	
 
# network
 
integer-encoding = "1.0.7"
 
byteorder = "1.3.2"
 
# mio = "0.6.21"
 
mio-extras = "2.0.6"
 
mio = { version = "0.7.0", package = "mio", features = ["tcp", "os-poll"] }
 

	
 
# protocol
 
# id-arena = "2.2.1"
 
backtrace = "0.3"
 

	
 
[dev-dependencies]
 
test-generator = "0.3.0"
 
crossbeam-utils = "0.7.0"
 
lazy_static = "1.4.0"
 

	
 
[lib]
 
# compile target: dynamically linked library using C ABI
 
crate-type = ["cdylib"]
 

	
 
[features]
 
default = ["ffi"]
 
ffi = [] # no feature dependencies
 
\ No newline at end of file
README.md
Show inline comments
 
# Reowolf 1.0 Implementation
 

	
 
## Compilation instructions
 
1. Install the latest stable Rust toolchain using Rustup. See https://rustup.rs/ for further instructions.
 
2. Run `cargo build --release` to download source dependencies, and compile the library with release-level optimizations. 
 
	- The resulting dylib can be found in target/release/, to be used with the header file reowolf.h.
 
	- Note: A list of immediate ancestor dependencies is visible in Cargo.toml.
 
	- Note: Run `cargo test --release` to run unit tests with release-level optimizations.
 

	
 
## Structure
 
- The user-facing API is visible in src/runtime/connector.rs
reowolf_old.h
Show inline comments
 
file renamed from reowolf.h to reowolf_old.h
src/runtime/ffi.rs
Show inline comments
 
new file 100644
 
use super::*;
 

	
 
use core::cell::RefCell;
 
use std::os::raw::{c_char, c_int, c_uchar, c_uint};
 

	
 
#[derive(Default)]
 
struct StoredError {
 
    // invariant: len is zero IFF its occupied
 
    // contents are 1+ bytes because we also store the NULL TERMINATOR
 
    buf: Vec<u8>,
 
}
 
impl StoredError {
 
    const NULL_TERMINATOR: u8 = 0;
 
    fn clear(&mut self) {
 
        // no null terminator either!
 
        self.buf.clear();
 
    }
 
    fn store<E: Debug>(&mut self, error: &E) {
 
        write!(&mut self.buf, "{:?}", error);
 
        self.buf.push(Self::NULL_TERMINATOR);
 
    }
 
    fn tl_store<E: Debug>(error: &E) {
 
        STORED_ERROR.with(|stored_error| {
 
            let mut stored_error = stored_error.borrow_mut();
 
            stored_error.clear();
 
            stored_error.store(error);
 
        })
 
    }
 
    fn tl_clear() {
 
        STORED_ERROR.with(|stored_error| {
 
            let mut stored_error = stored_error.borrow_mut();
 
            stored_error.clear();
 
        })
 
    }
 
    fn tl_raw_peek() -> (*const u8, usize) {
 
        STORED_ERROR.with(|stored_error| {
 
            let stored_error = stored_error.borrow();
 
            match stored_error.buf.len() {
 
                0 => (core::ptr::null(), 0), // no error!
 
                n => {
 
                    // stores an error of length n-1 AND a NULL TERMINATOR
 
                    (stored_error.buf.as_ptr(), n - 1)
 
                }
 
            }
 
        })
 
    }
 
}
 
thread_local! {
 
    static STORED_ERROR: RefCell<StoredError> = RefCell::new(StoredError::default());
 
}
 

	
 
type ErrorCode = i32;
 

	
 
//////////////////////////////////////
 

	
 
/// Returns length (via out pointer) and pointer (via return value) of the last Reowolf error.
 
/// - pointer is NULL iff there was no last error
 
/// - data at pointer is null-delimited
 
/// - len does NOT include the length of the null-delimiter
 
#[no_mangle]
 
pub unsafe extern "C" fn reowolf_error_peek(len: *mut usize) -> *const u8 {
 
    let (err_ptr, err_len) = StoredError::tl_raw_peek();
 
    len.write(err_len);
 
    err_ptr
 
}
 

	
 
#[no_mangle]
 
pub unsafe extern "C" fn protocol_description_parse(
 
    pdl: *const u8,
 
    pdl_len: usize,
 
    pd: *mut Arc<ProtocolDescription>,
 
) -> ErrorCode {
 
    StoredError::tl_clear();
 
    let slice: *const [u8] = std::slice::from_raw_parts(pdl, pdl_len);
 
    let slice: &[u8] = &*slice;
 
    match ProtocolDescription::parse(slice) {
 
        Ok(new) => {
 
            pd.write(Arc::new(new));
 
            0
 
        }
 
        Err(err) => {
 
            StoredError::tl_store(&err);
 
            -1
 
        }
 
    }
 
}
 

	
 
#[no_mangle]
 
pub unsafe extern "C" fn protocol_description_destroy(pd: Arc<ProtocolDescription>) {
 
    drop(pd)
 
}
 

	
 
#[no_mangle]
 
pub unsafe extern "C" fn protocol_description_clone(
 
    pd: &Arc<ProtocolDescription>,
 
) -> Arc<ProtocolDescription> {
 
    pd.clone()
 
}
 

	
 
// #[no_mangle]
 
// pub extern "C" fn connector_new(pd: *const Arc<ProtocolDescription>) -> *mut Connector {
 
//     Box::into_raw(Box::new(Connector::default()))
 
// }
 

	
 
// /// Creates and returns Reowolf Connector structure allocated on the heap.
 
// #[no_mangle]
 
// pub extern "C" fn connector_with_controller_id(controller_id: ControllerId) -> *mut Connector {
 
//     Box::into_raw(Box::new(Connector::Unconfigured(Unconfigured { controller_id })))
 
// }
src/runtime/mod.rs
Show inline comments
 
mod communication;
 
mod endpoints;
 
pub mod error;
 
mod ffi;
 
mod logging;
 
mod setup;
 

	
 
#[cfg(test)]
 
mod tests;
 

	
 
use crate::common::*;
 
use error::*;
 

	
 
#[derive(Debug)]
 
pub struct RoundOk {
 
    batch_index: usize,
 
    gotten: HashMap<PortId, Payload>,
 
}
 
#[derive(Debug)]
 
pub struct VecSet<T: std::cmp::Ord> {
 
    // invariant: ordered, deduplicated
 
    vec: Vec<T>,
 
}
 
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
 
pub enum ComponentId {
 
    Native,
 
    Proto(ProtoComponentId),
 
}
 
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
 
pub enum Route {
 
    LocalComponent(ComponentId),
 
    Endpoint { index: usize },
 
}
 
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
 
pub struct MyPortInfo {
 
    polarity: Polarity,
 
    port: PortId,
 
}
 
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
 
pub enum Decision {
 
    Failure,
 
    Success(Predicate),
 
}
 
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
 
pub enum Msg {
 
    SetupMsg(SetupMsg),
 
    CommMsg(CommMsg),
 
}
 
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
 
pub enum SetupMsg {
 
    MyPortInfo(MyPortInfo),
 
    LeaderWave { wave_leader: ConnectorId },
 
@@ -157,96 +158,97 @@ pub struct Connector {
 
#[derive(Debug)]
 
pub struct ConnectorCommunication {
 
    round_index: usize,
 
    endpoint_manager: EndpointManager,
 
    neighborhood: Neighborhood,
 
    mem_inbox: Vec<MemInMsg>,
 
    native_batches: Vec<NativeBatch>,
 
    round_result: Result<Option<RoundOk>, SyncError>,
 
}
 
#[derive(Debug)]
 
pub struct ConnectorUnphased {
 
    proto_description: Arc<ProtocolDescription>,
 
    proto_components: HashMap<ProtoComponentId, ProtoComponent>,
 
    logger: Box<dyn Logger>,
 
    id_manager: IdManager,
 
    native_ports: HashSet<PortId>,
 
    port_info: PortInfo,
 
}
 
#[derive(Debug)]
 
pub enum ConnectorPhased {
 
    Setup { endpoint_setups: Vec<(PortId, EndpointSetup)>, surplus_sockets: u16 },
 
    Communication(ConnectorCommunication),
 
}
 
#[derive(Default, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
 
pub struct Predicate {
 
    pub assigned: BTreeMap<FiringVar, bool>,
 
}
 
#[derive(Debug, Default)]
 
pub struct NativeBatch {
 
    // invariant: putters' and getters' polarities respected
 
    to_put: HashMap<PortId, Payload>,
 
    to_get: HashSet<PortId>,
 
}
 
pub struct NonsyncProtoContext<'a> {
 
    logger: &'a mut dyn Logger,
 
    proto_component_id: ProtoComponentId,
 
    port_info: &'a mut PortInfo,
 
    id_manager: &'a mut IdManager,
 
    proto_component_ports: &'a mut HashSet<PortId>,
 
    unrun_components: &'a mut Vec<(ProtoComponentId, ProtoComponent)>,
 
}
 
pub struct SyncProtoContext<'a> {
 
    logger: &'a mut dyn Logger,
 
    predicate: &'a Predicate,
 
    port_info: &'a PortInfo,
 
    inbox: &'a HashMap<PortId, Payload>,
 
}
 
////////////////
 

	
 
pub fn would_block(err: &std::io::Error) -> bool {
 
    err.kind() == std::io::ErrorKind::WouldBlock
 
}
 
impl<T: std::cmp::Ord> VecSet<T> {
 
    fn iter(&self) -> std::slice::Iter<T> {
 
        self.vec.iter()
 
    }
 
    fn contains(&self, element: &T) -> bool {
 
        self.vec.binary_search(element).is_ok()
 
    }
 
    fn new(mut vec: Vec<T>) -> Self {
 
        vec.sort();
 
        vec.dedup();
 
        Self { vec }
 
    }
 
}
 
impl PortInfo {
 
    fn firing_var_for(&self, port: PortId) -> FiringVar {
 
        FiringVar(match self.polarities.get(&port).unwrap() {
 
            Getter => port,
 
            Putter => *self.peers.get(&port).unwrap(),
 
        })
 
    }
 
}
 
impl IdManager {
 
    fn new(connector_id: ConnectorId) -> Self {
 
        Self {
 
            connector_id,
 
            port_suffix_stream: Default::default(),
 
            proto_component_suffix_stream: Default::default(),
 
        }
 
    }
 
    fn new_port_id(&mut self) -> PortId {
 
        Id { connector_id: self.connector_id, u32_suffix: self.port_suffix_stream.next() }.into()
 
    }
 
    fn new_proto_component_id(&mut self) -> ProtoComponentId {
 
        Id {
 
            connector_id: self.connector_id,
 
            u32_suffix: self.proto_component_suffix_stream.next(),
 
        }
 
        .into()
 
    }
 
}
 
impl Drop for Connector {
 
    fn drop(&mut self) {
 
        log!(&mut *self.unphased.logger, "Connector dropping. Goodbye!");
 
    }
 
}
src/runtime/retired/actors.rs
Show inline comments
 
deleted file
src/runtime/retired/communication.rs
Show inline comments
 
deleted file
src/runtime/retired/connector.rs
Show inline comments
 
deleted file
src/runtime/retired/endpoint.rs
Show inline comments
 
deleted file
src/runtime/retired/errors.rs
Show inline comments
 
deleted file
src/runtime/retired/experimental/api.rs
Show inline comments
 
deleted file
src/runtime/retired/experimental/bits.rs
Show inline comments
 
deleted file
src/runtime/retired/experimental/ecs.rs
Show inline comments
 
deleted file
src/runtime/retired/experimental/mod.rs
Show inline comments
 
deleted file
src/runtime/retired/experimental/pdl.rs
Show inline comments
 
deleted file
src/runtime/retired/experimental/predicate.rs
Show inline comments
 
deleted file
src/runtime/retired/experimental/vec_storage.rs
Show inline comments
 
deleted file
src/runtime/retired/ffi.rs
Show inline comments
 
deleted file
src/runtime/retired/serde.rs
Show inline comments
 
deleted file
src/runtime/retired/setup.rs
Show inline comments
 
deleted file
src/runtime/retired/v2.rs
Show inline comments
 
deleted file
0 comments (0 inline, 0 general)