diff --git a/stdlib/src/sync.rs b/stdlib/src/sync.rs index bdb50b9e..b21c911f 100644 --- a/stdlib/src/sync.rs +++ b/stdlib/src/sync.rs @@ -125,4 +125,35 @@ mod tests { let guard = mutex.locked.lock().unwrap(); assert_eq!(*guard, false); } + + #[test] + fn test_script_mutex_lock() { + use std::sync::Arc; + use std::thread; + use std::time::Duration; + use std::sync::atomic::{AtomicBool, Ordering}; + + let mutex = Arc::new(ScriptMutex::new()); + let mutex_clone = Arc::clone(&mutex); + let flag = Arc::new(AtomicBool::new(false)); + let flag_clone = Arc::clone(&flag); + + mutex.lock(); + + let handle = thread::spawn(move || { + mutex_clone.lock(); + flag_clone.store(true, Ordering::SeqCst); + mutex_clone.unlock(); + }); + + thread::sleep(Duration::from_millis(50)); + // Verify the second thread is blocked and hasn't set the flag + assert_eq!(flag.load(Ordering::SeqCst), false); + + mutex.unlock(); + + handle.join().unwrap(); + // Verify the second thread acquired the lock and set the flag + assert_eq!(flag.load(Ordering::SeqCst), true); + } }