]> git.lizzy.rs Git - rust.git/commitdiff
Add test of recursive mutex using libc FFI
authorDavid Cook <divergentdave@gmail.com>
Sat, 22 Feb 2020 01:10:20 +0000 (19:10 -0600)
committerDavid Cook <divergentdave@gmail.com>
Sun, 5 Apr 2020 15:05:14 +0000 (10:05 -0500)
tests/run-pass/sync.rs

index 46cad3c1620160fd21f8db76c429a4729e2a1260..b247061083798411ec252fa8a126d9f20533bb92 100644 (file)
@@ -8,6 +8,7 @@ fn main() {
     test_mutex();
     #[cfg(not(target_os = "windows"))] // TODO: implement RwLock on Windows
     {
+        test_mutex_libc_recursive();
         test_rwlock_stdlib();
         test_rwlock_libc_init();
         test_rwlock_libc_static_initializer();
@@ -24,6 +25,28 @@ fn test_mutex() {
     drop(m);
 }
 
+#[cfg(not(target_os = "windows"))]
+fn test_mutex_libc_recursive() {
+    unsafe {
+        let mut attr: libc::pthread_mutexattr_t = std::mem::zeroed();
+        assert_eq!(libc::pthread_mutexattr_init(&mut attr as *mut _), 0);
+        assert_eq!(libc::pthread_mutexattr_settype(&mut attr as *mut _, libc::PTHREAD_MUTEX_RECURSIVE), 0);
+        let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
+        assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mut attr as *mut _), 0);
+        assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
+        assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
+        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
+        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
+        assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
+        assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
+        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
+        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
+        assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), libc::EPERM);
+        assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
+        assert_eq!(libc::pthread_mutexattr_destroy(&mut attr as *mut _), 0);
+    }
+}
+
 #[cfg(not(target_os = "windows"))]
 fn test_rwlock_stdlib() {
     let rw = RwLock::new(0);