#endif /* CXX_STD_THREAD */
}
+/* See cooked-index.h. */
+
+void
+cooked_index_worker::write_to_cache (const cooked_index *idx) const
+{
+ if (idx != nullptr)
+ m_cache_store.store ();
+}
+
cooked_index::cooked_index (dwarf2_per_objfile *per_objfile,
std::unique_ptr<cooked_index_worker> &&worker)
: m_state (std::move (worker)),
m_state->set (cooked_state::MAIN_AVAILABLE);
- index_cache_store_context ctx (global_index_cache, m_per_bfd);
-
/* This is run after finalization is done -- but not before. If
this task were submitted earlier, it would have to wait for
finalization. However, that would take a slot in the global
thread pool, and if enough such tasks were submitted at once, it
would cause a livelock. */
- gdb::task_group finalizers ([this, ctx = std::move (ctx)] ()
+ gdb::task_group finalizers ([this] ()
{
m_state->set (cooked_state::FINALIZED);
- maybe_write_index (ctx);
+ m_state->write_to_cache (index_for_writing ());
+ m_state->set (cooked_state::CACHE_DONE);
});
for (auto &idx : m_vector)
}
}
-void
-cooked_index::maybe_write_index (const index_cache_store_context &ctx)
-{
- if (index_for_writing () != nullptr)
- {
- /* (maybe) store an index in the cache. */
- ctx.store ();
- }
- m_state->set (cooked_state::CACHE_DONE);
-}
-
/* Wait for all the index cache entries to be written before gdb
exits. */
static void
public:
explicit cooked_index_worker (dwarf2_per_objfile *per_objfile)
- : m_per_objfile (per_objfile)
+ : m_per_objfile (per_objfile),
+ m_cache_store (global_index_cache, per_objfile->per_bfd)
{ }
virtual ~cooked_index_worker ()
{ }
protected:
- /* Let cooked_index call the 'set' method. */
+ /* Let cooked_index call the 'set' and 'write_to_cache' methods. */
friend class cooked_index;
+
+ /* Set the current state. */
void set (cooked_state desired_state);
+ /* Write to the index cache. */
+ void write_to_cache (const cooked_index *idx) const;
+
/* Helper function that does the work of reading. This must be able
to be run in a worker thread without problems. */
virtual void do_reading () = 0;
scanning is stopped and this exception will later be reported by
the 'wait' method. */
std::optional<gdb_exception> m_failed;
+ /* An object used to write to the index cache. */
+ index_cache_store_context m_cache_store;
};
/* The main index of DIEs.
private:
- /* Maybe write the index to the index cache. */
- void maybe_write_index (const index_cache_store_context &);
-
/* The vector of cooked_index objects. This is stored because the
entries are stored on the obstacks in those objects. */
vec_type m_vector;
#include "gdbsupport/selftest.h"
#include <string>
#include <stdlib.h>
+#include "run-on-main-thread.h"
/* When set to true, show debug messages about the index cache. */
static bool debug_index_cache = false;
m_dir (ic.m_dir),
m_per_bfd (per_bfd)
{
+ /* Capturing globals may only be done on the main thread. */
+ gdb_assert (is_main_thread ());
+
if (!m_enabled)
return;