diff options
author | Oxore <oxore@protonmail.com> | 2019-07-14 01:30:57 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2019-07-14 13:59:20 +0300 |
commit | 24e3ef936efd5b9239de76088efa02d06537ceb6 (patch) | |
tree | 2ec8875dcd264e9b145946cdc9f21d86f2da5d27 /src/main.rs |
Initial commit
Implement CLI and CPU core skeletons
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f5af35c --- /dev/null +++ b/src/main.rs @@ -0,0 +1,54 @@ +use std::io; +use std::io::prelude::*; +use std::{thread, time}; +use std::thread::sleep; +use std::sync::{Arc, Mutex, Condvar}; + +mod core; +use self::core::Core; + +fn core_worker(mut core: Core, should_stop: &Mutex<bool>, cvar: &Condvar) { + loop { + while false == *should_stop.lock().unwrap() { + println!("step {}", core.step()); + sleep(time::Duration::from_millis(1000)); + } + + while true == *should_stop.lock().unwrap() { + let _ = cvar.wait(should_stop.lock().unwrap()).unwrap(); + } + } +} + +fn cli_controller(asyncpair: Arc<(Mutex<bool>, Condvar)>) { + let &(ref should_stop, ref cvar) = &*asyncpair; + let stdin = io::stdin(); + for line in stdin.lock().lines() { + let line = line.unwrap(); + match line.as_ref() { + "stop" => { + println!("stop"); + *should_stop.lock().unwrap() = true; + }, + "start" => { + println!("start"); + *should_stop.lock().unwrap() = false; + cvar.notify_one(); + }, + _ => () + } + } +} + +fn main() { + let core = Core::new(); + let asyncpair = Arc::new((Mutex::new(false), Condvar::new())); + let asyncpair2 = asyncpair.clone(); + + thread::spawn(move || { + let (ref should_stop, ref condvar) = *asyncpair2; + core_worker(core, should_stop, condvar); + }); + + cli_controller(asyncpair); +} |