|
| 1 | +//@ ignore-target: illumos solaris wasm |
| 2 | +//@ revisions: with_isolation without_isolation |
| 3 | +//@ [without_isolation] compile-flags: -Zmiri-disable-isolation |
| 4 | + |
| 5 | +// This test is based on `getpid.rs` |
| 6 | + |
| 7 | +fn gettid() -> u64 { |
| 8 | + cfg_if::cfg_if! { |
| 9 | + if #[cfg(any(target_os = "android", target_os = "linux", target_os = "nto"))] { |
| 10 | + (unsafe { libc::gettid() }) as u64 |
| 11 | + } else if #[cfg(target_os = "openbsd")] { |
| 12 | + (unsafe { libc::getthrid() }) as u64 |
| 13 | + } else if #[cfg(target_os = "freebsd")] { |
| 14 | + (unsafe { libc::pthread_getthreadid_np() }) as u64 |
| 15 | + } else if #[cfg(target_os = "netbsd")] { |
| 16 | + (unsafe { libc::_lwp_self() }) as u64 |
| 17 | + } else if #[cfg(target_vendor = "apple")] { |
| 18 | + let mut id = 0u64; |
| 19 | + let status: libc::c_int = unsafe { libc::pthread_threadid_np(0, &mut id) }; |
| 20 | + assert_eq!(status, 0); |
| 21 | + id |
| 22 | + } else if #[cfg(windows)] { |
| 23 | + use windows_sys::Win32::System::Threading::{GetCurrentThread, GetThreadId}; |
| 24 | + (unsafe { GetThreadId(GetCurrentThread()) }) as u64 |
| 25 | + } else { |
| 26 | + compile_error!("platform has no gettid") |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/// Specific platforms can query the tid of arbitrary threads; test that here. |
| 32 | +#[cfg(any(target_vendor = "apple", windows))] |
| 33 | +mod queried { |
| 34 | + use std::ffi::c_void; |
| 35 | + use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; |
| 36 | + use std::{ptr, thread, time}; |
| 37 | + |
| 38 | + use super::*; |
| 39 | + |
| 40 | + static SPAWNED_TID: AtomicU64 = AtomicU64::new(0); |
| 41 | + static CAN_JOIN: AtomicBool = AtomicBool::new(false); |
| 42 | + |
| 43 | + #[cfg(unix)] |
| 44 | + extern "C" fn thread_start(_data: *mut c_void) -> *mut c_void { |
| 45 | + thread_body(); |
| 46 | + ptr::null_mut() |
| 47 | + } |
| 48 | + |
| 49 | + #[cfg(windows)] |
| 50 | + extern "system" fn thread_start(_data: *mut c_void) -> u32 { |
| 51 | + thread_body(); |
| 52 | + 0 |
| 53 | + } |
| 54 | + |
| 55 | + fn thread_body() { |
| 56 | + SPAWNED_TID.store(gettid(), Ordering::Relaxed); |
| 57 | + let sleep_duration = time::Duration::from_millis(10); |
| 58 | + |
| 59 | + // Spin until the main thread has a chance to read this thread's ID |
| 60 | + while !CAN_JOIN.load(Ordering::Relaxed) { |
| 61 | + thread::sleep(sleep_duration); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + #[cfg(unix)] |
| 66 | + fn spawn_update_join() -> u64 { |
| 67 | + let mut t: libc::pthread_t = 0; |
| 68 | + let mut spawned_tid_from_handle = 0u64; |
| 69 | + |
| 70 | + unsafe { |
| 71 | + let res = libc::pthread_create(&mut t, ptr::null(), thread_start, ptr::null_mut()); |
| 72 | + assert_eq!(res, 0); |
| 73 | + |
| 74 | + let res = libc::pthread_threadid_np(t, &mut spawned_tid_from_handle); |
| 75 | + assert_eq!(res, 0); |
| 76 | + CAN_JOIN.store(true, Ordering::Relaxed); |
| 77 | + |
| 78 | + let res = libc::pthread_join(t, ptr::null_mut()); |
| 79 | + assert_eq!(res, 0); |
| 80 | + } |
| 81 | + |
| 82 | + spawned_tid_from_handle |
| 83 | + } |
| 84 | + |
| 85 | + #[cfg(windows)] |
| 86 | + fn spawn_update_join() -> u64 { |
| 87 | + use windows_sys::Win32::Foundation::WAIT_FAILED; |
| 88 | + use windows_sys::Win32::System::Threading::{ |
| 89 | + CreateThread, GetThreadId, INFINITE, WaitForSingleObject, |
| 90 | + }; |
| 91 | + |
| 92 | + let spawned_tid_from_handle; |
| 93 | + let mut tid_at_spawn = 0u32; |
| 94 | + |
| 95 | + unsafe { |
| 96 | + let handle = |
| 97 | + CreateThread(ptr::null(), 0, Some(thread_start), ptr::null(), 0, &mut tid_at_spawn); |
| 98 | + assert!(!handle.is_null()); |
| 99 | + |
| 100 | + spawned_tid_from_handle = GetThreadId(handle); |
| 101 | + assert_ne!(spawned_tid_from_handle, 0); |
| 102 | + CAN_JOIN.store(true, Ordering::Relaxed); |
| 103 | + |
| 104 | + let res = WaitForSingleObject(handle, INFINITE); |
| 105 | + assert_ne!(res, WAIT_FAILED); |
| 106 | + } |
| 107 | + |
| 108 | + assert_eq!(spawned_tid_from_handle, tid_at_spawn); |
| 109 | + |
| 110 | + spawned_tid_from_handle.into() |
| 111 | + } |
| 112 | + |
| 113 | + pub fn check() { |
| 114 | + let spawned_tid_from_handle = spawn_update_join(); |
| 115 | + assert_ne!(spawned_tid_from_handle, 0); |
| 116 | + assert_ne!(spawned_tid_from_handle, gettid()); |
| 117 | + assert_eq!(spawned_tid_from_handle, SPAWNED_TID.load(Ordering::Relaxed)); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +fn main() { |
| 122 | + let tid = gettid(); |
| 123 | + |
| 124 | + std::thread::spawn(move || { |
| 125 | + assert_ne!(gettid(), tid); |
| 126 | + }); |
| 127 | + |
| 128 | + // Test that in isolation mode a deterministic value will be returned. |
| 129 | + // The value is not important, we only care that whatever the value is, |
| 130 | + // won't change from execution to execution. |
| 131 | + if cfg!(with_isolation) { |
| 132 | + if cfg!(target_os = "linux") { |
| 133 | + // Linux starts the TID at the PID, which is 1000. |
| 134 | + assert_eq!(tid, 1000); |
| 135 | + } else { |
| 136 | + // Other platforms start counting from 0. |
| 137 | + assert_eq!(tid, 0); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // On Linux and NetBSD, the first TID is the PID. |
| 142 | + #[cfg(any(target_os = "linux", target_os = "netbsd"))] |
| 143 | + assert_eq!(tid, unsafe { libc::getpid() } as u64); |
| 144 | + |
| 145 | + #[cfg(any(target_vendor = "apple", windows))] |
| 146 | + queried::check(); |
| 147 | +} |
0 commit comments