From 04ab320661b66d64761e1ac3c5b55237222dc630 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sun, 17 Sep 2023 15:12:51 +0300 Subject: [PATCH] give up on lab1a rust --- lab1a/rust/.gitignore | 1 + lab1a/rust/Cargo.lock | 122 +++++++++++++++++++++++++++++++ lab1a/rust/Cargo.toml | 12 +++ lab1a/rust/README.md | 3 + lab1a/rust/src/common.rs | 7 ++ lab1a/rust/src/limited_buffer.rs | 59 +++++++++++++++ lab1a/rust/src/main.rs | 118 ++++++++++++++++++++++++++++++ 7 files changed, 322 insertions(+) create mode 100644 lab1a/rust/.gitignore create mode 100644 lab1a/rust/Cargo.lock create mode 100644 lab1a/rust/Cargo.toml create mode 100644 lab1a/rust/README.md create mode 100644 lab1a/rust/src/common.rs create mode 100644 lab1a/rust/src/limited_buffer.rs create mode 100644 lab1a/rust/src/main.rs diff --git a/lab1a/rust/.gitignore b/lab1a/rust/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/lab1a/rust/.gitignore @@ -0,0 +1 @@ +target diff --git a/lab1a/rust/Cargo.lock b/lab1a/rust/Cargo.lock new file mode 100644 index 0000000..4612927 --- /dev/null +++ b/lab1a/rust/Cargo.lock @@ -0,0 +1,122 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "a-rust" +version = "0.1.0" +dependencies = [ + "anyhow", + "crossbeam-channel", + "crossbeam-utils", + "serde_json", +] + +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "crossbeam-channel" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + +[[package]] +name = "proc-macro2" +version = "1.0.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + +[[package]] +name = "serde" +version = "1.0.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "2.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e02e55d62894af2a08aca894c6577281f76769ba47c94d5756bec8ac6e7373" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/lab1a/rust/Cargo.toml b/lab1a/rust/Cargo.toml new file mode 100644 index 0000000..5285d36 --- /dev/null +++ b/lab1a/rust/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "a-rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.75" +crossbeam-channel = "0.5.8" +crossbeam-utils = "0.8.16" +serde_json = "1.0.107" diff --git a/lab1a/rust/README.md b/lab1a/rust/README.md new file mode 100644 index 0000000..ec67951 --- /dev/null +++ b/lab1a/rust/README.md @@ -0,0 +1,3 @@ +# Lab1a + +THIS IS NOT FINISHED diff --git a/lab1a/rust/src/common.rs b/lab1a/rust/src/common.rs new file mode 100644 index 0000000..39ae786 --- /dev/null +++ b/lab1a/rust/src/common.rs @@ -0,0 +1,7 @@ + +#[derive(Debug)] +pub struct DataEntry { + pub name: String, + pub sugar: f32, + pub criteria: i32 +} diff --git a/lab1a/rust/src/limited_buffer.rs b/lab1a/rust/src/limited_buffer.rs new file mode 100644 index 0000000..15d7099 --- /dev/null +++ b/lab1a/rust/src/limited_buffer.rs @@ -0,0 +1,59 @@ +use crate::common::DataEntry; + +pub struct LimitedBuffer { + items: Vec, + capacity: usize, + + estimated_size: i32, + consumed_count: u32, + processed_count: u32 +} + +impl LimitedBuffer { + pub fn new(capacity: usize) -> LimitedBuffer { + return LimitedBuffer{ + items: Vec::with_capacity(capacity), + capacity, + estimated_size: 0, + consumed_count: 0, + processed_count: 0 + } + } + + pub fn try_insert(&mut self, entry: DataEntry) -> bool { + if self.items.len() == self.capacity { + return false + } + self.items.push(entry); + return true + } + + pub fn remove(&mut self) -> Option { + if self.items.is_empty() { + None + } else { + Some(self.items.remove(0)) + } + } + + pub fn is_done(&self) -> bool { + return (self.estimated_size as u32) == self.processed_count + } + + pub fn consume_estimated(&mut self) -> bool { + if (self.estimated_size as u32) == self.consumed_count { + return false; + } else { + self.consumed_count += 1; + return true; + } + } + + pub fn update_estimated(&mut self, change: i32) { + self.estimated_size += change + } + + pub fn mark_processed(&mut self) { + self.processed_count += 1 + } +} diff --git a/lab1a/rust/src/main.rs b/lab1a/rust/src/main.rs new file mode 100644 index 0000000..1714877 --- /dev/null +++ b/lab1a/rust/src/main.rs @@ -0,0 +1,118 @@ +use std::{env::args, process::exit, thread, time::Duration, sync::{Arc, Mutex}}; + +use anyhow::Result; + +mod limited_buffer; +mod common; + +use common::DataEntry; +use limited_buffer::LimitedBuffer; + +// This was a mistake + +const INPUT_BUFFER_SIZE: usize = 2; +const INPUT_MONITOR_COUNT: i32 = 2; + +const OUTPUT_BUFFER_SIZE: usize = 10; +const OUTPUT_MONITOR_COUNT: i32 = 1; + +fn filter_entry(entry: &DataEntry) -> bool { + thread::sleep(Duration::from_millis(100 + entry.criteria as u64)); + return entry.sugar > entry.criteria as f32 +} + +fn output_entry(output_list: &mut Vec, entry: DataEntry) { + thread::sleep(Duration::from_millis(200)); + output_list.push(entry); + output_list.sort_by(|a, b| a.sugar.total_cmp(&b.sugar)) +} + +fn filter_entries(input: Arc>, output: Arc>) { + while !input.lock().unwrap().is_done() { + if input.lock().unwrap().consume_estimated() { + let entry = input.lock().unwrap().remove(); + if entry.is_none() { continue; } + let entry = entry.unwrap(); + + println!("Started to filter: {}", entry.name); + let is_filtered = filter_entry(&entry); + println!("Finished to filter: {}", entry.name); + + if is_filtered { + // output.lock().unwrap().update_estimated(1); + // while !output.lock().unwrap().try_insert(entry) {} + } + + input.lock().unwrap().mark_processed() + } + } +} + +fn ouput_entries(input: Arc>, output: Arc>, output_list: Arc>>) { + while !input.lock().unwrap().is_done() { + let entry = input.lock().unwrap().remove(); + if entry.is_none() { continue; } + let entry = entry.unwrap(); + output.lock().unwrap().consume_estimated(); + + let entry_name = entry.name.clone(); + println!("Started to output: {}", entry_name); + { + let mut output_list = output_list.lock().unwrap(); + output_entry(&mut output_list, entry); + } + println!("Finished to output: {}", entry_name); + + output.lock().unwrap().mark_processed() + } +} + +fn main() -> Result<()> { + let args = args().collect::>(); + if args.len() != 3 { + eprintln!("Usage: {} ", args[0]); + exit(-1) + } + + let entries = vec![ + DataEntry{ name: "foo1".into(), sugar: 100.0, criteria: 100 }, + DataEntry{ name: "foo2".into(), sugar: 100.0, criteria: 100 }, + DataEntry{ name: "foo3".into(), sugar: 100.0, criteria: 100 }, + DataEntry{ name: "foo4".into(), sugar: 100.0, criteria: 100 }, + DataEntry{ name: "foo5".into(), sugar: 100.0, criteria: 100 } + ]; + + let mut input_threads = vec![]; + // let mut output_threads = vec![]; + + // let output_list = Arc::new(Mutex::new(Vec::new())); + let input_buffer = Arc::new(Mutex::new(LimitedBuffer::new(INPUT_BUFFER_SIZE))); + let output_buffer = Arc::new(Mutex::new(LimitedBuffer::new(OUTPUT_BUFFER_SIZE))); + input_buffer.lock().unwrap().update_estimated(entries.len() as i32); + + for _ in 0..INPUT_MONITOR_COUNT { + let input_buffer = input_buffer.clone(); + let output_buffer = output_buffer.clone(); + input_threads.push(thread::spawn(|| filter_entries(input_buffer, output_buffer))); + } + // for _ in 0..OUTPUT_MONITOR_COUNT { + // let input_buffer = input_buffer.clone(); + // let output_buffer = output_buffer.clone(); + // let output_list = output_list.clone(); + // output_threads.push(thread::spawn(|| ouput_entries(input_buffer, output_buffer, output_list))); + // } + + for entry in entries { + input_buffer.lock().unwrap().try_insert(entry); + } + + for handle in input_threads { + handle.join().expect("Failed to join input thread"); + } + // for handle in output_threads { + // handle.join().expect("Failed to join output thread"); + // } + + println!("Finished"); + Ok(()) +}