Skip to content

Commit

Permalink
Extract thread worker logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmarsh committed Jan 26, 2024
1 parent e4acea1 commit 07bb4f8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 64 deletions.
68 changes: 4 additions & 64 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,12 @@ use crate::{
game::{self, Color::*},
ray::Rays,
},
ui::{draw, piece},
ui::{
draw, piece,
worker::{ThreadState, WorkerState},
},
};

#[derive(Clone, Debug)]
pub struct ThreadState(Arc<Mutex<ThreadData>>);

impl Default for ThreadState {
fn default() -> Self {
Self::new()
}
}

impl ThreadState {
fn new() -> Self {
Self(Arc::new(Mutex::new(ThreadData::new())))
}

fn get(&self) -> WorkerState {
self.0.lock().worker_state
}

fn set_ready(&self, state: game::State) {
let mut lock = self.0.lock();
lock.worker_state = WorkerState::Ready;
lock.new_state = state;
}

fn set_working(&self) {
self.0.lock().worker_state = WorkerState::Working;
}

fn set_done(&self) {
self.0.lock().worker_state = WorkerState::Done;
}

fn set_and_fetch(&self, worker_state: WorkerState) -> game::State {
let mut lock = self.0.lock();
lock.worker_state = worker_state;
lock.new_state.clone()
}
}

#[derive(Debug, Default)]
pub struct Konego {
pub state: game::State,
Expand Down Expand Up @@ -78,30 +42,6 @@ impl UIState {
}
}

#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum WorkerState {
Idle,
Working,
Ready,
Done,
}

#[derive(Debug)]
pub struct ThreadData {
pub new_state: game::State,
pub worker_state: WorkerState,
}

impl ThreadData {
fn new() -> Self {
Self {
new_state: game::State::new(),
worker_state: WorkerState::Idle,
}
}
}

impl GameLoop for Konego {
fn new(_c: &mut EngineState) -> Self {
Rays::build_lut();
Expand Down
1 change: 1 addition & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod app;
pub mod draw;
pub mod piece;
pub mod worker;
66 changes: 66 additions & 0 deletions src/ui/worker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::core::game;

use std::sync::{Arc, Mutex};

#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum WorkerState {
Idle,
Working,
Ready,
Done,
}

#[derive(Debug)]
pub struct ThreadData {
pub new_state: game::State,
pub worker_state: WorkerState,
}

impl ThreadData {
fn new() -> Self {
Self {
new_state: game::State::new(),
worker_state: WorkerState::Idle,
}
}
}

#[derive(Clone, Debug)]
pub struct ThreadState(Arc<Mutex<ThreadData>>);

impl Default for ThreadState {
fn default() -> Self {
Self::new()
}
}

impl ThreadState {
pub fn new() -> Self {
Self(Arc::new(Mutex::new(ThreadData::new())))
}

pub fn get(&self) -> WorkerState {
self.0.lock().unwrap().worker_state
}

pub fn set_ready(&self, state: game::State) {
let mut lock = self.0.lock().unwrap();
lock.worker_state = WorkerState::Ready;
lock.new_state = state;
}

pub fn set_working(&self) {
self.0.lock().unwrap().worker_state = WorkerState::Working;
}

pub fn set_done(&self) {
self.0.lock().unwrap().worker_state = WorkerState::Done;
}

pub fn set_and_fetch(&self, worker_state: WorkerState) -> game::State {
let mut lock = self.0.lock().unwrap();
lock.worker_state = worker_state;
lock.new_state.clone()
}
}

0 comments on commit 07bb4f8

Please sign in to comment.