From 11fab41c3638e9368c5589c4092af0b0e29ff054 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Thu, 6 Oct 2016 23:25:43 +0300 Subject: [PATCH] libwallet_api: TransactionHistory: read/write syncchronization --- src/wallet/api/transaction_history.cpp | 31 ++++++++++++++++---------- src/wallet/api/transaction_history.h | 4 ++-- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/wallet/api/transaction_history.cpp b/src/wallet/api/transaction_history.cpp index 5f99750d5..df495591f 100644 --- a/src/wallet/api/transaction_history.cpp +++ b/src/wallet/api/transaction_history.cpp @@ -60,12 +60,24 @@ TransactionHistoryImpl::~TransactionHistoryImpl() int TransactionHistoryImpl::count() const { - boost::lock_guard guarg(m_historyMutex); - return m_history.size(); + boost::shared_lock lock(m_historyMutex); + int result = m_history.size(); + return result; +} + +TransactionInfo *TransactionHistoryImpl::transaction(int index) const +{ + boost::shared_lock lock(m_historyMutex); + // sanity check + if (index < 0) + return nullptr; + unsigned index_ = static_cast(index); + return index_ < m_history.size() ? m_history[index_] : nullptr; } TransactionInfo *TransactionHistoryImpl::transaction(const std::string &id) const { + boost::shared_lock lock(m_historyMutex); auto itr = std::find_if(m_history.begin(), m_history.end(), [&](const TransactionInfo * ti) { return ti->hash() == id; @@ -75,13 +87,16 @@ TransactionInfo *TransactionHistoryImpl::transaction(const std::string &id) cons std::vector TransactionHistoryImpl::getAll() const { + boost::shared_lock lock(m_historyMutex); return m_history; } void TransactionHistoryImpl::refresh() { // multithreaded access: - boost::lock_guard guarg(m_historyMutex); + // boost::lock_guard guarg(m_historyMutex); + // for "write" access, locking exclusively + boost::unique_lock lock(m_historyMutex); // TODO: configurable values; uint64_t min_height = 0; @@ -190,16 +205,8 @@ void TransactionHistoryImpl::refresh() ti->m_timestamp = pd.m_timestamp; m_history.push_back(ti); } - } -TransactionInfo *TransactionHistoryImpl::transaction(int index) const -{ - // sanity check - if (index < 0) - return nullptr; - unsigned index_ = static_cast(index); - return index_ < m_history.size() ? m_history[index_] : nullptr; -} + } diff --git a/src/wallet/api/transaction_history.h b/src/wallet/api/transaction_history.h index 04db7e8e1..1b6617ead 100644 --- a/src/wallet/api/transaction_history.h +++ b/src/wallet/api/transaction_history.h @@ -29,7 +29,7 @@ // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers #include "wallet/wallet2_api.h" -#include +#include namespace Bitmonero { @@ -52,7 +52,7 @@ private: // TransactionHistory is responsible of memory management std::vector m_history; WalletImpl *m_wallet; - mutable boost::mutex m_historyMutex; + mutable boost::shared_mutex m_historyMutex; }; }