Use sync wrapper instead of Mutex (#7456)

This commit is contained in:
Tomasz Drwięga 2018-01-09 16:10:39 +01:00 committed by Marek Kotewicz
parent 69d7c4f519
commit e4bd4ed060

View File

@ -58,7 +58,7 @@ struct CacheQueueItem {
/// Account address. /// Account address.
address: Address, address: Address,
/// Acccount data or `None` if account does not exist. /// Acccount data or `None` if account does not exist.
account: Mutex<Option<Account>>, account: SyncAccount,
/// Indicates that the account was modified before being /// Indicates that the account was modified before being
/// added to the cache. /// added to the cache.
modified: bool, modified: bool,
@ -268,7 +268,7 @@ impl StateDB {
modifications.insert(account.address.clone()); modifications.insert(account.address.clone());
} }
if is_best { if is_best {
let acc = account.account.lock().take(); let acc = account.account.0;
if let Some(&mut Some(ref mut existing)) = cache.accounts.get_mut(&account.address) { if let Some(&mut Some(ref mut existing)) = cache.accounts.get_mut(&account.address) {
if let Some(new) = acc { if let Some(new) = acc {
if account.modified { if account.modified {
@ -409,7 +409,7 @@ impl state::Backend for StateDB {
fn add_to_account_cache(&mut self, addr: Address, data: Option<Account>, modified: bool) { fn add_to_account_cache(&mut self, addr: Address, data: Option<Account>, modified: bool) {
self.local_cache.push(CacheQueueItem { self.local_cache.push(CacheQueueItem {
address: addr, address: addr,
account: Mutex::new(data), account: SyncAccount(data),
modified: modified, modified: modified,
}) })
} }
@ -457,6 +457,13 @@ impl state::Backend for StateDB {
} }
} }
/// Sync wrapper for the account.
struct SyncAccount(Option<Account>);
/// That implementation is safe because account is never modified or accessed in any way.
/// We only need `Sync` here to allow `StateDb` to be kept in a `RwLock`.
/// `Account` is `!Sync` by default because of `RefCell`s inside it.
unsafe impl Sync for SyncAccount {}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use bigint::prelude::U256; use bigint::prelude::U256;