From 4469bd99f7a923dbe9542b8ba515e1f662130d9f 2020-02-20 11:10:13 From: Christopher Esterhuyse Date: 2020-02-20 11:10:13 Subject: [PATCH] one buffer, multiple ranges --- diff --git a/cbindgen.toml b/cbindgen.toml index f7e4686ef41006bd371f378a3ac2d6116d18d459..173a9ddff4dbc2519137b73fceecf18644d8df37 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -1,4 +1,3 @@ language = "C" - header = "/* CBindgen generated */" include_guard = "REOWOLF_HEADER_DEFINED" \ No newline at end of file diff --git a/huang b/huang new file mode 100644 index 0000000000000000000000000000000000000000..d5d491feda2e55a058b56d62adf92463d73db0a5 --- /dev/null +++ b/huang @@ -0,0 +1,147 @@ +/* CBindgen generated */ + +#ifndef REOWOLF_HEADER_DEFINED +#define REOWOLF_HEADER_DEFINED + +#include +#include +#include +#include + +typedef struct Connector Connector; + +typedef uint32_t ControllerId; + +/** + * Provides a binding annotation for the port with the given index with "active": + * (The port will conenct to a "passive" port at the given address during connect()) + * Returns: + * - 0 for success + * - 1 if the port was already bound and was left unchanged + * # Safety + * TODO + */ +int connector_bind_active(Connector *connector, unsigned int proto_port_index, const char *address); + +/** + * Provides a binding annotation for the port with the given index with "native": + * (The port is exposed for reading and writing from the application) + * Returns: + * # Safety + * TODO + */ +int connector_bind_native(Connector *connector, uintptr_t proto_port_index); + +/** + * Provides a binding annotation for the port with the given index with "native": + * (The port is exposed for reading and writing from the application) + * Returns: + * # Safety + * TODO + */ +int connector_bind_passive(Connector *connector, + unsigned int proto_port_index, + const char *address); + +/** + * Configures the given Reowolf connector with a protocol description in PDL. + * Returns: + * # Safety + * TODO + */ +int connector_configure(Connector *connector, char *pdl, char *main); + +/** + * Provides a binding annotation for the port with the given index with "active": + * (The port will conenct to a "passive" port at the given address during connect()) + * Returns: + * - 0 SUCCESS: connected successfully + * - TODO error codes + * # Safety + * TODO + */ +int connector_connect(Connector *connector, uint64_t timeout_millis); + +/** + * Destroys the given connector, freeing its underlying resources. + * # Safety + * TODO + */ +void connector_destroy(Connector *connector); + +/** + * Resets the error message buffer. + * Returns: + * - 0 if an error was cleared + * - 1 if there was no error to clear + * # Safety + * TODO + */ +int connector_error_clear(void); + +/** + * Returns a pointer into the error buffer for reading as a null-terminated string + * Returns null if there is no error in the buffer. + * # Safety + * TODO + */ +const char *connector_error_peek(void); + +/** + * Prepares to synchronously put a message at the given port, writing it to the given buffer. + * - 0 SUCCESS + * - 1 this port has the wrong direction + * - 2 this port is already marked to get + * # Safety + * TODO + */ +int connector_get(Connector *connector, unsigned int proto_port_index); + +/** + * # Safety + * TODO + */ +int connector_gotten(Connector *connector, + unsigned int proto_port_index, + const unsigned char **buf_ptr_outptr, + unsigned int *len_outptr); + +/** + * Creates and returns Reowolf Connector structure allocated on the heap. + */ +Connector *connector_new(void); + +/** + * # Safety + * TODO + */ +int connector_next_batch(Connector *connector); + +/** + * Prepares to synchronously put a message at the given port, reading it from the given buffer. + * # Safety + * TODO + */ +int connector_put(Connector *connector, + unsigned int proto_port_index, + unsigned char *buf_ptr, + unsigned int msg_len); + +/** + * # Safety + * TODO + */ +int connector_sync(Connector *connector, uint64_t timeout_millis); + +/** + * Creates and returns Reowolf Connector structure allocated on the heap. + */ +Connector *connector_with_controller_id(ControllerId controller_id); + +/** + * # Safety + * TODO + */ +int port_close(Connector *connector, unsigned int _proto_port_index); + +#endif /* REOWOLF_HEADER_DEFINED */ diff --git a/src/lib.rs b/src/lib.rs index 9fd8ad6a559a2a554ffa7fb2b5af208c720b6b21..7ecb45b933861008e028bb234c0fe2f914e12664 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,5 +10,7 @@ mod test; pub use runtime::{errors, Connector, PortBinding}; +pub use crate::runtime::experimental::api::Port; + #[cfg(feature = "ffi")] pub use runtime::ffi; diff --git a/src/runtime/experimental/api.rs b/src/runtime/experimental/api.rs index 7974f67db3c3999479c4c7d3ac382647fa2f2623..f3d5a75a11bb7875d8552c08f0741c6834e4eadf 100644 --- a/src/runtime/experimental/api.rs +++ b/src/runtime/experimental/api.rs @@ -50,6 +50,7 @@ impl<'a> From<&'a mut [u8]> for MsgBuffer<'a> { } #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[repr(C)] pub struct Port(pub u32); impl From for Port { fn from(x: InPort) -> Self { @@ -63,10 +64,6 @@ impl From for Port { } pub struct InPort(Port); pub struct OutPort(Port); -pub enum PortOp<'a> { - In { port: &'a InPort, poll: bool, msg: Option<&'a mut [u8]> }, - Out { port: &'a OutPort, offer: bool, msg: Option<&'a [u8]> }, -} #[derive(Default)] struct ChannelIndexStream { @@ -176,12 +173,13 @@ impl Connected { // TODO add a singleton machine Ok(()) } - pub fn sync_set(&mut self, _ops: &mut [PortOp]) -> Result<(), ()> { + pub fn sync_set(&mut self, _inbuf: &mut [u8], _ops: &mut [PortOpRs]) -> Result<(), ()> { Ok(()) } pub fn sync_subsets( &mut self, - _ops: &mut [PortOp], + _inbuf: &mut [u8], + _ops: &mut [PortOpRs], bit_subsets: &[&[usize]], ) -> Result { for (batch_index, bit_subset) in bit_subsets.iter().enumerate() { @@ -196,23 +194,50 @@ impl Connected { } } +macro_rules! bitslice { + ($( $num:expr ),*) => {{ + &[0 $( | (1usize << $num) )*] + }}; +} + #[test] fn api_new_test() { let mut c = Connecting::default(); - let net_out: OutPort = c.bind(Coupling::Active, "127.0.0.1:8001".parse().unwrap()); + let net_out: OutPort = c.bind(Coupling::Active, "127.0.0.1:8000".parse().unwrap()); let net_in: InPort = c.bind(Coupling::Active, "127.0.0.1:8001".parse().unwrap()); let proto_0 = Arc::new(Protocol::parse(b"").unwrap()); let mut c = c.connect(None).unwrap(); let (mem_out, mem_in) = c.new_channel(); + let mut inbuf = [0u8; 64]; c.new_component(&proto_0, b"sync".to_vec(), &[net_in.into(), mem_out.into()]).unwrap(); - - let mut buf = vec![0; 32]; let mut ops = [ - PortOp::Out { port: &net_out, offer: false, msg: Some(b"hi!") }, - PortOp::Out { port: &net_out, offer: false, msg: Some(b"hey!") }, - PortOp::Out { port: &net_out, offer: false, msg: Some(b"hello, there!") }, - PortOp::In { port: &mem_in, poll: false, msg: Some(&mut buf) }, + PortOpRs::In { msg_range: None, port: &mem_in }, + PortOpRs::Out { msg: b"hey", port: &net_out, optional: false }, + PortOpRs::Out { msg: b"hi?", port: &net_out, optional: true }, + PortOpRs::Out { msg: b"yo!", port: &net_out, optional: false }, ]; - c.sync_subsets(&mut ops, &[&[0b001], &[0b010], &[0b100]]).unwrap(); - c.sync_set(&mut ops).unwrap(); + c.sync_set(&mut inbuf, &mut ops).unwrap(); + c.sync_subsets(&mut inbuf, &mut ops, &[bitslice! {0,1,2}]).unwrap(); +} + +#[repr(C)] +pub struct PortOp { + msgbuf: *mut u8, + buflen: usize, + msglen: usize, + optional: bool, +} + +pub enum PortOpRs<'a> { + In { msg_range: Option>, port: &'a InPort }, + Out { msg: &'a [u8], port: &'a OutPort, optional: bool }, +} +pub struct InPortOp<'a> { + msg_range: Option>, // written by sync + port: &'a InPort, +} +pub struct OutPortOp<'a> { + msg: &'a [u8], + port: &'a OutPort, + optional: bool, } diff --git a/src/runtime/experimental/mod.rs b/src/runtime/experimental/mod.rs index c1187ad2c3d353ff7601c5d95b35bb84c6721d5c..700533992b6a3c82da62005fab4239d84b72dead 100644 --- a/src/runtime/experimental/mod.rs +++ b/src/runtime/experimental/mod.rs @@ -1,2 +1,2 @@ -mod api; +pub mod api; mod bits; diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index 54351a756516aedd3d0c933faff8debca1f7c823..a4a927db72cda173496233c78d0a025d78ef8b7b 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -6,7 +6,7 @@ pub(crate) mod communication; pub(crate) mod connector; pub(crate) mod endpoint; pub mod errors; -mod experimental; +pub mod experimental; mod serde; pub(crate) mod setup;