[Lldb-commits] [PATCH] D131081: [lldb] Prevent race condition when fetching /proc/cpuinfo

walter erquinigo via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 4 22:08:33 PDT 2022


wallace updated this revision to Diff 450226.
wallace added a comment.

simplify this diff following @labath's advice


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131081/new/

https://reviews.llvm.org/D131081

Files:
  lldb/source/Plugins/Process/Linux/Procfs.cpp


Index: lldb/source/Plugins/Process/Linux/Procfs.cpp
===================================================================
--- lldb/source/Plugins/Process/Linux/Procfs.cpp
+++ lldb/source/Plugins/Process/Linux/Procfs.cpp
@@ -7,9 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "Procfs.h"
-
 #include "lldb/Host/linux/Support.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Threading.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -17,17 +17,16 @@
 using namespace llvm;
 
 Expected<ArrayRef<uint8_t>> lldb_private::process_linux::GetProcfsCpuInfo() {
-  static Optional<std::vector<uint8_t>> cpu_info;
-  if (!cpu_info) {
-    auto buffer_or_error = errorOrToExpected(getProcFile("cpuinfo"));
-    if (!buffer_or_error)
-      return buffer_or_error.takeError();
-    MemoryBuffer &buffer = **buffer_or_error;
-    cpu_info = std::vector<uint8_t>(
-        reinterpret_cast<const uint8_t *>(buffer.getBufferStart()),
-        reinterpret_cast<const uint8_t *>(buffer.getBufferEnd()));
-  }
-  return *cpu_info;
+  static ErrorOr<std::unique_ptr<MemoryBuffer>> cpu_info_or_err =
+      getProcFile("cpuinfo");
+
+  if (!*cpu_info_or_err)
+    cpu_info_or_err.getError();
+
+  MemoryBuffer &buffer = **cpu_info_or_err;
+  return ArrayRef<uint8_t>(
+      reinterpret_cast<const uint8_t *>(buffer.getBufferStart()),
+      reinterpret_cast<const uint8_t *>(buffer.getBufferEnd()));
 }
 
 Expected<std::vector<cpu_id_t>>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131081.450226.patch
Type: text/x-patch
Size: 1502 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220805/9227442a/attachment.bin>


More information about the lldb-commits mailing list