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

use an idea similar to labath's to simplify this code while still using 
call_once


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
@@ -9,7 +9,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 +19,18 @@
 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 Optional<ErrorOr<std::unique_ptr<MemoryBuffer>>> cpu_info_or_err;
+  static llvm::once_flag g_once_flag;
+
+  llvm::call_once(g_once_flag,
+                  [] { 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>>


Index: lldb/source/Plugins/Process/Linux/Procfs.cpp
===================================================================
--- lldb/source/Plugins/Process/Linux/Procfs.cpp
+++ lldb/source/Plugins/Process/Linux/Procfs.cpp
@@ -9,7 +9,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 +19,18 @@
 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 Optional<ErrorOr<std::unique_ptr<MemoryBuffer>>> cpu_info_or_err;
+  static llvm::once_flag g_once_flag;
+
+  llvm::call_once(g_once_flag,
+                  [] { 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>>
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to