| @@ -43,6 +43,7 @@ use std::cell::{Cell, RefCell}; |
| 43 | 43 | use std::fmt; |
| 44 | 44 | use std::io::Write; |
| 45 | 45 | use std::rc::Rc; |
| 46 | +use std::sync::Arc; |
| 46 | 47 | |
| 47 | 48 | use louise_core::ir::*; |
| 48 | 49 | |
| @@ -182,10 +183,10 @@ pub(crate) struct OpRequest { |
| 182 | 183 | |
| 183 | 184 | /// Code in a function body of the program. |
| 184 | 185 | /// |
| 185 | | −/// The program is shared (`Rc`) by the interpreter and never changes, and |
| 186 | | −/// every machine and value lives on the interpreter's thread and is gone |
| 187 | | −/// before [`run`] returns, so the pointer stays valid as long as anything |
| 188 | | −/// holds it. |
| 186 | +/// The interpreter holds the program (an `Arc`, which runs may share) and |
| 187 | +/// it never changes, and every machine and value lives on the |
| 188 | +/// interpreter's thread and is gone before [`run`] returns, so the pointer |
| 189 | +/// stays valid as long as anything holds it. |
| 189 | 190 | #[derive(Clone, Copy)] |
| 190 | 191 | struct Code(*const Expr); |
| 191 | 192 | |
| @@ -378,14 +379,20 @@ pub(crate) struct State { |
| 378 | 379 | |
| 379 | 380 | #[derive(Clone)] |
| 380 | 381 | pub struct Interp { |
| 381 | | − pub(crate) program: Rc<Program>, |
| 382 | + pub(crate) program: Arc<Program>, |
| 382 | 383 | pub(crate) state: Rc<State>, |
| 383 | 384 | } |
| 384 | 385 | |
| 385 | 386 | /// Run `entry()` writing program output to `out`. Returns the error that |
| 386 | 387 | /// stopped the program, if any. `LOUISE_INTERP_MAX_FRAMES` limits the |
| 387 | | −/// depth of its stacks (see [`run_limited`]). |
| 388 | | −pub fn run(program: Program, entry: FnId, out: Box<dyn Write + Send>) -> Result<(), RtError> { |
| 388 | +/// depth of its stacks (see [`run_limited`]). Runs given the same |
| 389 | +/// `Arc<Program>` share it, at once on several threads if need be: the |
| 390 | +/// interpreter never changes a program (issue #316). |
| 391 | +pub fn run( |
| 392 | + program: impl Into<Arc<Program>>, |
| 393 | + entry: FnId, |
| 394 | + out: Box<dyn Write + Send>, |
| 395 | +) -> Result<(), RtError> { |
| 389 | 396 | let max_frames = std::env::var("LOUISE_INTERP_MAX_FRAMES") |
| 390 | 397 | .ok() |
| 391 | 398 | .and_then(|n| n.parse().ok()); |
| @@ -397,16 +404,17 @@ pub fn run(program: Program, entry: FnId, out: Box<dyn Write + Send>) -> Result< |
| 397 | 404 | /// heap and never overflow otherwise: tests limit them to check that deep |
| 398 | 405 | /// recursion runs in constant stack, as `LOUISE_STACK_KIB` does natively. |
| 399 | 406 | pub fn run_limited( |
| 400 | | − program: Program, |
| 407 | + program: impl Into<Arc<Program>>, |
| 401 | 408 | entry: FnId, |
| 402 | 409 | out: Box<dyn Write + Send>, |
| 403 | 410 | max_frames: Option<usize>, |
| 404 | 411 | ) -> Result<(), RtError> { |
| 412 | + let program: Arc<Program> = program.into(); |
| 405 | 413 | std::thread::Builder::new() |
| 406 | 414 | .stack_size(MAIN_STACK) |
| 407 | 415 | .spawn(move || { |
| 408 | 416 | let interp = Interp { |
| 409 | | − program: Rc::new(program), |
| 417 | + program, |
| 410 | 418 | state: Rc::new(State { |
| 411 | 419 | out: RefCell::new(out), |
| 412 | 420 | reductions: Cell::new(0), |