Skip to content

Commit

Permalink
refactor: simplify the input hander
Browse files Browse the repository at this point in the history
Removed the worker thread and instead moved just the necessary functionality into `InputHandler` directly.
bench: 2577543
  • Loading branch information
DeveloperPaul123 committed Nov 21, 2024
1 parent aaabe6c commit 309727e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 127 deletions.
65 changes: 26 additions & 39 deletions src/bin/byte-knight/input_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
use std::{
io::{stdin, BufRead},
str::FromStr,
sync::mpsc::{self, Receiver, Sender},
sync::{
atomic::{AtomicBool, Ordering},
mpsc::{self, Receiver},
Arc,
},
thread::JoinHandle,
};

use uci_parser::UciCommand;

use crate::worker_thread::WorkerThread;

#[derive(Debug)]
pub(crate) struct InputHandler {
worker: WorkerThread<UciCommand>,
handle: Option<JoinHandle<()>>,
stop_flag: Arc<AtomicBool>,
receiver: Receiver<UciCommand>,
}

impl InputHandler {
Expand All @@ -49,8 +54,10 @@ impl InputHandler {
/// ```
///
pub(crate) fn new() -> InputHandler {
let stop_flag = Arc::new(AtomicBool::new(false));
let stop_flag_clone = stop_flag.clone();
let (sender, receiver) = mpsc::channel();
let worker = WorkerThread::new(sender.clone(), receiver, move |stop_flag| {
let worker = std::thread::spawn(move || {
let stdin = stdin();
let mut input = stdin.lock().lines();
while !stop_flag.load(std::sync::atomic::Ordering::Relaxed) {
Expand All @@ -71,48 +78,28 @@ impl InputHandler {
}
}
});
InputHandler { worker }
InputHandler {
handle: Some(worker),
stop_flag: stop_flag_clone,
receiver,
}
}

fn sender(&self) -> Sender<UciCommand> {
self.worker.sender()
pub fn stop(&mut self) {
self.stop_flag.store(true, Ordering::Relaxed);

if let Some(handle) = self.handle.take() {
handle.join().unwrap();
}
}

/// Returns a reference to the receiver end of the channel.
///
/// # Returns
///
/// A reference to the receiver end of the channel.
pub(crate) fn receiver(&self) -> &Receiver<UciCommand> {
self.worker.receiver()
pub fn receiver(&self) -> &Receiver<UciCommand> {
&self.receiver
}

/// Signal to the worker thread that it should stop. This method does not block the calling
/// thread.
pub(crate) fn exit(&mut self) {
self.worker.stop();
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_input_handler() {
let input_handler = InputHandler::new();
let sender = input_handler.sender();

sender.send(UciCommand::Uci).unwrap();
sender.send(UciCommand::IsReady).unwrap();
sender.send(UciCommand::UciNewGame).unwrap();

let receiver = input_handler.receiver();
let inputs: Vec<UciCommand> = receiver.iter().take(3).collect();

assert_eq!(
inputs,
vec![UciCommand::Uci, UciCommand::IsReady, UciCommand::UciNewGame]
);
self.stop();
}
}
1 change: 0 additions & 1 deletion src/bin/byte-knight/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ mod score;
mod search;
mod search_thread;
mod tt_table;
mod worker_thread;

use defs::About;
use engine::ByteKnight;
Expand Down
87 changes: 0 additions & 87 deletions src/bin/byte-knight/worker_thread.rs

This file was deleted.

0 comments on commit 309727e

Please sign in to comment.