Skip to content
Snippets Groups Projects
Commit 9b89acd3 authored by Per's avatar Per
Browse files

Initial commit

parents
Branches
No related tags found
No related merge requests found
/target
**/*.rs.bk
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "cargo run --example vec_deque",
"command": "cargo run --example vec_deque",
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
\ No newline at end of file
[[package]]
name = "heapless"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"untagged-option 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "timber"
version = "0.1.0"
dependencies = [
"heapless 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"untagged-option 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "untagged-option"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum heapless 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8b916e459cf234b519c012932b893a3a69e2d3bf9be14f60a376d0bb853d398d"
"checksum untagged-option 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89553f60895e868761e18120e72077da22920614562d2f4fe98fa707fbb12fe6"
[package]
name = "timber"
version = "0.1.0"
authors = ["pln <Per Lindgren>"]
[dependencies]
heapless = "0.2.5"
untagged-option = "0.1.1"
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
static NTHREADS: i32 = 3;
fn main() {
// Channels have two endpoints: the `Sender<T>` and the `Receiver<T>`,
// where `T` is the type of the message to be transferred
// (type annotation is superfluous)
let (tx, rx): (Sender<i32>, Receiver<i32>) = mpsc::channel();
for id in 0..NTHREADS {
// The sender endpoint can be copied
let thread_tx = tx.clone();
// Each thread will send its id via the channel
thread::spawn(move || {
// The thread takes ownership over `thread_tx`
// Each thread queues a message in the channel
thread_tx.send(id).unwrap();
// Sending is a non-blocking operation, the thread will continue
// immediately after sending its message
println!("thread {} finished", id);
});
}
// Here, all the messages are collected
let mut ids = Vec::with_capacity(NTHREADS as usize);
for _ in 0..NTHREADS {
// The `recv` method picks a message from the channel
// `recv` will block the current thread if there are no messages available
ids.push(rx.recv());
}
// Show the order in which the messages were sent
println!("{:?}", ids);
}
#![allow(unused)]
fn main() {
use std::sync::mpsc::channel;
use std::sync::mpsc::{Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::thread;
const N: usize = 10;
// Spawn a few threads to increment a shared variable (non-atomically), and
// let the main thread know once all increments are done.
//
// Here we're using an Arc to share memory among threads, and the data inside
// the Arc is protected with a mutex.
let data = Arc::new(Mutex::new(0));
let (tx, rx): (Sender<i32>, Receiver<i32>) = channel();
for _ in 0..N {
let (data, tx) = (data.clone(), tx.clone());
thread::spawn(move || {
// The shared state can only be accessed once the lock is held.
// Our non-atomic increment is safe because we're the only thread
// which can access the shared state when the lock is held.
//
// We unwrap() the return value to assert that we are not expecting
// threads to ever fail while holding the lock.
let mut data = data.lock().unwrap();
*data += 1;
tx.send((*data)).unwrap();
// the lock is unlocked here when `data` goes out of scope.
});
}
println!("spawned");
for _ in 0..N {
println!("received {:?}", rx.recv());
}
//rx.recv().unwrap();
let mut data = data.lock().unwrap();
println!("there {}", data);
}
use std::collections::vec_deque::VecDeque;
trait Dispatch {
fn dispatch(&mut self);
}
struct MsgQ {
data: VecDeque<i32>,
}
impl MsgQ {
fn new() -> Self {
MsgQ {
data: VecDeque::new(),
}
}
}
impl Dispatch for MsgQ {
fn dispatch(&mut self) {
let data = self.data.pop_front().unwrap();
println!("dispatched value {}", data);
}
}
fn main() {
let mut q = MsgQ::new();
q.data.push_back(1);
q.data.push_back(2);
q.dispatch();
q.dispatch();
// println!("{}", b.pop_front().unwrap());
// println!("{}", b.pop_front().unwrap());
}
use std::collections::vec_deque::VecDeque;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
trait Dispatch {
fn dispatch(&mut self);
}
struct MsgQ {
data: VecDeque<i32>,
}
impl MsgQ {
fn new() -> Self {
MsgQ {
data: VecDeque::new(),
}
}
}
impl Dispatch for MsgQ {
fn dispatch(&mut self) {
let data = self.data.pop_front().unwrap();
println!("dispatched value {}", data);
}
}
// interface for object A
struct A {
data: i32,
cb: (Option<Arc<Mutex<A>>>, fn(&A) -> ()),
}
impl A {
fn new(data: i32, cb: (Option<Arc<Mutex<A>>>, fn(&A) -> ())) -> Self {
A { data, cb }
}
fn print(&self) {
println!("data {}", self.data);
}
fn update(&mut self, v: i32) {
self.data = v;
//let o = self.cb.0.unwrap();
self.cb.1(&*self.cb.0.as_ref().unwrap().lock().unwrap());
}
}
fn main() {
let mut q = MsgQ::new();
let mut a1 = Arc::new(Mutex::new(A::new(1, (None, A::print))));
let mut a2 = Arc::new(Mutex::new(A::new(2, (None, A::print))));
a1 = Arc::new(Mutex::new(A::new(1, (Some(a2), A::print))));
a2 = Arc::new(Mutex::new(A::new(2, (Some(a1), A::print))));
//a1.print();
a2.lock().unwrap().update(17);
// a3.print();
// let f = A::update;
// f(&mut a, 2);
// q.data.push_back(1);
// q.data.push_back(2);
// q.dispatch();
// q.dispatch();
// println!("{}", b.pop_front().unwrap());
// println!("{}", b.pop_front().unwrap());
}
extern crate heapless;
extern crate untagged_option;
use heapless::{ring_buffer::Consumer /*, ring_buffer::Producer */,
RingBuffer};
use untagged_option::UntaggedOption;
trait Dispatch {
fn dispatch(&mut self);
}
struct MsgQ(RingBuffer<&'static mut Dispatch, [&'static mut Dispatch; 4]>);
impl Dispatch for MsgQ {
fn dispatch(&mut self) {
self.0.dequeue().unwrap().dispatch();
}
}
pub struct AConsumer<'a>(Consumer<'a, u8, [u8; 4]>);
impl<'a> Dispatch for AConsumer<'a> {
fn dispatch(&mut self) {
a(self.0.dequeue().unwrap());
}
}
pub struct BConsumer<'a>(Consumer<'a, u8, [u8; 4]>);
impl<'a> Dispatch for BConsumer<'a> {
fn dispatch(&mut self) {
b(self.0.dequeue().unwrap());
}
}
static mut RA: RingBuffer<u8, [u8; 4]> = RingBuffer::new();
static mut RB: RingBuffer<u8, [u8; 4]> = RingBuffer::new();
static mut AC: UntaggedOption<AConsumer<'static>> = UntaggedOption { none: () };
static mut BC: UntaggedOption<BConsumer<'static>> = UntaggedOption { none: () };
fn main() {
unsafe {
let (mut ap, ac) = RA.split();
AC.some = AConsumer { 0: ac };
let (mut bp, bc) = RB.split();
BC.some = BConsumer { 0: bc };
let mut mq = MsgQ {
0: RingBuffer::new(),
};
let _ = ap.enqueue(1);
let _ = mq.0.enqueue(&mut AC.some);
let _ = ap.enqueue(2);
let _ = mq.0.enqueue(&mut AC.some);
let _ = bp.enqueue(3);
let _ = mq.0.enqueue(&mut BC.some);
mq.dispatch();
mq.dispatch();
mq.dispatch();
}
}
fn a(d: u8) {
println!("a : {}", d);
}
fn b(d: u8) {
println!("b : {}", d);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment