Skip to content

Commit

Permalink
wallet: use Synced<T> for g_unloading_wallet_set and remove g_wallet_…
Browse files Browse the repository at this point in the history
…release_mutex

Convert `g_unloading_wallet_set` to use `Synced<T>` and ditch the global mutex
`g_wallet_release_mutex`.
  • Loading branch information
vasild committed Oct 5, 2023
1 parent 8471be5 commit b749c4d
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/wallet/wallet.cpp
Expand Up @@ -212,10 +212,9 @@ void NotifyWalletLoaded(WalletContext& context, const std::shared_ptr<CWallet>&
}
}

static GlobalMutex g_wallet_release_mutex;
static std::condition_variable g_wallet_release_cv;
static Synced<std::set<std::string>> g_loading_wallet_set;
static std::set<std::string> g_unloading_wallet_set GUARDED_BY(g_wallet_release_mutex);
static Synced<std::set<std::string>> g_unloading_wallet_set;

// Custom deleter for shared_ptr<CWallet>.
static void ReleaseWallet(CWallet* wallet)
Expand All @@ -225,12 +224,9 @@ static void ReleaseWallet(CWallet* wallet)
wallet->Flush();
delete wallet;
// Wallet is now released, notify UnloadWallet, if any.
{
LOCK(g_wallet_release_mutex);
if (g_unloading_wallet_set.erase(name) == 0) {
// UnloadWallet was not called for this wallet, all done.
return;
}
if (WITH_SYNCED_LOCK(g_unloading_wallet_set, p, return p->erase(name) == 0)) {
// UnloadWallet was not called for this wallet, all done.
return;
}
g_wallet_release_cv.notify_all();
}
Expand All @@ -240,8 +236,8 @@ void UnloadWallet(std::shared_ptr<CWallet>&& wallet)
// Mark wallet for unloading.
const std::string name = wallet->GetName();
{
LOCK(g_wallet_release_mutex);
auto it = g_unloading_wallet_set.insert(name);
SYNCED_LOCK(g_unloading_wallet_set, p);
auto it = p->insert(name);
assert(it.second);
}
// The wallet can be in use so it's not possible to explicitly unload here.
Expand All @@ -252,10 +248,8 @@ void UnloadWallet(std::shared_ptr<CWallet>&& wallet)
// Time to ditch our shared_ptr and wait for ReleaseWallet call.
wallet.reset();
{
WAIT_LOCK(g_wallet_release_mutex, lock);
while (g_unloading_wallet_set.count(name) == 1) {
g_wallet_release_cv.wait(lock);
}
SYNCED_LOCK(g_unloading_wallet_set, lock);
g_wallet_release_cv.wait(lock, [&lock, &name]() { return lock->count(name) == 0; });
}
}

Expand Down

0 comments on commit b749c4d

Please sign in to comment.