diff --git a/Cargo.lock b/Cargo.lock index ecc5b2ee0f..6844bcb1a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2801,6 +2801,7 @@ dependencies = [ "roc_tracing", "roc_types", "roc_unify", + "roc_work", "tempfile", "ven_pretty", ] @@ -3216,6 +3217,15 @@ dependencies = [ "roc_error_macros", ] +[[package]] +name = "roc_work" +version = "0.0.1" +dependencies = [ + "roc_collections", + "roc_error_macros", + "roc_module", +] + [[package]] name = "rustc-demangle" version = "0.1.23" diff --git a/crates/compiler/load_internal/Cargo.toml b/crates/compiler/load_internal/Cargo.toml index e5437947c8..0f9ee8c98e 100644 --- a/crates/compiler/load_internal/Cargo.toml +++ b/crates/compiler/load_internal/Cargo.toml @@ -10,6 +10,7 @@ version.workspace = true [dependencies] roc_builtins = { path = "../builtins" } roc_can = { path = "../can" } +roc_work = { path = "../work" } roc_checkmate = { path = "../checkmate" } roc_collections = { path = "../collections" } roc_constrain = { path = "../constrain" } diff --git a/crates/compiler/load_internal/src/file.rs b/crates/compiler/load_internal/src/file.rs index a0c5eaba65..b7788c2118 100644 --- a/crates/compiler/load_internal/src/file.rs +++ b/crates/compiler/load_internal/src/file.rs @@ -81,8 +81,8 @@ use { roc_packaging::https::{PackageMetadata, Problem}, }; -pub use crate::work::Phase; -use crate::work::{DepCycle, Dependencies}; +pub use roc_work::Phase; +use roc_work::{DepCycle, Dependencies}; #[cfg(target_family = "wasm")] use crate::wasm_instant::{Duration, Instant}; @@ -150,7 +150,8 @@ fn start_phase<'a>( ) -> Vec> { // we blindly assume all dependencies are met - use crate::work::PrepareStartPhase::*; + use roc_work::PrepareStartPhase::*; + match state.dependencies.prepare_start_phase(module_id, phase) { Continue => { // fall through diff --git a/crates/compiler/load_internal/src/lib.rs b/crates/compiler/load_internal/src/lib.rs index 24226d4c6b..919832ab4f 100644 --- a/crates/compiler/load_internal/src/lib.rs +++ b/crates/compiler/load_internal/src/lib.rs @@ -8,7 +8,6 @@ pub mod docs; pub mod file; pub mod module; mod module_cache; -mod work; #[cfg(target_family = "wasm")] mod wasm_instant; diff --git a/crates/compiler/work/Cargo.toml b/crates/compiler/work/Cargo.toml new file mode 100644 index 0000000000..6bbb4c74f2 --- /dev/null +++ b/crates/compiler/work/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "roc_work" +description = "This is used in loading." + +authors.workspace = true +edition.workspace = true +license.workspace = true +version.workspace = true + +[dependencies] +roc_collections = { path = "../collections" } +roc_module = { path = "../module" } +roc_error_macros = { path = "../../error_macros" } diff --git a/crates/compiler/work/src/lib.rs b/crates/compiler/work/src/lib.rs new file mode 100644 index 0000000000..6f51e383fe --- /dev/null +++ b/crates/compiler/work/src/lib.rs @@ -0,0 +1,3 @@ +mod work; + +pub use work::*; diff --git a/crates/compiler/load_internal/src/work.rs b/crates/compiler/work/src/work.rs similarity index 100% rename from crates/compiler/load_internal/src/work.rs rename to crates/compiler/work/src/work.rs diff --git a/crates/compiler/work/src/worker.rs b/crates/compiler/work/src/worker.rs new file mode 100644 index 0000000000..deef2763b5 --- /dev/null +++ b/crates/compiler/work/src/worker.rs @@ -0,0 +1,47 @@ +use crate::work::Dependencies; + +/// This is a trait so that we can implement both a single-threaded and a multithreaded version. +/// We *need* a single-threaded version for wasm, but it's also desirable in tests. +/// +/// The general design here is "mutex granularity" - basically, try to have a mutex around each +/// individual piece of global state, and lock it for as little time as possible. +pub trait Worker<'global, Event, ModuleId> { + fn enqueue_event(&mut self, event: Event); + fn with_deps(&mut self, func: impl FnMut(&mut Dependencies<'global, ModuleId>)); + // fn with_module_names(&mut self, func: impl FnMut(MutMap>)); + + // /// Phases + // fn with_headers(&mut self, func: impl FnMut(MutMap>)); + // fn with_parsed(&mut self, func: impl FnMut(MutMap>)); + // fn with_aliases(&mut self, func: impl FnMut(MutMap>)); + // fn with_pending_abilities(&mut self, func: impl FnMut(MutMap)); + // fn with_constrained(&mut self, func: impl FnMut(MutMap)); + // fn with_typechecked(&mut self, func: impl FnMut(MutMap>)); + // fn with_checked(&mut self, func: impl FnMut(MutMap)); + // fn with_found_specializations( + // &mut self, + // func: impl FnMut(MutMap>), + // ); + // fn with_late_specializations( + // &mut self, + // func: impl FnMut(MutMap>), + // ); + // fn with_external_specializations_requested( + // &mut self, + // func: impl FnMut(MutMap>>), + // ); + + // /// Various information + // fn with_imports(&mut self, func: impl FnMut(MutMap>)); + // fn with_exposes(&mut self, func: impl FnMut(MutMap>)); + // fn with_exposed_imports(&mut self, func: impl FnMut(MutMap>)); + // fn with_top_level_thunks(&mut self, func: impl FnMut(MutMap>)); + // fn with_documentation(&mut self, func: impl FnMut(VecMap)); + // fn with_can_problems( + // &mut self, + // func: impl FnMut(MutMap>), + // ); + // fn with_type_problems(&mut self, func: impl FnMut(MutMap>)); + + // fn with_sources(&mut self, func: impl FnMut(MutMap)); +}