Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions stdlib/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}