Skip to content

Commit 5180b60

Browse files
Rollup merge of rust-lang#123505 - ChrisDenton:revert-121666, r=workingjubilee
Revert "Use OS thread name by default" This reverts rust-lang#121666 (Use the OS thread name by default if `THREAD_INFO` has not been initialized) due to rust-lang#123495 (Thread names are not always valid UTF-8). It's not a direct revert because there have been other changes since that PR.
2 parents 63033e7 + 2f47b32 commit 5180b60

File tree

13 files changed

+10
-164
lines changed

13 files changed

+10
-164
lines changed

std/src/sys/pal/hermit/thread.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use super::abi;
44
use super::thread_local_dtor::run_dtors;
5-
use crate::ffi::{CStr, CString};
5+
use crate::ffi::CStr;
66
use crate::io;
77
use crate::mem;
88
use crate::num::NonZero;
@@ -71,10 +71,6 @@ impl Thread {
7171
// nope
7272
}
7373

74-
pub fn get_name() -> Option<CString> {
75-
None
76-
}
77-
7874
#[inline]
7975
pub fn sleep(dur: Duration) {
8076
unsafe {

std/src/sys/pal/itron/thread.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::{
88
};
99
use crate::{
1010
cell::UnsafeCell,
11-
ffi::{CStr, CString},
11+
ffi::CStr,
1212
hint, io,
1313
mem::ManuallyDrop,
1414
num::NonZero,
@@ -204,10 +204,6 @@ impl Thread {
204204
// nope
205205
}
206206

207-
pub fn get_name() -> Option<CString> {
208-
None
209-
}
210-
211207
pub fn sleep(dur: Duration) {
212208
for timeout in dur2reltims(dur) {
213209
expect_success(unsafe { abi::dly_tsk(timeout) }, &"dly_tsk");

std/src/sys/pal/sgx/thread.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![cfg_attr(test, allow(dead_code))] // why is this necessary?
22
use super::unsupported;
3-
use crate::ffi::{CStr, CString};
3+
use crate::ffi::CStr;
44
use crate::io;
55
use crate::num::NonZero;
66
use crate::time::Duration;
@@ -133,10 +133,6 @@ impl Thread {
133133
// which succeeds as-is with the SGX target.
134134
}
135135

136-
pub fn get_name() -> Option<CString> {
137-
None
138-
}
139-
140136
pub fn sleep(dur: Duration) {
141137
usercalls::wait_timeout(0, dur, || true);
142138
}

std/src/sys/pal/teeos/thread.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::convert::TryInto;
22

33
use crate::cmp;
4-
use crate::ffi::{CStr, CString};
4+
use crate::ffi::CStr;
55
use crate::io;
66
use crate::mem;
77
use crate::num::NonZero;
@@ -101,10 +101,6 @@ impl Thread {
101101
// contact the teeos rustzone team.
102102
}
103103

104-
pub fn get_name() -> Option<CString> {
105-
None
106-
}
107-
108104
/// only main thread could wait for sometime in teeos
109105
pub fn sleep(dur: Duration) {
110106
let sleep_millis = dur.as_millis();

std/src/sys/pal/uefi/thread.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::unsupported;
2-
use crate::ffi::{CStr, CString};
2+
use crate::ffi::CStr;
33
use crate::io;
44
use crate::num::NonZero;
55
use crate::ptr::NonNull;
@@ -23,10 +23,6 @@ impl Thread {
2323
// nope
2424
}
2525

26-
pub fn get_name() -> Option<CString> {
27-
None
28-
}
29-
3026
pub fn sleep(dur: Duration) {
3127
let boot_services: NonNull<r_efi::efi::BootServices> =
3228
crate::os::uefi::env::boot_services().expect("can't sleep").cast();

std/src/sys/pal/unix/thread.rs

+1-73
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::cmp;
2-
use crate::ffi::{CStr, CString};
2+
use crate::ffi::CStr;
33
use crate::io;
44
use crate::mem;
55
use crate::num::NonZero;
@@ -234,78 +234,6 @@ impl Thread {
234234
// Newlib, Emscripten, and VxWorks have no way to set a thread name.
235235
}
236236

237-
#[cfg(any(
238-
target_os = "linux",
239-
target_os = "freebsd",
240-
target_os = "netbsd",
241-
target_os = "solaris",
242-
target_os = "illumos"
243-
))]
244-
pub fn get_name() -> Option<CString> {
245-
#[cfg(target_os = "linux")]
246-
const TASK_COMM_LEN: usize = 16;
247-
#[cfg(target_os = "freebsd")]
248-
const TASK_COMM_LEN: usize = libc::MAXCOMLEN + 1;
249-
#[cfg(any(target_os = "netbsd", target_os = "solaris", target_os = "illumos"))]
250-
const TASK_COMM_LEN: usize = 32;
251-
let mut name = vec![0u8; TASK_COMM_LEN];
252-
let res = unsafe {
253-
libc::pthread_getname_np(libc::pthread_self(), name.as_mut_ptr().cast(), name.len())
254-
};
255-
if res != 0 {
256-
return None;
257-
}
258-
name.truncate(name.iter().position(|&c| c == 0)?);
259-
CString::new(name).ok()
260-
}
261-
262-
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "watchos"))]
263-
pub fn get_name() -> Option<CString> {
264-
let mut name = vec![0u8; libc::MAXTHREADNAMESIZE];
265-
let res = unsafe {
266-
libc::pthread_getname_np(libc::pthread_self(), name.as_mut_ptr().cast(), name.len())
267-
};
268-
if res != 0 {
269-
return None;
270-
}
271-
name.truncate(name.iter().position(|&c| c == 0)?);
272-
CString::new(name).ok()
273-
}
274-
275-
#[cfg(target_os = "haiku")]
276-
pub fn get_name() -> Option<CString> {
277-
unsafe {
278-
let mut tinfo = mem::MaybeUninit::<libc::thread_info>::uninit();
279-
// See BeOS teams group and threads api.
280-
// https://www.haiku-os.org/legacy-docs/bebook/TheKernelKit_ThreadsAndTeams_Overview.html
281-
let thread_self = libc::find_thread(ptr::null_mut());
282-
let res = libc::get_thread_info(thread_self, tinfo.as_mut_ptr());
283-
if res != libc::B_OK {
284-
return None;
285-
}
286-
let info = tinfo.assume_init();
287-
let name =
288-
core::slice::from_raw_parts(info.name.as_ptr() as *const u8, info.name.len());
289-
CStr::from_bytes_until_nul(name).map(CStr::to_owned).ok()
290-
}
291-
}
292-
293-
#[cfg(not(any(
294-
target_os = "linux",
295-
target_os = "freebsd",
296-
target_os = "netbsd",
297-
target_os = "macos",
298-
target_os = "ios",
299-
target_os = "tvos",
300-
target_os = "watchos",
301-
target_os = "haiku",
302-
target_os = "solaris",
303-
target_os = "illumos"
304-
)))]
305-
pub fn get_name() -> Option<CString> {
306-
None
307-
}
308-
309237
#[cfg(not(target_os = "espidf"))]
310238
pub fn sleep(dur: Duration) {
311239
let mut secs = dur.as_secs();

std/src/sys/pal/unsupported/thread.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::unsupported;
2-
use crate::ffi::{CStr, CString};
2+
use crate::ffi::CStr;
33
use crate::io;
44
use crate::num::NonZero;
55
use crate::time::Duration;
@@ -22,10 +22,6 @@ impl Thread {
2222
// nope
2323
}
2424

25-
pub fn get_name() -> Option<CString> {
26-
None
27-
}
28-
2925
pub fn sleep(_dur: Duration) {
3026
panic!("can't sleep");
3127
}

std/src/sys/pal/wasi/thread.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::ffi::{CStr, CString};
1+
use crate::ffi::CStr;
22
use crate::io;
33
use crate::mem;
44
use crate::num::NonZero;
@@ -134,10 +134,6 @@ impl Thread {
134134
// nope
135135
}
136136

137-
pub fn get_name() -> Option<CString> {
138-
None
139-
}
140-
141137
pub fn sleep(dur: Duration) {
142138
let nanos = dur.as_nanos();
143139
assert!(nanos <= u64::MAX as u128);

std/src/sys/pal/wasm/atomics/thread.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::ffi::CStr;
2-
use crate::ffi::CString;
32
use crate::io;
43
use crate::num::NonZero;
54
use crate::sys::unsupported;
@@ -18,9 +17,6 @@ impl Thread {
1817
pub fn yield_now() {}
1918

2019
pub fn set_name(_name: &CStr) {}
21-
pub fn get_name() -> Option<CString> {
22-
None
23-
}
2420

2521
pub fn sleep(dur: Duration) {
2622
use crate::arch::wasm32;

std/src/sys/pal/windows/thread.rs

-24
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::sys::handle::Handle;
99
use crate::sys::stack_overflow;
1010
use crate::sys_common::FromInner;
1111
use crate::time::Duration;
12-
use alloc::ffi::CString;
1312
use core::ffi::c_void;
1413

1514
use super::time::WaitableTimer;
@@ -67,29 +66,6 @@ impl Thread {
6766
};
6867
}
6968

70-
pub fn get_name() -> Option<CString> {
71-
unsafe {
72-
let mut ptr = core::ptr::null_mut();
73-
let result = c::GetThreadDescription(c::GetCurrentThread(), &mut ptr);
74-
if result < 0 {
75-
return None;
76-
}
77-
let name = String::from_utf16_lossy({
78-
let mut len = 0;
79-
while *ptr.add(len) != 0 {
80-
len += 1;
81-
}
82-
core::slice::from_raw_parts(ptr, len)
83-
})
84-
.into_bytes();
85-
// Attempt to free the memory.
86-
// This should never fail but if it does then there's not much we can do about it.
87-
let result = c::LocalFree(ptr.cast::<c_void>());
88-
debug_assert!(result.is_null());
89-
if name.is_empty() { None } else { Some(CString::from_vec_unchecked(name)) }
90-
}
91-
}
92-
9369
pub fn join(self) {
9470
let rc = unsafe { c::WaitForSingleObject(self.handle.as_raw_handle(), c::INFINITE) };
9571
if rc == c::WAIT_FAILED {

std/src/sys/pal/xous/thread.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::ffi::{CStr, CString};
1+
use crate::ffi::CStr;
22
use crate::io;
33
use crate::num::NonZero;
44
use crate::os::xous::ffi::{
@@ -113,10 +113,6 @@ impl Thread {
113113
// nope
114114
}
115115

116-
pub fn get_name() -> Option<CString> {
117-
None
118-
}
119-
120116
pub fn sleep(dur: Duration) {
121117
// Because the sleep server works on units of `usized milliseconds`, split
122118
// the messages up into these chunks. This means we may run into issues

std/src/thread/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,7 @@ pub(crate) fn set_current(thread: Thread) {
694694
/// In contrast to the public `current` function, this will not panic if called
695695
/// from inside a TLS destructor.
696696
pub(crate) fn try_current() -> Option<Thread> {
697-
CURRENT
698-
.try_with(|current| current.get_or_init(|| Thread::new(imp::Thread::get_name())).clone())
699-
.ok()
697+
CURRENT.try_with(|current| current.get_or_init(|| Thread::new(None)).clone()).ok()
700698
}
701699

702700
/// Gets a handle to the thread that invokes it.

std/src/thread/tests.rs

-20
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,6 @@ fn test_named_thread_truncation() {
7070
result.unwrap().join().unwrap();
7171
}
7272

73-
#[cfg(any(
74-
all(target_os = "windows", not(target_vendor = "win7")),
75-
target_os = "linux",
76-
target_os = "macos",
77-
target_os = "ios",
78-
target_os = "tvos",
79-
target_os = "watchos"
80-
))]
81-
#[test]
82-
fn test_get_os_named_thread() {
83-
use crate::sys::thread::Thread;
84-
// Spawn a new thread to avoid interfering with other tests running on this thread.
85-
let handler = thread::spawn(|| {
86-
let name = c"test me please";
87-
Thread::set_name(name);
88-
assert_eq!(name, Thread::get_name().unwrap().as_c_str());
89-
});
90-
handler.join().unwrap();
91-
}
92-
9373
#[test]
9474
#[should_panic]
9575
fn test_invalid_named_thread() {

0 commit comments

Comments
 (0)