[4/5] Move the 'store' method to index_cache_store_context

Message ID 20240128-pr-31262-index-cache-race-v1-4-4fe53c5265e3@tromey.com
State New
Headers
Series Fix race when writing to index cache |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Tom Tromey Jan. 28, 2024, 4:28 p.m. UTC
  I think it is cleaner for 'store' to be a method on
index_cache_store_context rather than on the global index cache
itself.  This patch makes this change.
---
 gdb/dwarf2/cooked-index.c |  2 +-
 gdb/dwarf2/index-cache.c  | 16 ++++++++--------
 gdb/dwarf2/index-cache.h  |  8 +++-----
 3 files changed, 12 insertions(+), 14 deletions(-)
  

Patch

diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c
index c53b5129d9e..ad9fe871552 100644
--- a/gdb/dwarf2/cooked-index.c
+++ b/gdb/dwarf2/cooked-index.c
@@ -830,7 +830,7 @@  cooked_index::maybe_write_index (const index_cache_store_context &ctx)
   if (index_for_writing () != nullptr)
     {
       /* (maybe) store an index in the cache.  */
-      global_index_cache.store (ctx);
+      ctx.store ();
     }
   m_state->set (cooked_state::CACHE_DONE);
 }
diff --git a/gdb/dwarf2/index-cache.c b/gdb/dwarf2/index-cache.c
index 91f2af3e275..780d2c4f200 100644
--- a/gdb/dwarf2/index-cache.c
+++ b/gdb/dwarf2/index-cache.c
@@ -155,30 +155,30 @@  index_cache_store_context::index_cache_store_context (const index_cache &ic,
 /* See dwarf-index-cache.h.  */
 
 void
-index_cache::store (const index_cache_store_context &ctx)
+index_cache_store_context::store () const
 {
-  if (!ctx.m_enabled)
+  if (!m_enabled)
     return;
 
-  const char *dwz_build_id_ptr = (ctx.m_dwz_build_id_str.has_value ()
-				  ? ctx.m_dwz_build_id_str->c_str ()
+  const char *dwz_build_id_ptr = (m_dwz_build_id_str.has_value ()
+				  ? m_dwz_build_id_str->c_str ()
 				  : nullptr);
 
   try
     {
       index_cache_debug ("writing index cache for objfile %s",
-			 bfd_get_filename (ctx.m_per_bfd->obfd));
+			 bfd_get_filename (m_per_bfd->obfd));
 
       /* Write the index itself to the directory, using the build id as the
 	 filename.  */
-      write_dwarf_index (ctx.m_per_bfd, ctx.m_dir.c_str (),
-			 ctx.m_build_id_str.c_str (), dwz_build_id_ptr,
+      write_dwarf_index (m_per_bfd, m_dir.c_str (),
+			 m_build_id_str.c_str (), dwz_build_id_ptr,
 			 dw_index_kind::GDB_INDEX);
     }
   catch (const gdb_exception_error &except)
     {
       index_cache_debug ("couldn't store index cache for objfile %s: %s",
-			 bfd_get_filename (ctx.m_per_bfd->obfd), except.what ());
+			 bfd_get_filename (m_per_bfd->obfd), except.what ());
     }
 }
 
diff --git a/gdb/dwarf2/index-cache.h b/gdb/dwarf2/index-cache.h
index 09577aca257..95f217ed961 100644
--- a/gdb/dwarf2/index-cache.h
+++ b/gdb/dwarf2/index-cache.h
@@ -40,10 +40,11 @@  struct index_cache_resource
 
 struct index_cache_store_context
 {
-  friend class index_cache;
-
   index_cache_store_context (const index_cache &ic, dwarf2_per_bfd *per_bfd);
 
+  /* Store the index in the cache.  */
+  void store () const;
+
 private:
   /* Captured value of enabled ().  */
   bool m_enabled;
@@ -82,9 +83,6 @@  class index_cache
   /* Disable the cache.  */
   void disable ();
 
-  /* Store an index for the specified object file in the cache.  */
-  void store (const index_cache_store_context &);
-
   /* Look for an index file matching BUILD_ID.  If found, return the contents
      as an array_view and store the underlying resources (allocated memory,
      mapped file, etc) in RESOURCE.  The returned array_view is valid as long