]> git.lizzy.rs Git - rust.git/commitdiff
Add more concurrency tests.
authorVytautas Astrauskas <astrauv@amazon.com>
Sun, 19 Apr 2020 23:42:58 +0000 (16:42 -0700)
committerVytautas Astrauskas <astrauv@amazon.com>
Mon, 27 Apr 2020 21:26:36 +0000 (14:26 -0700)
tests/compile-fail/concurrency/libc_pthread_create_main_terminate.rs [new file with mode: 0644]
tests/compile-fail/concurrency/libc_pthread_join_detached.rs [new file with mode: 0644]
tests/compile-fail/concurrency/libc_pthread_join_joined.rs [new file with mode: 0644]
tests/compile-fail/concurrency/libc_pthread_join_multiple.rs [new file with mode: 0644]
tests/compile-fail/concurrency/libc_pthread_join_self.rs [new file with mode: 0644]

diff --git a/tests/compile-fail/concurrency/libc_pthread_create_main_terminate.rs b/tests/compile-fail/concurrency/libc_pthread_create_main_terminate.rs
new file mode 100644 (file)
index 0000000..e34d3f5
--- /dev/null
@@ -0,0 +1,24 @@
+// ignore-windows: Concurrency on Windows is not supported yet.
+
+// Check that we terminate the program when the main thread terminates.
+
+//~^^^^ ERROR: unsupported operation: the main thread terminated without waiting for other threads
+
+#![feature(rustc_private)]
+
+extern crate libc;
+
+use std::{mem, ptr};
+
+extern "C" fn thread_start(_null: *mut libc::c_void) -> *mut libc::c_void {
+    ptr::null_mut()
+}
+
+fn main() {
+    unsafe {
+        let mut native: libc::pthread_t = mem::zeroed();
+        let attr: libc::pthread_attr_t = mem::zeroed();
+        // assert_eq!(libc::pthread_attr_init(&mut attr), 0); FIXME: this function is not yet implemented.
+        assert_eq!(libc::pthread_create(&mut native, &attr, thread_start, ptr::null_mut()), 0);
+    }
+}
diff --git a/tests/compile-fail/concurrency/libc_pthread_join_detached.rs b/tests/compile-fail/concurrency/libc_pthread_join_detached.rs
new file mode 100644 (file)
index 0000000..ad83fb2
--- /dev/null
@@ -0,0 +1,24 @@
+// ignore-windows: Concurrency on Windows is not supported yet.
+
+// Joining a detached thread is undefined behavior.
+
+#![feature(rustc_private)]
+
+extern crate libc;
+
+use std::{mem, ptr};
+
+extern "C" fn thread_start(_null: *mut libc::c_void) -> *mut libc::c_void {
+    ptr::null_mut()
+}
+
+fn main() {
+    unsafe {
+        let mut native: libc::pthread_t = mem::zeroed();
+        let attr: libc::pthread_attr_t = mem::zeroed();
+        // assert_eq!(libc::pthread_attr_init(&mut attr), 0); FIXME: this function is not yet implemented.
+        assert_eq!(libc::pthread_create(&mut native, &attr, thread_start, ptr::null_mut()), 0);
+        assert_eq!(libc::pthread_detach(native), 0);
+        assert_eq!(libc::pthread_join(native, ptr::null_mut()), 0); //~ ERROR: Undefined Behavior: trying to join a detached or already joined thread
+    }
+}
diff --git a/tests/compile-fail/concurrency/libc_pthread_join_joined.rs b/tests/compile-fail/concurrency/libc_pthread_join_joined.rs
new file mode 100644 (file)
index 0000000..3ca0424
--- /dev/null
@@ -0,0 +1,24 @@
+// ignore-windows: Concurrency on Windows is not supported yet.
+
+// Joining an already joined thread is undefined behavior.
+
+#![feature(rustc_private)]
+
+extern crate libc;
+
+use std::{mem, ptr};
+
+extern "C" fn thread_start(_null: *mut libc::c_void) -> *mut libc::c_void {
+    ptr::null_mut()
+}
+
+fn main() {
+    unsafe {
+        let mut native: libc::pthread_t = mem::zeroed();
+        let attr: libc::pthread_attr_t = mem::zeroed();
+        // assert_eq!(libc::pthread_attr_init(&mut attr), 0); FIXME: this function is not yet implemented.
+        assert_eq!(libc::pthread_create(&mut native, &attr, thread_start, ptr::null_mut()), 0);
+        assert_eq!(libc::pthread_join(native, ptr::null_mut()), 0);
+        assert_eq!(libc::pthread_join(native, ptr::null_mut()), 0); //~ ERROR: Undefined Behavior: trying to join a detached or already joined thread
+    }
+}
diff --git a/tests/compile-fail/concurrency/libc_pthread_join_multiple.rs b/tests/compile-fail/concurrency/libc_pthread_join_multiple.rs
new file mode 100644 (file)
index 0000000..08ce940
--- /dev/null
@@ -0,0 +1,30 @@
+// ignore-windows: Concurrency on Windows is not supported yet.
+
+// Joining the same thread multiple times is undefined behavior.
+
+#![feature(rustc_private)]
+
+extern crate libc;
+
+use std::thread;
+use std::{mem, ptr};
+
+extern "C" fn thread_start(_null: *mut libc::c_void) -> *mut libc::c_void {
+    ptr::null_mut()
+}
+
+fn main() {
+    unsafe {
+        let mut native: libc::pthread_t = mem::zeroed();
+        let attr: libc::pthread_attr_t = mem::zeroed();
+        // assert_eq!(libc::pthread_attr_init(&mut attr), 0); FIXME: this function is not yet implemented.
+        assert_eq!(libc::pthread_create(&mut native, &attr, thread_start, ptr::null_mut()), 0);
+        let mut native_copy: libc::pthread_t = mem::zeroed();
+        ptr::copy_nonoverlapping(&native, &mut native_copy, 1);
+        let handle = thread::spawn(move || {
+            assert_eq!(libc::pthread_join(native_copy, ptr::null_mut()), 0); //~ ERROR: Undefined Behavior: trying to join a detached or already joined thread
+        });
+        assert_eq!(libc::pthread_join(native, ptr::null_mut()), 0);
+        handle.join().unwrap();
+    }
+}
diff --git a/tests/compile-fail/concurrency/libc_pthread_join_self.rs b/tests/compile-fail/concurrency/libc_pthread_join_self.rs
new file mode 100644 (file)
index 0000000..1aeb274
--- /dev/null
@@ -0,0 +1,16 @@
+// ignore-windows: Concurrency on Windows is not supported yet.
+
+// Joining itself is undefined behavior.
+
+#![feature(rustc_private)]
+
+extern crate libc;
+
+use std::ptr;
+
+fn main() {
+    unsafe {
+        let native: libc::pthread_t = libc::pthread_self();
+        assert_eq!(libc::pthread_join(native, ptr::null_mut()), 0); //~ ERROR: Undefined Behavior: trying to join itself
+    }
+}