Skip to content

Commit b4b3ff4

Browse files
author
Yuki Okushi
authored
Rollup merge of #103596 - RalfJung:thread-setname, r=cuviper
thread::set_name: debug-assert that things went well r? `@cuviper`
2 parents 77145c0 + d1132fb commit b4b3ff4

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ impl Thread {
137137
unsafe {
138138
// Available since glibc 2.12, musl 1.1.16, and uClibc 1.0.20.
139139
let name = truncate_cstr(name, TASK_COMM_LEN);
140-
libc::pthread_setname_np(libc::pthread_self(), name.as_ptr());
140+
let res = libc::pthread_setname_np(libc::pthread_self(), name.as_ptr());
141+
// We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
142+
debug_assert_eq!(res, 0);
141143
}
142144
}
143145

@@ -152,19 +154,22 @@ impl Thread {
152154
pub fn set_name(name: &CStr) {
153155
unsafe {
154156
let name = truncate_cstr(name, libc::MAXTHREADNAMESIZE);
155-
libc::pthread_setname_np(name.as_ptr());
157+
let res = libc::pthread_setname_np(name.as_ptr());
158+
// We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
159+
debug_assert_eq!(res, 0);
156160
}
157161
}
158162

159163
#[cfg(target_os = "netbsd")]
160164
pub fn set_name(name: &CStr) {
161165
unsafe {
162166
let cname = CStr::from_bytes_with_nul_unchecked(b"%s\0".as_slice());
163-
libc::pthread_setname_np(
167+
let res = libc::pthread_setname_np(
164168
libc::pthread_self(),
165169
cname.as_ptr(),
166170
name.as_ptr() as *mut libc::c_void,
167171
);
172+
debug_assert_eq!(res, 0);
168173
}
169174
}
170175

@@ -177,9 +182,8 @@ impl Thread {
177182
}
178183

179184
if let Some(f) = pthread_setname_np.get() {
180-
unsafe {
181-
f(libc::pthread_self(), name.as_ptr());
182-
}
185+
let res = unsafe { f(libc::pthread_self(), name.as_ptr()) };
186+
debug_assert_eq!(res, 0);
183187
}
184188
}
185189

0 commit comments

Comments
 (0)