sim: Prevent reentrancy on simulation

Lock the simulation with a mutex to prevent concurrent access.  The C
code being tested uses globals, and can only be run in one context at a
time.  The Rust test framework may run tests concurrently, so use the
mutex to prevent this.

Signed-off-by: David Brown <david.brown@linaro.org>
diff --git a/sim/mcuboot-sys/src/c.rs b/sim/mcuboot-sys/src/c.rs
index f5643ab..dd2aac3 100644
--- a/sim/mcuboot-sys/src/c.rs
+++ b/sim/mcuboot-sys/src/c.rs
@@ -4,9 +4,18 @@
 use simflash::Flash;
 use libc;
 use api;
+use std::sync::Mutex;
+
+lazy_static! {
+    /// Mutex to lock the simulation.  The C code for the bootloader uses
+    /// global variables, and is therefore non-reentrant.
+    static ref BOOT_LOCK: Mutex<()> = Mutex::new(());
+}
 
 /// Invoke the bootloader on this flash device.
 pub fn boot_go(flash: &mut Flash, areadesc: &AreaDesc, counter: Option<&mut i32>, align: u8) -> i32 {
+    let _lock = BOOT_LOCK.lock().unwrap();
+
     unsafe {
         api::set_flash(flash);
         raw::sim_flash_align = align;
diff --git a/sim/mcuboot-sys/src/lib.rs b/sim/mcuboot-sys/src/lib.rs
index 16bd6c3..9f5e6b5 100644
--- a/sim/mcuboot-sys/src/lib.rs
+++ b/sim/mcuboot-sys/src/lib.rs
@@ -1,3 +1,4 @@
+#[macro_use] extern crate lazy_static;
 extern crate libc;
 #[macro_use] extern crate log;
 extern crate simflash;