[compiler-rt] [compiler-rt] Reland "SetThreadName implementation for Fuchsia" (PR #105179)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 11:41:34 PDT 2024


================
@@ -607,7 +607,14 @@ size_t PageSize() {
 }
 
 void SetThreadName(std::thread &thread, const std::string &name) {
-  // TODO ?
+  std::string tmp(name);
+  if (tmp.resize() > 31)
+    tmp.resize(31);
+  if (zx_status_t s = zx_object_set_property(
+          thread.native_handle(), ZX_PROP_NAME, tmp.c_str(), tmp.size());
+      s != ZX_OK)
----------------
zeroomega wrote:

I talked to the kernel folks. The ZX_PROP_NAME doesn't need the string to be a NULL-terminated and it doesn't need to be size capped. The kernel will copy the string, NULL-terminate it and cap the size for you. In this case, you can just do 

```
zx_status_t s = zx_object_set_property(thread.native_handle(), ZX_PROP_NAME, name.data(), name.size())
```
And remove the local tmp variable to avoid a new string allocation. 

https://github.com/llvm/llvm-project/pull/105179


More information about the llvm-commits mailing list