From 831d26052a211aa1db87e1615a3a25ba30b25162 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Thu, 17 Feb 2022 23:45:04 +0200 Subject: [PATCH] store: expose the mutex so we can lock query-results The QueryResults must not outlive the lock (when in a MT context), so expose for clients (mu-server) to handle it. --- lib/mu-store.cc | 8 ++++++-- lib/mu-store.hh | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/mu-store.cc b/lib/mu-store.cc index 16b4c053..6e4ae364 100644 --- a/lib/mu-store.cc +++ b/lib/mu-store.cc @@ -662,14 +662,18 @@ Store::for_each_term(const std::string& field, Store::ForEachTermFunc func) cons return n; } +std::mutex& +Store::lock() const +{ + return priv_->lock_; +} + Option Store::run_query(const std::string& expr, MuMsgFieldId sortfieldid, QueryFlags flags, size_t maxnum) const { return xapian_try([&] { - std::lock_guard guard{priv_->lock_}; Query q{*this}; - return q.run(expr, sortfieldid, flags, maxnum); }, Nothing); diff --git a/lib/mu-store.hh b/lib/mu-store.hh index bee74df2..2f32b7e9 100644 --- a/lib/mu-store.hh +++ b/lib/mu-store.hh @@ -143,6 +143,9 @@ public: /** * Run a query; see the `mu-query` man page for the syntax. * + * Multi-threaded callers must aquire the lock and keep it + * at least as long as the return value. + * * @param expr the search expression * @param sortfieldid the sortfield-id. If the field is NONE, sort by DATE * @param flags query flags @@ -150,6 +153,7 @@ public: * * @return the query-results, or Nothing in case of error. */ + std::mutex& lock() const; Option run_query(const std::string& expr = "", MuMsgFieldId sortfieldid = MU_MSG_FIELD_ID_NONE, QueryFlags flags = QueryFlags::None,