still deadlocking

This commit is contained in:
Joe Ardent 2024-05-30 21:52:52 -07:00
parent c53edd921a
commit e6d76ba37c
2 changed files with 9 additions and 5 deletions

View file

@ -1,4 +1,7 @@
use std::sync::{Arc, Mutex}; use std::{
rc::Rc,
sync::{Arc, Mutex},
};
use nebkor_maelstrom::{mk_payload, mk_stdin, Body, Message, Node, Runner}; use nebkor_maelstrom::{mk_payload, mk_stdin, Body, Message, Node, Runner};
@ -6,7 +9,7 @@ fn main() {
let node = Counter; let node = Counter;
let node = Arc::new(Mutex::new(node)); let node = Arc::new(Mutex::new(node));
let runner = Runner::new(node); let runner = Rc::new(Runner::new(node));
let (i, _, rx) = mk_stdin(); let (i, _, rx) = mk_stdin();
@ -33,7 +36,7 @@ const KEY: &str = "COUNTER";
struct Counter; struct Counter;
impl Node for Counter { impl Node for Counter {
fn handle<'slf>(&'slf mut self, runner: &'slf Runner, req: Message) { fn handle<'slf>(&'slf mut self, runner: &'slf Rc<Runner>, req: Message) {
let typ = req.body.typ.as_str(); let typ = req.body.typ.as_str();
let frm = req.src.clone(); let frm = req.src.clone();
let msg_id = req.body.msg_id.to_owned(); let msg_id = req.body.msg_id.to_owned();

View file

@ -1,6 +1,7 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
io::{BufRead, Stdout, Write}, io::{BufRead, Stdout, Write},
rc::Rc,
sync::{ sync::{
atomic::{AtomicU64, AtomicUsize, Ordering}, atomic::{AtomicU64, AtomicUsize, Ordering},
mpsc::{channel, Receiver, Sender}, mpsc::{channel, Receiver, Sender},
@ -26,7 +27,7 @@ static MSG_ID: AtomicU64 = AtomicU64::new(0);
static OUTPUT: LazyLock<Stdout> = LazyLock::new(std::io::stdout); static OUTPUT: LazyLock<Stdout> = LazyLock::new(std::io::stdout);
pub trait Node { pub trait Node {
fn handle(&mut self, runner: &Runner, msg: Message); fn handle(&mut self, runner: &Rc<Runner>, msg: Message);
} }
#[derive(Clone)] #[derive(Clone)]
@ -49,7 +50,7 @@ impl Runner {
} }
} }
pub fn run(&self, input: Receiver<Message>, on_init: Option<OnInit>) { pub fn run(self: &Rc<Self>, input: Receiver<Message>, on_init: Option<OnInit>) {
for msg in input.iter() { for msg in input.iter() {
let typ = &msg.body.typ; let typ = &msg.body.typ;
if let "init" = typ.as_str() { if let "init" = typ.as_str() {