Tinker with concurrency with FsSession

This commit is contained in:
Lucien Greathouse 2018-04-23 00:59:47 -07:00
parent 817ee31a23
commit 591b9c5f9a
2 changed files with 23 additions and 10 deletions

View file

@ -1,8 +1,9 @@
use std::collections::HashMap;
use std::path::PathBuf;
use std::process;
use std::time::Instant;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Instant;
use rand;
@ -55,10 +56,22 @@ pub fn serve(project_path: &PathBuf, port: Option<u64>) {
println!("Using session config {:#?}", config);
let mut session = FsSession::new(&config);
session.init();
let session = {
let mut session = FsSession::new(config.clone());
session.init();
Arc::new(Mutex::new(session))
};
// TODO: Let FsSession handle the main loop and spawn a bunch of threads
// ...since Rust stable doesn't have a channel select implementation
thread::spawn(move || {
loop {
session.lock().unwrap().step();
}
});
loop {
session.step();
thread::park();
}
}

View file

@ -27,8 +27,8 @@ pub enum FileItem {
},
}
pub struct FsSession<'a> {
pub config: &'a SessionConfig,
pub struct FsSession {
pub config: SessionConfig,
/// The in-memory files associated with each partition.
pub partition_files: HashMap<String, FileItem>,
@ -36,10 +36,10 @@ pub struct FsSession<'a> {
watchers: HashMap<String, FileWatcher>,
}
impl<'a> FsSession<'a> {
pub fn new(config: &SessionConfig) -> FsSession {
impl FsSession {
pub fn new(config: SessionConfig) -> FsSession {
FsSession {
config,
config: config,
partition_files: HashMap::new(),
watchers: HashMap::new(),
}