@@ -1042,8 +1042,6 @@ ThreadLister::Result ThreadLister::ListThreads(
1042
1042
1043
1043
Result result = Ok;
1044
1044
for (bool first_read = true ;; first_read = false ) {
1045
- // Resize to max capacity if it was downsized by IsAlive.
1046
- buffer_.resize (buffer_.capacity ());
1047
1045
CHECK_GE (buffer_.size (), 4096 );
1048
1046
uptr read = internal_getdents (
1049
1047
descriptor, (struct linux_dirent *)buffer_.data (), buffer_.size ());
@@ -1088,14 +1086,25 @@ ThreadLister::Result ThreadLister::ListThreads(
1088
1086
}
1089
1087
}
1090
1088
1089
+ const char *ThreadLister::LoadStatus (int tid) {
1090
+ auto cleanup = at_scope_exit ([&] {
1091
+ // Resize back to capacity if it is downsized by `ReadFileToVector`.
1092
+ buffer_.resize (buffer_.capacity ());
1093
+ });
1094
+ if (!ReadFileToVector (status_path_.data (), &buffer_) || buffer_.empty ())
1095
+ return nullptr ;
1096
+ buffer_.push_back (' \0 ' );
1097
+ return buffer_.data ();
1098
+ }
1099
+
1091
1100
bool ThreadLister::IsAlive (int tid) {
1092
1101
// /proc/%d/task/%d/status uses same call to detect alive threads as
1093
1102
// proc_task_readdir. See task_state implementation in Linux.
1094
- if (!ReadFileToVector (status_path_.data (), &buffer_) || buffer_.empty ())
1095
- return false ;
1096
- buffer_.push_back (0 );
1097
1103
static const char kPrefix [] = " \n PPid:" ;
1098
- const char *field = internal_strstr (buffer_.data (), kPrefix );
1104
+ const char *status = LoadStatus (tid);
1105
+ if (!status)
1106
+ return false ;
1107
+ const char *field = internal_strstr (status, kPrefix );
1099
1108
if (!field)
1100
1109
return false ;
1101
1110
field += internal_strlen (kPrefix );
0 commit comments